Quick Sort (Quicksort) is an improvement to the bubbling sort. After studying in the university now basically forget, recently in a lot of places have seen the quick sort in the interview will ask, so he is also ready to pick up the previously forgotten things to slowly accumulate their basic knowledge. Fighting
Algorithm Concepts
Quick Sort by C. A. R. Hoare in 1962 (more than 50 years) The basic idea is to divide the data into two separate parts, one by one, and all of the data is smaller than the other part of the data, and then use this method to quickly sort the two parts separately. The entire sorting process can be recursive, in order to achieve the entire data into an orderly sequence (from Baidu Encyclopedia).
Quick sorting takes a strategy of division, so sometimes we call it a divide-and-conquer algorithm.
Algorithmic thinking
1. First, a number is taken from the series as the base number, and the first number is generally taken as the base number.
2. Then the sequence is partitioned, first of all the number larger than this number is placed on its right, less than or equal to the number of the base number on its left (fits, the left is less than the base number, the right is greater than the base number).
3. Repeat the second step to the left and right intervals until each interval has only one number left to complete the sorting.
Below to draw a detailed big picture, only draw the first trip sort of process , know the first lay how to sort, the back is the same.
Algorithm code
PackageCom.roc.Quicksort;/*** Quick Sort * *@authorLIAOWP **/ Public classQuicksort {Static voidSortintValue[],intLeftintRight ) { if(Left < right) {//judging the subscript on the left is a small less right operand. inti = left and j = right;//i is a small value, J is a large value inttemp = Value[left];//The default middle number is temp, which is the leftmost number while(I < J) {//if I<j while(I < J && Value[j] >= temp) {//compare from right to left, if I<j and Value[j]>=tempj--; } if(I < J) {//if i<j, find something bigger than the middle number.Value[i] = Value[j];//the number on the left is the number on the right.i++;//Move left one } while(I < J && Value[i] < temp) {//look from left to righti++; } if(I < J) {//find there are smaller than the middle number, replace to the rightVALUE[J] =Value[i]; J--; }} Value[i]=temp; for(intn = 0; n < value.length; n++) {System.out.print (Value[n]+ " "); } System.out.println (); Sort (value, left, I-1);//repeat on the leftSort (value, I + 1, right);//repeat on the right } } Public Static voidMain (string[] args) {intValue[] = {5, 1, 8, 4, 15 }; System.out.print ("Raw Data:"); for(inti = 0; i < value.length; i++) {System.out.print (Value[i]+ " "); } System.out.println (); Sort (value,0, 4); System.out.print ("Sort Result:"); for(inti = 0; i < value.length; i++) {System.out.print (Value[i]+ " "); } }}
The code results are as follows:
Raw DATA: 5 1 8 4 4 1 5 8 1 4 5 8 1 4 5 8 results sorted by:
Note: The first time quick sort does not directly get the final result, only the number that is smaller than the base number and the base number is divided to both sides of the base number. To get the final result, you need to perform this step on each of the arrays on both sides of the base number again, and then decompose the array until the array is no longer decomposed (only one data) to get the correct result.
Fast ordering of Java basic algorithms