There is an algorithm for fast sorting, which is very carefully written, and is good enough for understanding the idea of a fast line. But recently, in the subject of the written examination on the topic of quick sorting, found that the general topic is the first element of the specified array of elements, and I am relatively good at the last element as the principal element, it is very embarrassing. In fact, in order to achieve the best time complexity of the queue, in addition to the array to be sorted out of order, the choice of the main element is a determinant, the choice of the first and last element as the principal element, in fact, is not the best choice. Only random selection of the main element is possible to achieve the best time complexity. Suppose to order is a[0] ... A[N-1], first select the first number of the array as the key data, and then put all the smaller than its number in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm. A quick sorting algorithm is: 1) Set two variables I, J, the beginning of the order: I=0,J=N-1;2) with the first array element as the key data, assigned to key, i.e., key=a[0];3) from the start of J forward search, that is, from the beginning to search forward (j--), to find
The first one is less than keyValue a[j], a[j] and A[i] interchange, 4) from I start backward search, that is, from the front to start backward search (i++), find
The first one is greater than keyA[i], will a[i] and A[j] interchange, 5) Repeat the 3rd, 4 steps, until I=j, 3, 4 steps, did not find the matching criteria, that is, 3 a[j] not less than key,4 in the time of the key change J, I value, so that a[i until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J is complete, at which time the loop ends.
Public classQuickSort {/*** Quick sort of Main method *@paramarray: Arrays to be sorted *@parambegin: The subscript of the first element of the array (actually 0) *@paramEnd: The subscript of the last element of the array (actually the array length-1) *@return */ Public Static int[] QuickSort (int[] Array,intBeginintend) { if(Begin <end) { //get the main element location intK =Partitionwithfirst (array, begin, end); //re-dividing the left element of the main elementQuickSort (array,begin,k-1); //re-dividing the elements on the right of the main elementQuickSort (array,k+1, end); } returnArray; } Public Static intPartitionwithfirst (int[] Data,intHeadintend) { intKey=data[head];//Takes the first element of the array, takes data[head] as the main meta key intI=Head; intj=end; while(J >i) { //traverse the target array in j--direction until a value smaller than key is reached while(Data[j] >= key && J >i) { --J; } if(I <j) {Data[i]= Data[j];//Data[i] has been saved in the key, can be less than the number of key data[j] fill in the position I placei++; } //traverse the target array in i++ direction until a value larger than key is reached while(I < J && Data[i] <=key) {i++; } if(I <j) {Data[j]= Data[i];//As can be seen from the above, Data[j] has been saved in data[i], you can put the previous value is greater than key book Data[i] fill in the position of Jj--; }} Data[i]= key;//now I==j, put the main element back into the array returni; } Public Static voidMain (string[] args) {int[] Array = {2,8,7,1,3,5,6,4,9}; intLen = array.length-1; Array= QuickSort (Array, 0, Len); for(inti = 0; i < Array.Length; i++) {System.out.print (array[i]); } }}
Quick Sort Algorithm 2---The first element as the primary