Chat sorting algorithm that is linear time complexity

Indeed, based on the comparison and exchange sorting algorithm, which limit the time complexity is O (nlog 2 n- ). Bubble sort, insertion sort, etc. Needless to say, the time complexity is O (n- 2 ), even as strong as quick sort, heap sort, also reached only O (nlog 2 n- complexity) of. Then those who can break through the legendary O (nlog 2 the n- ) limit, to linear time complexity O (n) sorting algorithm in the end what is it, let's take a closer look.

Bucket sort

The basic idea

Sentence is, each element will be sorted by column-configured mapping function assigned to a limited number of buckets, each bucket and then sorting the elements.
The basic steps are as follows:

  1. A limited number of empty bucket ready
  2. Traversing the column to be sorted, each assigned to a corresponding element in the tub by the mapping function
  3. Each bucket is not empty sort
  4. From each bucket is not empty then followed by the elements back into the original sequence

Bucket sort function mapping is done using division element, the step of comparing the exchange is omitted, then a small amount of data is performed in the tub, the sort herein may select any sorting algorithm according to the actual needs, such as using a fast sort. Note that the map selection function must ensure that each tub is ordered, i.e., all elements of a bucket must be greater or less than all of the elements of another bucket. In this way in order to put back the original sequence from each element in the bucket, to ensure orderly elements.

Complexity and stability of the advantages and disadvantages

  • Space complexity: O (m + n), m represents a number of buckets, n is expressed as the required length of the auxiliary space n

  • Time complexity: O (n)
    bucket sort time-consuming two main parts:

    1. All elements will be sorted column to the bucket, the time complexity of O (n)
    2. And sorting each barrel element, because the algorithm is based on the comparison, the average time complexity can only reach O (N I log 2 N I ), N I is the number of elements in each tub

    For columns N to be sorted elements, M buckets, each bucket average N / M elements, which bucket sort average time complexity can be expressed as follows:
    O (n-) + O (M * (N / M) log 2 (N / M) ), when M == N, the complexity is O (n)

  • Preferably cases: O (n), when the element column to be sorted is evenly distributed into the tub, is a linear time O (n). Each barrel fewer data used to sort the less time, but the corresponding space consumption will increase. When each bucket only one element, i.e., M == N, where the best ordering of the tub, reached a real O (n)

  • Worst case: all the elements are assigned to the same bucket, bucket sort degenerates into an ordinary sort.

  • Stability: Stable

  • Pros: stable, breaking the lower limit based on a comparison of the sort

  • Disadvantages: the need for additional support space needs good mapping function

Algorithm

public void BucketSort(int[] array){
    int max = array[0], min = array[0];
    for(int i = 1; i < array.Length; i ++){
        if(array[i] > max) max = array[i];
        if(array[i] < min) min = array[i];
    }
    List<int>[] buckets = new List<int>[Fun(max, min, array.Length) + 1];
    for(int i = 0; i < buckets.Length; i ++){
        buckets[i] = new List<int>();
    }
    for(int i = 0; i < array.Length; i ++){
        buckets[Fun(array[i], min, array.Length)].Add(array[i]);
    }
    int index = 0;
    for(int i = 0; i < buckets.Length; i ++){
        // 桶内的排序借助了Sort方法,也可以使用其他排序方法
        buckets[i].Sort();
        foreach(int item in buckets[i]){
            array[index ++] = item;
        }
    }
}
// 映射函数,可以根据实际需求选择不同的映射函数
public int Fun(int value, int minValue, int length){
    return (value - minValue) / length;
}

[Interpretation] algorithm

Firstly, to determine the mapping function Fun, the return value of the function is the element corresponding to subscript tub. And then find the maximum and minimum values to be sorted column, and determining the required number of buckets bucket sort by maximum and minimum. Traversing all the elements to be sorted by column mapping function and Funassign them to the corresponding subscripts tub. And then sequentially to sort all the elements for each bucket, is used herein to provide C # Sort method (selection of a different sort method). When the elements of a barrel of sorted and then put them back into the original sequence.

[For] chestnuts

For a column to be sorted 4, 7, 1, 16, may be used at FIG. 6 showing its bucket sort process:

Counting Sort

The basic idea

Counting sort can be considered a special bucket sort of realize, if you understand the words of bucket sort, counting sort is relatively very simple.
Counting sequencing requirements of all elements to be sorted column is in the range [0, max] a positive integer (of course after deformation can be negative, such as by adding a value, so that all elements become positive)
The basic steps are as follows:

  1. Obtain the maximum value of the column to be sorted, construct (maximum value + 1) count array C, can be considered (maximum value + 1) th barrel, but the barrel is no longer stored in the elements, but each element appears frequency
  2. Traversing the column to be sorted, count the number of each element appearing in the count array. An element i occurs, places count is incremented, i.e., C [i] is the element position index ++
  3. Traversal count array, if the C [i]> 0, it indicates that there is an element i, i sequentially assigned to the elements present in the original sequence

Complexity and stability of the advantages and disadvantages

  • Space complexity: O (k)
    explain here, for counting the sort of space complexity, many online articles are marked is O (n + k), in fact, this sort and count specific algorithm related to some algorithm Add the final sequence counting sequencing obtained to the original sequence an auxiliary sequence, without modifying the original sequence, so that the space complexity is O (n + k), the algorithm described herein without the use of helper sequences, modified directly, so the space complexity is O (k). The algorithm provides some articles no matter how directly labeled O (n + k) space complexity is obviously very easy to mislead readers.
  • Time complexity: O (n + k), k represents the constant value of the largest element in the column to be sorted
  • Preferably where: O (n + k)
  • Worst case: O (n + k)
  • Stability: Stable
  • Benefits: stable and suitable for a maximum of a sequence of integers is not very big, breaking the lower limit is small compared sort algorithm based on the value of k
  • Disadvantages: there is a prerequisite, k value is large, it requires a lot of extra space

