sorting algorithm (i)--quantifying the order of arrays

Source: Internet
Author: User

1. Basic Order

Among the many sorting algorithms, one concept has been mentioned several times: arrays are basically ordered.

For example:

    • The direct insert sort (insertion sort) shows good performance when the array is basically ordered.
    • Smooth sorting (Smooth sort) tends to have a time complexity of O (n) when the array tends to be orderly.
    • Fast sorting (quick sort) and heap sorting (heap sort) do not increase the speed at which they are sorted when confronted with a basic ordered array.

It is this nature that makes the direct insertion sort, compared to the time complexity of O (n^2) bubble sort (Bubble sort), which has more usage scenarios:

    • Hill sort (Shell sort) optimizes the basis for direct insertion sorting.
    • Merge sort merges a small-scale array with a direct insert sort to optimize efficiency.

So here's the problem:

    • How to evaluate the word "basic order"?
    • If there are two arrays now, which one is relatively more orderly?
    • Is there a way to quantify this trait?

2. Reverse pairs (inversion)

Https://en.wikipedia.org/wiki/Inversion_ (Discrete_mathematics)

Mathematically, there is a property called "reverse pair", which can be used to quantify the order of an array, as defined below:

For array A, if there are positive integers i and j, and I < J, there is a[i] > a[j].

So the numbers on <i, j> or <a[i], a[j]> can be called an inverse pair of array A.

For example:

For Arrays a = {4, 2, 3},<4, 2> and <4, 3> is the inverse pair of this array, and the number of reverse pairs for array A is 2.

For Arrays B = {4, 3, 2},<4, 2>,<4, 3>, and <3, 2> is the inverse pair of this array, and array B has an inverse number of 3.

So we can say that array A is more orderly than array B.

To calculate the code for reverse pairs:

    public static int calculateinversion (int[] array) {        int counter = 0;        for (int i = 0, i < array.length-1; ++i) {for            (int j = i + 1; j < Array.Length; ++j) {                if (Array[i] > Array[j]) {                    counter++;                }            }        }        return counter;    }

3. Optimization reason for hill sort and comb sort (comb sort)

Bubble sorting and direct insertion sorting are among the least efficient of the many algorithms that have a time complexity of O (n^2).

The root cause of this is that each exchange/shift in the sequencing process can only reduce the number of reverse pairs of the original array by 1.

For example:

Array A = {1, 3, 4, 2}, and the direct insertion sort for element 2.

    • Before sorting: A = {1, 3, 4, 2},inversion = 2.
    • During the sorting process, the entire array was shifted 2 times.
    • After sorting: A = {1, 2, 3, 4},inversion = 0.

And in the hill sort, each shift operation, will bring more in reverse order to reduce the number.

For example, using the example previously introduced in Hill sort: http://www.cnblogs.com/jing-an-feng-shao/p/6169690.html

Array A = {89, 45, 54, 29, 90, 34, 68}, initial increment gap = 3.

    • Before ordering: A = {},inversion, A, Si, Wu, Wu, Wu, and 10.
    • During the sorting process, the entire array was shifted 2 times.
    • After sorting: A = {$, a, 4, a.

So, in this case, Hill sort uses 2 shift operations, eliminating 6 pairs of reverse order, thus improving the overall algorithm efficiency.

The concept of gap is also introduced in comb sorting, which increases the efficiency of the bubble sorting algorithm.

sorting algorithm (i)--quantifying the order of arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.