Quick sort algorithm and JAVA implementation

Quick sort

Sorting idea : Divide the reverse-sorted sequence into two parts through one-pass sorting, one of which is smaller than the other part, and then perform the next sorting on the two parts to achieve the order of the entire sequence

Average time complexity: O(nlog2(n)), n*log with 2 as the base n

The worst case is: O(n^2), the square of n

Sorting process

  1. First set a boundary value , and divide the array into left and right parts by the boundary value
  2. The data is greater than or equal to the boundary values to the right array concentration , concentration data less than the boundary value to the left of the array . At this time, each element in the left part is less than the cutoff value, and each element in the right part is greater than or equal to the cutoff value (see how to divide)
  3. Then, the data on the left and right can be sorted independently . For the array data on the left, you can also take a demarcation value to divide this part of data into left and right parts, and also place the smaller value on the left and the larger value on the right. The array data on the right can also be processed similarly
  4. Repeat the above process , you can see that this is a recursive definition. After recursively sorting the left part, recursively sort the right part. When the data in the left and right parts are sorted, the sorting of the entire array is completed

Sorting steps

  1. Set two variables i, j, when sorting starts: i=0, j=N-1
  2. Take the first array element as the key data and assign it to key , namely key =A[0]
  3. Search forward from j, that is, search forward from the back (j–), find the first value A[j] that is less than key , and exchange the values ​​of A[j] and A[i]
  4. Search backwards from i, that is, search backwards from the front (i++), find the first A[i] greater than the key , and exchange the values ​​of A[i] and A[j]
  5. Repeat steps 3 and 4 until i==j; (In steps 3 and 4, no qualified value is found, that is, A[j] in 3 is not less than key , and A[i] in 4 is not greater than key . The value of j and i is such that j=j-1, i=i+1, until it is found. Find the value that meets the conditions, and the pointer positions of i and j remain unchanged when the exchange is performed. In addition, the process of i==j It must happen exactly when i+ or j- is completed, and the loop ends at this time)

A trip to the quick sort algorithm demonstration

​ Initial keyword sequence: 49 , 38, 65, 97, 76, 13, 27, 49

​ Take the first place in the array as a reference for quick sorting: 49

  1. The first sort : 49 with the right comparison - 49, 49 = 49, and then find 49> 27, 27 into the left 49 (exchange position)
    • 27、38、65、97、76、13、49、49
  2. The second sort : from the left found 65> 49 side, switching position
    • 27、38、49、97、76、13、65、49
  3. Third Sort : From the right start looking to play 13 <49, exchange position
    • 27、38、13、97、76、49、65、49
  4. Fourth sort : from the left found 97> 49, exchange position
    • 27、38、13、49、76、97、65、49
  5. The last trip sort : from the right to find 76, 49 and then find, under the same standard, the end of the first trip, were ranked continue to sort an array of left and right
    • {27、38、13}、49、{76、97、65、49}

JAVA implements quick sorting algorithm

Define implementation by quick sort

public static void main(String[] args) {
    
    
    int[] array = {
    
    49,38,65,97,76,13,27,49};
    int low = 0;
    int high = array.length - 1;
    quicksort(array, low, high);
}

/**
 * 快速排序 ,循环执行 step1 和 step2
 * @param array 数组
 * @param low 每次比较的最低位
 * @param high 每次比较的最高位
 */
private static void quicksort(int[] array,int low,int high) {
    
    
    //记录开始位置,是下次左边数组(比分解值小的一边)排序的最低位。
    int start = low;
    //记录此次排序的最高位,是下次右边数组(比分界值大的一边)排序的最高位
    int end = high;
    //取数组第一位作为分界值
    int ref = array[low];
    //如果低位和高位重合则结束此趟排序
    while (low != high){
    
    
        //step1 先顺高位寻找小于分界值
        while (high > low){
    
    
            //如果array[high] 小于 分界,交换位置 high不指向下一位
            if(array[high] < ref){
    
    
                int temp = array[high];
                array[high] = array[low];
                array[low] = temp;
                printArray(array);
                break;
            }
            high --;
        }
        //step2 低位寻找高于分界值
        while (low < high){
    
    
            //如果array[low]或等于分界值 交换位置,low不指向下一个位置,继续比较高位
            if(array[low] >= ref){
    
    
                int temp = array[high];
                array[high] = array[low];
                array[low] = temp;
                printArray(array);
                break;
            }
            low ++;
        }
    }
    // low的值和最开始进来的值相等,说明只有一位结束循环
   if(low != start){
    
    
        quicksort(array,start,low - 1);
    }
    // low - 1 和 high + 1。因为分解值必然比右边小,必然比左边大,所有一个位置减1一个加1
    if(high != end){
    
    
        quicksort(array,high + 1,end);
    }
}

/**
 * 输出数组内容
 */
private static void printArray(int[] array){
    
    
    Arrays.stream(array).forEach(value -> {
    
    
        System.out.print(value + " ");
    });
    System.out.println();
}

Another implementation on Baidu Baike

public static void main(String[] args) {
    
    
    int[] array = {
    
    49,38,65,97,76,13,27,49};
    int len = array.length-1;
    qsort(array,0,len);

}

/**
 * 递归调用,第二次用的子数组,注意子数组下标位置
 * @param arr
 * @param start
 * @param end
 */
private static void qsort(int[] arr,int start,int end) {
    
    
    //开始位置的值,标记值 ,大的放右边,小的放左边
    int pivot = arr[start];
    //初始的开始和结束位置赋值给i和j,变动i和j,初始值不变下次递归判断用。
    int i = start;
    int j = end;
    //i < j
    while (i < j) {
    
    
        //当arr[j]最高位大于标记值,则继续寻找下一位,直到找到小于或等于标记值的元素,不超过最低位 i
        while ( i < j && arr[j] > pivot) {
    
    
            j--;
        }
        //arr[i]小于标记值,一直向上寻找,直到找到大于标记值或者找到最高位j结束循环
        while ( i < j && arr[i] < pivot) {
    
    
            i++;
        }
        //如果 arr[i]==arr[j] 则i进一位,且i不能超过j,也就是查找j找过的元素
        if (arr[i]==arr[j] && i < j ) {
    
    
            i++;
        } else {
    
    
            //交换j找到的小于标记值的元素 和 i找到大于标记值的元素
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            printArray(arr);
        }
    }
    //递归调用
    if (i-1>start) {
    
    
        qsort(arr,start,i-1);
    }
    if (j+1<end) {
    
    
        qsort(arr,j+1,end);
    }
}

/**
 * 输出数组内容
 */
private static void printArray(int[] array){
    
    
    Arrays.stream(array).forEach(value -> {
    
    
        System.out.print(value + " ");
    });
    System.out.println();
}

Guess you like

Origin blog.csdn.net/zhaoqingquanajax/article/details/114993758