Sort [US] data structures and algorithms of optimization: how to achieve a common, high-performance sorting function?

table of Contents

First, how to choose the right sort algorithm?

1. List sorting algorithm

2. Why choose Quick Sort?

Second, how to optimize the quick sort?

Third, the general sorting function implementation skills

Fourth, after-school thinking


First, how to choose the right sort algorithm?

1. List sorting algorithm

 The time complexity is stable sort? Place is sort?
Bubble sort O (n ^ 2) Yes
insertion sort O (n ^ 2) is a
select sort O (n ^ 2) No Yes
quicksort O (nlogn) No Yes 
merge sort O (nlogn) whether the
bucket sort O ( n) is
counting sequencing O (n + k), k is whether the data range
radix sort O (dn), d is the latitude whether

2. Why choose Quick Sort?

1) Sort linear low time complexity but using special scenarios, if a write generic sorting function can not be selected linear sequencing.
2) In order to take into account the size of the data of any sort, usually preferred time complexity of O (nlogn) sorting algorithms to achieve the sort function.
3) as compared with O (nlogn) row of fast and merge sort, merge sort is not the place sorting algorithm, so the best choice is fast row.

Second, how to optimize the quick sort?

Quick drain leading to reduced time complexity reasons O (n) is unreasonable partition point selection, optimal partition point is: two partitions separated by a partition point, a similar number of data. How to optimize select the partition point? There are two commonly used methods, as follows:
1. taking three digital France
① interval from the first, middle, and end, respectively, to take a number, and size comparison, an intermediate value as the partition point.
② If you want to sort an array of relatively large, and that "three take the number" may not be enough, you may want to "take the number 5" or "take the number 10 in."
2. randomly: a section to be sorted from each randomly selected element as a partition point.
3. The quick drain alert recursion stack overflow occurs, there are two solutions as follows:
① recursion depth limit, once the recursion exceeds the threshold set recursion stops.
② heap a function call stack analog implementation, analog recursive manual push, pop process, so there is no size limitation of the system stack.

Third, the general sorting function implementation skills

1. When the amount of data can be taken with ideas for space time
2. When the amount of data, point to optimize fast row select partition
3. prevents stack overflow can be selected manually resolved stack simulated call stack
4. Sort interval, when the constant is smaller than a certain number of elements, consider using O (n ^ 2) insertion sort level
5. sentinel simplify the code, each sort were reduced once determined, as far as possible to maximize the performance optimization

Fourth, after-school thinking

1.Java in what sort sorting algorithm functions are implemented using? What have skills?

In Java:
1. For primitive types of arrays, Java uses a double pivot quicksort (Dual-Pivot Quicksort), the algorithm is introduced in Java 7. Prior to this, Java uses a conventional quick sort, quicksort is optimized double pivot ordinary quicksort, code that implements the new algorithm is located in java.util.DualPivotQuicksort class.
2. For the object type, the algorithm uses a Java TimSort, TimSort algorithm is introduced in Java 7. Prior to this, Java uses a merge sort. TimSort optimization algorithm is actually a series of merge sort, TimSort implementation code located in java.util.TimSort class.
3. In the sorting algorithm, if the length of the array is relatively small, they also use more efficient insertion sort.

default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }

//在Arrays类中,sort()有一系列的重载方法,罗列几个典型的Arrays.sort()方法如下:
public static void sort(int[] a) {
     DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
 }

public static void sort(long[] a) {
     DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}

public static void sort(Object[] a) {
        if (LegacyMergeSort.userRequested)
            legacyMergeSort(a);
        else
            ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
}

 

Published 228 original articles · won praise 90 · views 810 000 +

Guess you like

Origin blog.csdn.net/guochunyang/article/details/104682324