java quick sort algorithm most straightforward explanation!

Quick sort of basically thought:

  • Select the first element of the array as the reference value key.
  • The last element of an array of high forward sequentially compares with the key from the partition will not be less than the number of full-key into the right side of it, the whole is not greater than the number of key put it on the left.
  • After comparing the key exchange element in a reference range around again repeat step until each section is only a few

Specific implementation of the algorithm:
Here Insert Picture Description
The basic idea is described, has been marked in the figure key and the last element of high


Here Insert Picture Description
key start comparing with the last element, that is, compared with 34 4, because thought needs to be not less than the key number into its whole right side , so 4 to 34 will continue to be relatively small, and so on and 33, when the comparison to 3:00, with the 3 key exchange, key index is 0 unchanged from the last element again be compared from the back, the right key until this is all bigger than its numbers. Made a mark from the figure, no means no exchange, yes indicates a swap.


According to the idea for the next key element, repeat the above steps.
Here Insert Picture Description
Can only be exchanged with 4, the following is the result of the exchange:
Here Insert Picture Description


2 is an index key, no exchange occurs because the right is larger than the index number 2.
Here Insert Picture Description
Since there is no exchange, the index of +1 key


Here Insert Picture Description
After the exchange, key index remained unchanged and continue to be compared with the last index, and so on.
Here Insert Picture Description
Here Insert Picture Description
The final results of the exchange:
Here Insert Picture Description
Key index continued to +1, making comparisons, because the same principle, it is not elaborated, and an example of the array has been sorted over, so directly display the final results will be sorted:
Here Insert Picture Description
Code to achieve the following:

//low是定义一个变量作为key
//high为数组最后一个元素
 public static void sort(int[] arr,int low,int high){
      int i,j,key,t;
       if(low>high){
           return;
       }
       i=low;
       j=high;
       //key就是基准位
       key = arr[low];

       while (i<j) {
           //先看右边,依次往左递减
           while (key<=arr[j]&&i<j) {
               j--;
           }
           //再看左边,依次往右递增
           while (key>=arr[i]&&i<j) {
               i++;
           }
           //如果满足条件则交换
           if (i<j) {
               t = arr[j];
               arr[j] = arr[i];
               arr[i] = t;
           }

       }
       //最后将基准为与i和j相等位置的数字交换
       arr[low] = arr[i];
       arr[i] = key;
       //递归调用左半数组
       sort(arr, low, j-1);
       //递归调用右半数组
       sort(arr, j+1, high);
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
      int[] arr =new int[]{4,12,3,52,15,33,34};
       sort(arr,0,6);
        for (int i = 0; i < arr.length; i++) {
           System.out.println(arr[i]);
       }
  }

Results are as follows:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/fight252/article/details/90814870