Algorithm Basics - Quick Sort

Requirement: Write Quicksort in 10 minutes!

I used the sorting algorithm in the written test before, and I always wanted to use the quick sort, but I couldn't write it out in the specified time. In the final analysis, the logical steps of the quick sort were not clear, which led to each failure! Thoroughly understand the logic of the quicksort action, so that you can write it out temporarily even if you don't use it for a long time!

Ideas:

1. Just like the selection sort sorts the smallest/largest value every time, the quick sort will sort the central axis value (the first in the array by default), and the previous central axis value is less than it , and the central axis value is always greater than it. In this way, before the central axis value (arr, low, middle-1) / after the central axis value (arr, middle+1, high) as a new array can be quickly sorted until the number of arrays is 1.

public void _quickSort(int() arr,int low,int high) {

    if(low < high) { //The number of arrays is not 1

        int middle = getMiddle(arr,low,high); //Quick sort sort order   

        _quickSort(arr,low,middle-1);

        _quickSort(arr,middle+1,high);

    }

}

2. The logic of each sorting is: put the first element of the array (the selected central axis value) at the position where it should be and return its subscript after sorting. Those who are bigger than him should be placed behind him, and those who are smaller than him should be placed in front of him.

We take out the central axis value and look for it from the back to the front, while(arr[high]>tmp) high-- ;until we encounter a value smaller than the central axis value, arr[low] = arr[high]; The value is assigned to the front of the central axis value; then pause to search from the back to the front, and start to search from the front to the back, while(arr[low]<tmp) high++; until it encounters a value larger than the central axis value, arr[high]=arr[ low]; This alternate cycle (this prevents the overwriting of the value) until low == high; and at this time low/high is the subscript of the central axis value, arr[low]=tmp;

private int getMiddle(int[] arr,int low,int high) {

    int tmp = arr[low];    

    while(low< high) {

        while(low < high && arr[high]>tmp) high --;

        arr[low]=arr[high];

        while(low<high && arr[low]<tmp) low++;

        arr[high] = arr[low];

       }

    arr[low]=tmp;

    return low;

}

 

To sum up: our quicksort is ready!

public class QuickSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {32,43,1,344,63,12,34,444,2,3,32,54};
int[] arr2 = {5};
QuickSort qs = new QuickSort();
qs.quickSort(arr2);
for(int i : arr2) {
System.out.print(i+" ");
}
}
public void quickSort(int[] arr) {
if(arr.length>0) { //查看数组是否为空
_quickSort(arr,0,arr.length-1);
}
}
private void _quickSort(int[] arr,int low,int high) {
if(low < high) {
int middle = getMiddle(arr,low,high);
_quickSort(arr,low,middle-1);
_quickSort(arr,middle+1,high);
}
}
//Single sorting of quick sort: The central axis value is not the middle value, it returns the coordinates of your random (the first in the array) central axis value after sorting
private int getMiddle(int[] arr,int low ,int high) {
int tmp = arr[low]; // the first in the array is the center axis
while(low < high) { 
while(low<high ​​&& arr[high] >= tmp) {
high--;
}
arr[low] = arr[high]; // move the value smaller than the middle axis to the low end
while(low<high ​​&& arr[low] <= tmp) {
low++;
}
arr[high]=arr[low ]; //
}
//low == high
arr[low] = tmp;
return low;
}
}





Guess you like

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