Quick Sort is the improved version of bubble sort, and the best kind of sort, which will appear in many interview questions, and also as a sort method that programmers must master.
Thought: 1. The element to be sorted takes one element as the datum (usually the first element is selected, but the most selective method is to randomly select one of the elements to be sorted as the datum), called the Datum element;
2. Partition the element to be sorted, and the element larger than the datum element is placed on its right, smaller than its left side;
3. Repeat the above steps for the left and right two partitions until all elements are ordered.
So I was a quick sort of Lenovo to the East or west of the demolition of the East , one side of the complement , until all elements reached an orderly state.
Let's take a look at the following diagram to understand it:
6. Repeat for element 52 side elements until the element reaches an orderly state.
Algorithm implementation:
Public classQuickSort { Public Static voidQuickSort (intArr[],int_left,int_right) { intleft =_left; intright =_right; inttemp = 0; if(Left <=Right ) { //the element to be sorted has at least two cases temp=Arr[left]; //The first element to be sorted as a datum element while(Left! =Right ) { //alternately scan from left to right
while(Right > Left && arr[right] >=temp) Right--; //scan from left to right to find the first element that is smaller than the Datum element Arr[left]=Arr[right]; ///find this element Arr[right] swap with Arr[left]
while(Left < right && Arr[left] <=temp) left++; //scan from right to left to find the first element larger than the base element Arr[right]=Arr[left]; ///find this element Arr[left], swap with arr[right]
} Arr[right]=temp; //Datum element homing QuickSort (Arr,_left,left-1); //recursive ordering of elements to the left of the Datum element QuickSort (arr, right+1, _right); //recursive ordering to the right of the Datum element } } Public Static voidMain (string[] args) {intArray[] = {10,5,3,1,7,2,8}; System.out.println ("Before sorting:"); for(intElement:array) {System.out.print (element+" "); } quickSort (Array,0,array.length-1);
System.out.println ("\ n Sort after:"); for(intElement:array) {System.out.print (element+" "); } }}
Sort Result:
before sorting:5 3 1 7 2 8 after sorting:
Algorithm analysis: 1. When the base element selected by the partition is the maximum or minimum value in the element to be sorted, for the worst case, the time complexity is the same as the direct insertion sort, and the number of moves reaches the maximum value
Cmax = 1+2+...+ (n-1) = N (n-1)/2 = O (n2) At this time it is better to have a complex O (N2)
2. When the base element selected by the partition is the "median" in the element to be sorted, the time complexity is O (nlog2n) for the best case.
3. The spatial complexity of quick sorting is O (log2n).
4. When the element to be sorted resembles [6,1,3, 7,3] and the datum element is 6 o'clock, it is partitioned to form [1,3,3, 6,7], two 3 of the relative position has changed, is Quick Sort is an unstable sort.
Fast sequencing of data structures