In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use java to achieve rapid sorting, Xiaobian feels quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
1)Introduction to Quick Sort Algorithm
Quicksort is an improvement on bubble sort. The basic idea is to divide the data to be sorted into two independent parts by a lie sort, all the data in one part are smaller than all the data in the other part, and then quickly sort the two parts of the data according to this method. The whole sorting process can be recursively carried out, so as to achieve the whole data into an ordered sequence.
2)Quick sort schematic:
3)Quick sort algorithm application real column:
Requirements: Sort [-9,78,0,23,-567,70] from small to large, requiring quick sorting.
a. If you cancel the left-right recursion, the result is-9 -567 0 23 78 70.
b. If you cancel the left-right recursion, the result is-567 -9 0 23 78 70.
c. If you cancel left recursion, the result is-9 -567 0 23 70 78
d. code implementation
import java.util.Arrays;//QuickSort public class QuickSort { public static void main(String[] args) { int[] arr= {-9,78,0,23,-567,70, -1,900, 4561}; System.out.println("sort before"); System.out.println(Arrays.toString(arr)); QuickSort.quickSort(arr, 0, arr.length-1); System.out.println("After sorting"); System.out.println(Arrays.toString(arr)); } public static void quickSort(int[] arr,int left,int right) { int l = left;//left subscript int r = right;//right subscript //central axis value, median value int pivot = arr[(left+right)/2]; int temp = 0;//intermediate value, used as an exchange /*while the loop is designed to put values smaller than pivot to the left and larger than pivot to the right */ while(l
< r) { //在pivot的左边一直找,找到大于等于pivot值,才退出 while(arr[l]pivot) { r-=1; } /* * 如果l>=r indicates that the left and right values of pivot have been determined according to the values on the left that are all less than or equal to pivot, and on the right that are all greater than or equal to pivot. */ if(l>=r) { break; } //Exchange temp=arr[l]; arr[l]=arr[r]; arr[r]=temp; //If after the exchange, we find that arr[l]==pivot is equal to r--, move forward if(arr[l]==pivot) { r-=1; } //If after the exchange, we find that arr[r]==pivot value is equal to l++, shift back if(arr[l]==pivot) { l+=1; } } //If l==r, it must be l++,r--, otherwise stack overflow will occur if(l==r) { l+=1; r-=1; } //left recursion if(leftl) { quickSort(arr, l, right); } }} About "how to use java to achieve rapid sorting" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.