Algorithm

public void CountSort(int[] array){
    int max = array[0];
    for(int i = 0; i < array.Length; i ++){
        if(array[i] > max) max = array[i];
    }
    int[] count = new int[max + 1];
    for(int i = 0; i < array.Length; i ++){
        count[array[i]] ++;
    }
    int index = 0;
    for(int i = 0; i < count.Length; i ++){
        while(count[i] -- > 0){
            array[index ++] = i;
        }
    }
}

[Interpretation] algorithm

Firstly, the maximum value is obtained can be seen that the column elements to be sorted and counted to build an array (maximum value +1) length. Each element of the column to be sorted traversal, and by element count array subscript is the number of occurrences of the recording elements. Then traverse count array, if the position corresponding to the target value is greater than 0 (equal to a few expressed on several elements), then there is a value of the element and the size of the subject. The elements are added to the original sequence. Since the objective is to small to large, the corresponding sequences are also obtained in ascending order.

[For] chestnuts

For a column to be sorted 1, 2, 3, 4, FIG. 5 showing the procedure may be used to sort its count:

Radix Sort

The basic idea

Radix sort is not to compare, but to achieve sorted by "distribution" and "collect" the two processes.
First, the establishment of r queues are numbered columns 0 ~ r-1, r radix column elements to be sorted (e.g. decimal, then r = 10), then follow the allocation rule collection elements:

  1. The value of the least significant bit of the press, the n-th element of r is assigned to said queue, and then small to general elements in the queue sequentially collected
  2. Then the value of second least significant bits of the newly collected elements of r are allocated to the queue, then collected
  3. Repeatedly performing the above-described distribution and collection, until the most significant bit. (That is, if the number of bits is d, d times need to be repeated, the number of bits d of a measurement of the maximum element of all elements, such as if the maximum number is 963, then d = 3)

Why you can complete ordering it?
In small to large, for example, when the first sequence in accordance with the least significant of allocation and deallocation, bits obtained at this time, the element is based on the lowest significant bits in ascending order of value.
When the distributing and collecting a second significant bit in accordance with the times, the obtained sequence is in ascending order according to the next lower significant bit element value, and then in ascending order according to the value of the least significant bit.
So, when the most significant bit in accordance with the final time distribution and collection, the resulting sequence is in ascending order according to the value of the most significant bit of the elements, again arranged according to the next most significant bits. . . , Again according to the second lowest significant bit, and then based on the least significant bit. Naturally completed in ascending order of each element.

Complexity and stability of the advantages and disadvantages

  • Space complexity: O (n + r), r represents a base element, such as a decimal element, r = 10
  • Time complexity: O (d (n + r))
  • Best case: O (d (n + r))
  • Worst case: O (d (n + r))
  • Stability: Stable
  • Pros: stable, time complexity can break through the lower limit based on a comparison of the sort of
  • Disadvantages: the need for additional auxiliary space

Algorithm

public void RadixSort(int[] array){

    int max = GetMaxValue(array);

    int[] buckets = new int[10];
    int[] buffer = new int[array.Length];

    for(int i = 1; max / i > 0; i = i * 10){
        // 统计每个桶中的元素个数
        for(int j = 0; j < array.Length; j ++){
            buckets[array[j] / i % 10] ++;
        }
        // 使每个桶记录的数表示在buffer数组中的位置
        for(int j = 1; j < buckets.Length; j ++){
            buckets[j] += buckets[j - 1];
        }
        // 收集,将桶中的数据收集到buffer数组中
        for(int j = array.Length - 1; j >= 0; j --){
            buffer[-- buckets[array[j] / i % 10]] = array[j];
        }
        for(int j = 0; j < array.Length; j ++){
            array[j] = buffer[j];
        }
        // 清空桶
        for(int j = 0; j < buckets.Length; j ++){
            buckets[j] = 0;
        }
    }
}

// 获得待排序列中的最大元素
public int GetMaxValue(int[] array){
    int max = array[0];
    for(int i = 1; i < array.Length; i ++){
        if(array[i] > max){
            max = array[i];
        }
    }
    return max;
}

[Interpretation] algorithm

Is a decimal number for the algorithm, so r = 10
first acquires to be sorted to the largest element in the column, and then collected and d times the number of bits allocated d largest element
distribution and collection during each pass follows:
the given the median value, is calculated for each element to be sorted on the column in position, if an element without this bit is represented by 0. Then using a length of the bucket array 10 record the number of times they appear (here, the tub is not used directly record corresponding element). Then traversed again bucket array buckets[j] += buckets[j - 1];, so that the value recorded in each bucket, is represented on the specified number of bits is equal to the bucket index of the array element in the auxiliary bufferposition.
Collection and then, based on the position information recorded in the bucket array sequentially collected to corresponding element bufferin the array. Finally empty the bucket, preparing for the next assignment to collect once.

[For] chestnuts

For a column to be sorted 9, 40, 123, 56, 7, 17 may be used at FIG its radix sorting procedure represents:

More

The above algorithm source code are placed on GitHub, interested students can click here to see the
summary and more code algorithm implementation (not just the sorting algorithm), you can view the GitHub repository Algorithm

Guess you like

Origin www.cnblogs.com/iwiniwin/p/12633644.html