java sort --- quick sort

1. Introduction

       Among the sorting algorithms, quicksort is one of the best. It is an improvement to the bubble sort. The basic idea is to divide the records to be sorted into two independent parts through one sorting, and the keywords of one part of the records are smaller than the keywords of the other part of the records, then the records can be sorted separately. These two parts of the records continue to be sorted to achieve ordering of the entire sequence. The division into two parts is mentioned here, which makes us think of the idea of ​​divide and conquer, which is actually to let the other two parts realize the previous sorting. Because to find the key, the key is used to split the two parts, so the best case is that the key is in the middle of the sorted array. The most extreme case is when this keyword is max or max.

Sorting process:


2. Code implementation

public class QuartSort {
    public static void main(String[] args) {
        int a[] = {5, 2, 6, 3, 7, 9, 10, 4, 8};
sort(a, 0, a.length - 1);
print(a);
}                    

    /**
     * 打印数组
     *
     * @param a
*/
private static void print(int[] a) {
        for (int i = 0; i < a.length; i++) {         
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }

    /**
      * Quick sort
      *
      * @param a     array
      * @param low   low
      * @param high high
      */
 static void sort ( int [] a , int low , int high) {
         if (low < high) {   //judgment The low order should be smaller than the high order
 int index = partition (a , low , high) ;   //Find out the position of the keyword key, and those larger than the key are on the right, and those smaller than the key are on the left
 sort (a , low , index -                            1 ) ;   //Quick sort on the left
 sort (a , index + 1 , high) ;   //Quick sort on the right
 }                    
    }

    /**
      * Find the position of the keyword key, and those larger than the key are on the right, and those smaller than the key are on the left
      *
      * @param a     array
      * @param low low   position
      * @param high high position
      * @return returns the position of the keyword
      * /
 static int partition ( int [] a , int low , int high) {
         int key = a[low] ;   //Save the keyword, the default is to go to the low value
 while (low < high) {   //The low order is smaller than the high order
 while (low < high && a[high] >= key) {   //Search from right to left, if the array is found to be greater than the key, the high bit is reduced by 1
 --high                                        ;
            }
            a[low] = a[high] ;   //In the case that the low position is less than the high position, if the value of the high position is less than the key, put the value of the high position above the low position
 while (low < high && a[low] <= key ) {            
                ++low;
            }
            a[high] = a[low] ;   ////In the case that the low order is less than the high order, if the value of the low order is greater than the key, put the value of the low order above the high order
         }
        a[low] = key ;   //Assign the keyword to the low position
 return low ;    //Return the position of the keyword
 }            
}

The comment code has been written very clearly, the result:

2 3 4 5 6 7 8 9 10 

3. Summary

In terms of average time, the sorting algorithm is considered to be the best internal sorting method at present. Its time complexity is nlog(n).

Next: Heap Sort

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325151777&siteId=291194637