Readily programming --- quick sort (QuickSort) -Java achieve

background

Quick sort is a sorting method in the 1960s, by the American Tony Hoare proposed. This sort, was already in a sort of very fast. So in the name, it's called the "quick sort." This algorithm is one of the seven algorithms of the twentieth century, the average case time complexity of Ο (nlogn), and in the case of O (nlogn), the actual computing speed is faster than other same sort of time complexity method.

Tony Hoare and for students interested in the background of quick sort of made, you can see this introduction: http: //www.nowamagic.net/librarys/veda/detail/2391

 

Sort idea

Quick Sort think the idea is very difficult, but it is very easy to understand

His idea is this:

1, first select the queue, one of the Value element as the base (usually selected head element or last element).

2, the base Value sequentially compared in magnitude with all the elements. According to the comparison result of the two elements are divided into queues A, B. All the elements of a base larger than Value, Value smaller than all the elements of a base.

3, as the new queue A, the base is selected again, and then divided into two smaller queue

4, this has been split queue unlimited each small two queues into smaller.

5, until a queue is not to be opened until the split (i.e., an element)

6, because the order between the queues are fixed. The first of these queues combined overall ranking is complete.

(Connection security: from starting herein http://www.cnblogs.com/jilodream/)

Note that there are two core steps, that is,

1, Value element is selected, the whole queue is divided into two sub-queue

2, and then again as a new sub-queue, the overall size is smaller than the current, new queue, calculated until the calculation is very easy so far.

The two key step in creating a fast row natural advantage:

1, by comparing the size of the sub-division of the elements in the queue, the next comparison process, the comparison range for this element always stay in this sub-queue, no longer do the extra comparison. This makes early comparisons still have a great impact on the relatively late. The method is similar to bubble sort, then a lot of relatively early, the role of the latter is very small. This point and the kmp algorithm like, pre-comparison as far as possible maximum utilization.

2, the original size of the queue, split into several small-scale sub-queue, sub-queue to be (security connector: This article first appeared from http://www.cnblogs.com/jilodream/) to solve the problem with the original queue the same, but the scale is smaller. So constantly split to form a divide and conquer thoughts. This idea and backpack algorithm also coincide.

For text to understand the difficulties students may have a look below this online classic motion picture, very vivid

 

java implementation:

import java.util.Arrays;

public class QuickSort
{
    public static void main(String args[])
    {
        QuickSort quicksort = new QuickSort();
        int[] arrays = new int[]
        { 1, 12, 2, 13, 3, 14, 4, 15, 5, 16, 17, 17, 177, 18, 8, 8, 19 };
        quicksort.quickSort(arrays);
        System.out.println(Arrays.toString(arrays));
    }
    
    private void quickSort(int[] arrays)
    {
        subQuickSort(arrays, 0, arrays.length - 1);
    }
    
    private void subQuickSort(int[] arrays, int start, int end)
    {
        if (start >= end)
        {
            return;
        }
        int middleIndex = subQuickSortCore(arrays, start, end);
        subQuickSort(arrays, start, middleIndex - 1);
        subQuickSort(arrays, middleIndex + 1, end);
    }
    
    private int subQuickSortCore(int[] arrays, int start, int end)
    {
        int middleValue = arrays[start];
        while (start < end)
        {
            while (arrays[end] >= middleValue && start < end)
            {
                end--;
            }
            arrays[start] = arrays[end];
            while (arrays[start] <= middleValue && start < end)
            {
                start++;
            }
            arrays[end] = arrays[start];
        }
        arrays[start] = middleValue;
        return start;
    }
}

 

Guess you like

Origin www.cnblogs.com/ziytong/p/12583782.html