The quick sort algorithm (recursive and non-recursive)

Two implementations of quick sort recursive and non-recursive

  1 package com.ebiz.sort;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.Arrays;
  5 import java.util.Date;
  6 import java.util.Stack;
  7 
  8 /**
  9  * @author YHj
 10  * @create 2019-08-18 17:42
 11  */
 12 public class Quick {
 13 
 14     public static void main(String[] args) {
 15         int[] arr = new int[8];
 16         for (int i = 0; i < 8; i++) {
 17             arr[i] = (int) (Math.random() * 800000);
 18         }
 19 
 20         String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
 21         System.out.println("排序前 = " + s);
 22 
 23         quickSort(arr,0,arr.length-1);
 24 
 25 
 26         String l = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDATE ());
 27          System.out.println ( "sorted =" + L);
 28      }
 29  
30      / ** 
31 is       * quicksort (recursively)
 32       * <P>
 33 is       . * ① pick from a number of column element, known as the "benchmark" (pivot).
34       * ②. Reorder columns, all smaller than the reference value elements placed in front of the base, all larger than the reference value of the element placed behind the baseline (the same number can be either side). After the end of the partition, the reference is in the middle of the number of columns. This is called the partition (partition) operation.
35       * ③. Recursively (Recursively) than the reference value the number of columns and the sub-element is greater than the number of columns a sorting reference value of the element.
36       *
 37 [       * @param ARR to be sorted array
 38 is       * @param Low left boundary
 39       * @param High right border
 40      */
 41     public static void quickSort(int[] arr, int low, int high) {
 42         if (arr.length <= 0){
 43             return;
 44         }
 45         if (low >= high) {
 46             return;
 47         }
 48         int left = low;
 49         int right = high;
 50 
 51         int temp = arr[left];   //挖坑1:保存基准的值
 52         while(left < right) {
 53 is              the while (left <right && ARR [right]> TEMP =) {   // Pit 2: found smaller than the reference element forwardly from the rear, inserted into the reference position of the pit. 1 
54 is                  Right - ;
 55              }
 56 is              ARR [left] = ARR [right];
 57 is              the while (left <right && ARR [left] <= TEMP) {    // pit 3: front to back found greater than the reference element, into a pit dug just 2 
58                  left ++ ;
 59              }
 60              ARR [right] = ARR [left];
 61 is          }
 62 is          ARR [left] = TEMP;    // reference value to fill the pit 3, prepared partition recursive fast row
63 is          System.out.println ( "the Sorting:" + of Arrays.toString (ARR));
 64          QUICKSORT (ARR, Low, left -. 1 );
 65          QUICKSORT (ARR, left +. 1 , High);
 66      }
 67      / ** 
68       * quick sort (non-recursive)
 69       *
 70       * ①. singled out a number of elements from the column, called "benchmarks" (pivot).
71       * ②. Reorder columns, all smaller than the reference value elements placed in front of the base, all larger than the reference value of the element placed behind the baseline (the same number can be either side). After the end of the partition, the reference is in the middle of the number of columns. This is called the partition (partition) operation.
72       * ③. After partitioning the two border sections (low and high) pushed onto the stack to save, and the cycle ①, ② the step
 73 is       * @param ARR to be sorted array
 74       * / 
75      public static  void quickSortByStack ( int [] ARR) {
 76          IF (arr.length <= 0 ) {
 77              return ;
 78          }
 79          Stack <Integer> = Stack new new Stack <Integer> ();
 80  
81          // about the initial state pointer stack 
82          stack.push (0 );
 83          stack.push (arr.length -. 1 );
 84          the while (! {stack.isEmpty ())
 85              int High stack.pop = ();      // out of the stack to be divided 
86              int Low = stack.pop();
 87 
 88             int pivotIdx = partition(arr, low, high);
 89 
 90             //保存中间变量
 91             if(pivotIdx > low) {
 92                 stack.push(low);
 93                 stack.push(pivotIdx - 1);
 94             }
 95             if(pivotIdx < high && pivotIdx >= 0){
 96                 stack.push(pivotIdx + 1);
 97                 stack.push(high);
 98             }
 99         }
100     }
101 
102      Private  static  int Partition ( int [] ARR, int Low, int High) {
 103          IF (arr.length <= 0 ) {
 104              return -1 ;
 105          }
 106          IF (Low> = High) {
 107              return -1 ;
 108          }
 109          int L = Low;
 110          int R & lt = High;
 111  
112          int Pivot ARR = [L];     // digging 1: stored reference value 
113          the while(L < R & lt) {
 114              the while (L <R & lt && ARR [R & lt]> = Pivot) {   // Pit 2: found smaller than the reference element forwardly from the rear, inserted into the reference position of the pit. 1 
115                  r-- ;
 1 16              }
 117              ARR [L] = ARR [R & lt];
 1 18              the while (L <R & lt && ARR [L] <= Pivot) {    // pit 3: front to back found greater than the reference element, into a pit dug just 2 
119                  L ++ ;
 120              }
 121              ARR [R & lt] = ARR [L];
 122          }
 123          ARR [L] = Pivot;    // reference value to fill the pit 3, the recursive partition prepare fast row 
124          return L;
125     }
126 
127 
128 }

 

Guess you like

Origin www.cnblogs.com/jiushixihuandaqingtian/p/11449371.html