Algorithm (five) quick sort

Sort principle

Quicksort uses a divide and conquer strategy, also known as divide and conquer. The principle is broken down into a big problem with the original problem the same small problem, recursive solving small problems, the solution of the problem of the integration solution grew up in a small problem. The main idea of quick sort is randomly selected from a scalar place it has row position good order it should be located on what position the scalar should be located it, that is to the left of the scalar whole is less than the standard amount, the right of the scalar All scalar greater than, respectively, and then both sides of the scalar processing logic array above, this logic recursively until all the elements so far ordered.
The specific process: start with the first step of a random array of scalar and rearwardly from the head while traversing the array, and traversing forwardly from the tail, until the two meet up only the needle, the traversal, the traversal rearwardly from the head If you encounter elements of the scalar magnitude of the ratio continues to traverse back and do nothing, if you encounter a large amount of elements than the standard stop, and the recording element, while the side from the tail forward traversal, if you encounter a large amount of elements than the standard do nothing and continue to traverse forward, if you encounter if they are smaller than the scalar element is stopped, this time only two needle exchange position within the meaning of the elements, when the two met only after the needle traversing stop, index position and where the scalars to be recorded in the index based on the search array is divided into two smaller problems, repeating the above steps, known sort is completed.

Here Insert Picture Description

The first time complexity

The time complexity of quick sort of divided into two cases

  • If the scalar is randomly selected and the maximum or minimum value in the array, the time complexity of O (n2). Because when the first cycle, in this case, the divided n-1 times and the number of elements in the first array is divided than the front unsorted just one less element, the i-th divided length is actually n- i + 1, the number of comparisons required to ni times, so the number of comparison reaches the maximum value n * (n-1) / 2 = o (n2).
  • If the randomly selected scalar array of values, the time complexity is O (nlog2n).

Sort Code

package com.alg.sort;

import org.junit.Test;

import java.util.Random;

public class QuickSort {
    public void quickSort2(int[] a, int l, int r) {
        //递归终止判断,当标量已经是数组最后一位了,就该终止程序
        if (l >= r) {
            return;
        }
        int p = partition2(a, l, r);
        quickSort2(a, l, p);
        quickSort2(a, p + 1, r);
    }

    /**
     * 数组中重复元素过多导致快速排序的时间复杂度为O(n^2),
     * 优化方式为:选定一个标量,从数组的两头开始遍历分别遇到比变量大的或者比标量小的停止
     * 并交换两个停止位置元素的位置。
     */
    public int partition2(int[] arr, int l, int r) {
        int t = arr[l];
        //优化1:随机抽取数组中的元素作为标量,优化近乎有序的数组的快速排序
        Random random = new Random();
        int randomIndex = random.nextInt(r) % (r - l + 1) + l;
        arr[l] = arr[randomIndex];
        arr[randomIndex] = t;

        int v = arr[l];
        
        int i = l + 1;
        int j = r;

        while (true) {
            //从左往右遍历,该循环停止了,那么就遇到大于等于标量的元素了,边界的i要小于等于r,因为r是数组遍历技术的位置。
            while (i <= r && arr[i] < v) {
                i++;
            }
            //从右往左遍历,该循环停止了就遇到了小于等于标量的元素了,边界j要大于等l,不能比L小,因为L是数组排序开始的位置
            while (j >= l && arr[j] > v) {
                j--;
            }
            //当i和j相遇了,则没有元素需要遍历了
            if (i > j) {
                break;
            }
            //将大于等于标量和小于等于标量的元素进行位置交换,这样重复的元素就别分散到了元素的两边
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        return j;
    }

Published 94 original articles · won praise 55 · views 110 000 +

Guess you like

Origin blog.csdn.net/Suubyy/article/details/100106199