6. Fast sorting and sorting of comparative sorting
Quick sorting (quick sorting for short) is often used in pen questions because of its high efficiency (average O (nlogn.
The first step of the quick rank is to select a "base", which will be used for comparison and exchange with other numbers.The "base" option will affect the efficiency of the quick ranking, but if you select the base to select the base, the Base will be put upside down. For example, to find the optimal base, you need to find the median in the entire column to be sorted, but the search for the median actually costs a lot. The base selection is usually the first object in the sequence to be sorted, an object in the middle, or the last object. This article takes the first element as an example to make a brief analysis and implementation of the quick sort.
Take the {6, 5, 3, 1, 7, 2, 4} columns to be sorted as an example, and select the first element 6 as the base.
After selecting the base number, you need to compare and exchange it with the array elements. How can we compare it with who?The second step of the Quick Sort sets a "Sentinel" for each of the first and last elements of the array ".
After selecting the base number and setting the Sentinel, start the comparison,Compare the base number with the last sentinj. If it is greater than sentinj, the Sentinel I + 1 is exchanged with it..
At this time, the base is no longer compared with the sentinj, but compared with the sentini, if the base is greater than the sentini, the Sentinel continues to move back until the base is greater than the sentinj and the Sentinel J-1.
Repeat the preceding steps to compare the base number with that of the guard j.
The final result shows that the position of the Sentinel I is the position of the Sentinel j. At this time, the base value is assigned to this position.
In this way, the number on the left of base 6 is smaller than it, and the number on the right is greater than it. Then, recursively select the base from the left and right arrays of base 6 and set the Sentinel, finally, the sorting can be completed.
Java
1 package com. algorithm. sort. quick; 2 3 import java. util. arrays; 4 5/** 6 * fast sorting 7 * Created by yulinfeng on. 8 */9 public class Quick {10 public static void main (String [] args) {11 int [] nums = {6, 5, 3, 1, 7, 2, 4}; 12 nums = quickSort (nums, 0, nums. length-1); 13 System. out. println (Arrays. toString (nums )); 14} 15 16/** 17 * fast sorting 18 * @ param nums to be sorted array sequence 19 * @ param left array first element index 20 * @ param right array last element index 21 * @ return: sorted array sequence 22 */23 private static int [] quickSort (int [] nums, int left, int right) {24 if (left <right) {25 int temp = nums [left]; // base 26 int I = left; // sentinel i27 int j = right; // sentinel j28 while (I <j) {29 while (I <j & nums [j]> = temp) {30 j --; 31} 32 if (I <j) {33 nums [I] = nums [j]; 34 I ++; 35} 36 while (I <j & nums [I] <temp) {37 I ++; 38} 39 while (I <j) {40 nums [j] = nums [I]; 41 j --; 42} 43} 44 nums [I] = temp; 45 quickSort (nums, left, I-1 ); 46 quickSort (nums, I + 1, right); 47} 48 return nums; 49} 50}
Python3
1 # Quick sorting 2 def quick_sort (nums, left, right): 3 if left <right: 4 temp = nums [left] # base 5 I = left # guard I 6 j = right # guard j 7 while I <j: 8 while I <j and nums [j]> = temp: 9 j-= 110 if I <j: 11 nums [I] = nums [j] 12 I + = 113 while I <j and nums [I] <temp: 14 I + = 115 if I <j: 16 nums [j] = nums [I] 17 j-= 118 nums [I] = temp19 quick_sort (nums, left, I-1) 20 quick_sort (nums, I + 1, right) 21 22 return nums23 24 nums = [6, 5, 3, 1, 7, 2, 4] 25 nums = quick_sort (nums, 0, len (nums)-1) 26 print (nums)