Quick sort Study Notes

Quick Sort

 

 

 


Ordering process

The basic idea

Quick sort and bubble sort, is based on the comparison and exchange of sorting algorithms, quick sort based on the idea of ​​divide and conquer, Bubble Sort is optimized.

The basic idea is: choose a sequence from a reference element , a sequence into two portions, wherein a portion of the element is smaller than the other portion of the element, and then repeat the process for each two parts, the entire sorting process can be recursive.

      

 


 

(1) an element selected from the sequence as a reference element ( selected reference element will affect the efficiency of sorting );

(2) using the reference element will sequence into two subsequences, wherein all elements of a sequence are smaller than the other sequence;

(3) sub-sequence repeats the process until the sequence is only one element.

 

FIG sorting process on a sequence used as a reference element of the first element

analysis

 

 1, when using a quick-sort more appropriate? Quick Sort Why fast?

    The larger the data size, the more excellent quick sort performance. The advantages and fast ordering in, no extra compared to less overall number of comparisons.

2, the best case, worst case? (In ascending order as an example)

 

 

 Mentioned above, the selection criterion will affect the efficiency of sorting the elements in the worst case, the reference elements are each selected interval smallest element, the best case, the reference elements are each selected interval intermediate value.

Worst case:

    The worst case, a total of n-1 time division.

    When the i-th divided interval length of ni + 1 (arithmetic sequence, term formula A i  = A . 1 + (. 1-n-) D), ni for comparisons.

For a total of n (n-1) / 2 comparisons (arithmetic sequence summing), the time complexity of O (n- 2 ).

Best case:

    At best, a total of n times divided.

    The time complexity of O (nlog n- ).

The average time complexity: O (nlog 2 n- )

Code:

 

1      / * 
2       * divided sections
 . 3       *
 . 4       * @param randomArray - to be sorted sequence
 . 5       * @param Low - Interval Lower Bound sort
 . 6       * @param High - sorting the interval boundaries
 . 7       * @return - After the division, where the reference element position
 . 8       * / 
. 9      public  static  int Divide (Integer [] randomArray, int Low, int High) {
 10          // If only two elements interval 
. 11          IF (High - Low ==. 1 ) {
 12 is              int TEMP;
13 is              IF (randomArray [Low]> randomArray [High]) {
 14                  TEMP = randomArray [Low];
 15                  randomArray [Low] = randomArray [High];
 16                  randomArray [High] = TEMP;
 . 17              }
 18 is              return -1 ;
 . 19          }
 20          // 1, select the reference element 
21 is          int datum = Low;
 22 is          // 2, traversing interval, divided interval. 
23 is          int TEMP;
 24          the while (Low < High) {
 25              //
 26 is             while (low < high && randomArray[low] <= randomArray[datum]) {
27                 low++;
28             }
29             //
30             while (high > low && randomArray[high] > randomArray[datum]) {
31                 high--;
32             }
33             temp = randomArray[low];
34             randomArray[low] = randomArray[high];
35             randomArray[high] = temp;
36         }
37         //交换基准元素至正确位置
38         temp = randomArray[datum];
39         randomArray[datum] = randomArray[low - 1];
40         randomArray[low - 1] = temp;
41         return low - 1;
42     }

 

 

1      / * 
2       * Quicksort
 . 3       *
 . 4       * @param randomArray - to be sorted sequence
 . 5       * @param Low - Interval Lower Bound sort
 . 6       * @param High - Upper Bound sorting section
 . 7       * @return - ordered sequence of
 8       * / 
9      public  static Integer [] quickSorted (Integer [] randomArray, int Low, int High) {
 10          // interval greater than 1 only elements necessary sorting 
. 11          IF (High - Low> = 1 ) {
 12 is              // 1, division section 
13             int Datum = Divide (randomArray, Low, High);
 14              // 2, whether to continue to divide 
15              IF (Datum = -1! {)
 16                  quickSorted (randomArray, Low, Datum -. 1 );
 . 17                  quickSorted (randomArray, Datum + . 1 , High);
 18 is              }
 . 19          }
 20 is          return randomArray;
 21 is      }

Guess you like

Origin www.cnblogs.com/KenBaiCaiDeMiao/p/12482219.html