Eight quick sort sorting algorithm of Java

First, the moving map presentation

Second, the idea of ​​analysis

Quick sort of thinking is, choose a number as the base (here I chose the first number), is greater than the base into the right side, less than the base into left, equal to the number of base may be placed on the left or right see their habits, where I was put on the left,

After the end of the trip, the intermediate partition into the base position, the second pass from the position of the array in half of the base, the two arrays divided continues to repeat the above steps, the base is selected from, on the left decimal base, the large numbers into the right base, in a striped array ,,, until arrays can not be divided up, ordering an end.

For example, from small to big Sort:

1. The   first pass, the first number of the base temp, provided two pointers left = 0, right = n.length,

  ① right from the start compared with the base temp, if n [right]> temp base, a pointer to the right to move forward, to continue with the comparison base temp, not satisfied until n [right]> temp base

  ② The n [right] assigned n [left]

  ③ from the left compared with the start temp base, if n [left] <= temp base, then moves back a left hand, the comparison continues with the base temp, is not satisfied until the n [left] <= temp Base

  ④ The n [left] assigned n [rigth]

  ①-④ ⑤ repeating step until the end of the left == right, the assigned base temp n [left]

2. A  second pass, separated from the intermediate array, then each array operating step 1, and then quickly separated array and then discharging the separated,

3.  recursive partition fast row repeated until the array can not be divided, that is, when only one element, the end of the recursion, the sort is complete

The idea of ​​analysis, the first trip execution flow as shown below:

 

Third, the negative heteroaryl analysis

1. The time complexity:

The worst situation is to take every element of the array is the minimum / maximum, this is actually a bubble sort (every time a row good sequence elements)

这种情况时间复杂度就好计算了,就是冒泡排序的时间复杂度:T[n] = n * (n-1) = n^2 + n;

最好情况下是O(nlog2n),推导过程如下:

递归算法的时间复杂度公式:T[n] = aT[n/b] + f(n) 

所以平均时间复杂度为O(nlog2n)

2.  空间复杂度:

  快速排序使用的空间是O(1)的,也就是个常数级;而真正消耗空间的就是递归调用了,因为每次递归就要保持一些数据:
  最优的情况下空间复杂度为:O(log2n);每一次都平分数组的情况
  最差的情况下空间复杂度为:O( n );退化为冒泡排序的情况
所以平均空间复杂度为O(log2n)

import java.util.Arrays;
public class quick{
    public static void main(String[] args) {
        int[] arr = new int[]{10,6,3,8,33,27,66,9,7,88};
        f(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
    public static void f(int[] arr,int start,int end){
        //直到start=end时结束递归
        if(start<end){
            int left = start;
            int right = end;
            int temp = arr[start];
           
            while(left<right){
               
                //右面的数字大于标准数时,右边的数的位置不变,指针向左移一个位置
                while(left<right && arr[right]>temp){
                    right--;
                }
               
                //右边的数字小于或等于基本数,将右边的数放到左边
                arr[left] = arr[right];
                left++;
                ////左边的数字小于或等于标准数时,左边的数的位置不变,指针向右移一个位置
                while(left<right && arr[left]<=temp){
                    left++;
                }
               
                //左边的数字大于基本数,将左边的数放到右边
                arr[right] = arr[left];
            }
           
            //一趟循环结束,此时left=right,将基数放到这个重合的位置,
            arr[left] = temp;
            System.out.println(Arrays.toString(arr));
            //将数组从left位置分为两半,继续递归下去进行排序
            f(arr,start,left);
            f(arr,left+1,end);
        }
    }
}

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159805.htm