Python implements counting sorting

Python implements counting sorting

1. Introduction to counting and sorting

Counting Sort is a sorting algorithm that does not compare the size of the data, and is a sorting algorithm that sacrifices space for time.

Counting sorting is suitable for sorting data with a large amount of data and a small data range, such as sorting people's ages, sorting test scores, and so on.

Counting sorting first finds the maximum value k in the list to be sorted, and opens up a count list of length k+1, and all initial values ​​in the count list are 0. Visiting the list to be sorted, if the value of the visited element is i, the value of index i in the counting list is increased by 1, and visiting the complete list to be sorted, you can count the number of each value in the list to be sorted. Then create a new list, and add the corresponding number of i to the new list according to the counted number in the count list to get a sorted list.

Second, the principle of counting and sorting

The principle of counting and sorting is as follows:

1. Find the maximum value k in the list to be sorted, and create a count list with a length of k+1. The values ​​in the count list are all 0.

2. Visit the list to be sorted. If the value of the visited element is i, the value of index i in the count list is increased by 1.

3. Visit the complete list to be sorted, the value j of index i in the counting list indicates that the number of i is j, and count the number of each value in the list to be sorted.

4. Create a new list, traverse the count list, and sequentially add j i to the new list. The new list is the sorted list. The entire process does not compare the size of the data in the list to be sorted.

Take the list [5, 7, 3, 7, 2, 3, 2, 5, 9, 5, 7, 6] in ascending order as an example. The initial state of the list is shown in the figure below.

1. The maximum value in the list to be sorted is 9, so open a count list with a length of 10, the index is 0-9, and the value is all 0.

2. Visit the list to be sorted. If the value of the visited element is i, the value of index i in the count list is increased by 1. The value of the first element is 5, and the value of index 5 in the count list is increased by 1, from 0 to 1.

3. The value of the second element is 7, and the value of index 7 in the count list is increased by 1, from 0 to 1.

4. The value of the third element is 3, and the value of index 3 in the count list is increased by 1, from 0 to 1.

5. The value of the fourth element is 7, and the value of index 7 in the count list is increased by 1, from 1 to 2.

6. Repeatedly visit the complete list to be sorted, the number of all elements is counted in the count list.

7. Create a new list, traverse the count list, and add the corresponding number of elements to the new list in turn. Both 0 and 1 are 0 and do not need to be added. There are two 2s. Add two 2s to the new list. After adding, subtract the corresponding amount from the count list.

8. There are two 3, and continue to add two 3s to the new list. After adding, subtract the corresponding amount from the count list.

9. Traverse the entire count list, after adding all the data, the new list is the list after sorting. The sorting result is as shown in the figure below.

Three, Python implements counting and sorting

# coding=utf-8
def counting_sort(array):
    if len(array) < 2:
        return array
    max_num = max(array)
    count = [0] * (max_num + 1)
    for num in array:
        count[num] += 1
    new_array = list()
    for i in range(len(count)):
        for j in range(count[i]):
            new_array.append(i)
    return new_array


if __name__ == '__main__':
    array = [5, 7, 3, 7, 2, 3, 2, 5, 9, 5, 7, 6]
    print(counting_sort(array))

operation result:

[2, 2, 3, 3, 5, 5, 5, 6, 7, 7, 7, 9]

In the code, use the Python built-in function max() to find the maximum value in the list to be sorted. Then count according to the sorting principle analyzed above, and then add the data to the new list. i represents the index of the counting list, and also represents the element of the value i in the list to be sorted, and j represents that there are j elements of the value i.

Fourth, the time complexity and stability of counting and sorting

1. Time complexity

In counting sorting, you need to visit each element in the list to be sorted and count. The length of the list is n, and then you need to traverse the count list and add data to the new list. The length of the count list is k+1, and the time complexity is T (n)=n+k+1, and then multiply the number of steps for counting and adding data (constant, does not affect the big O notation), so the time complexity of counting sorting is O(n+k).

2. Stability

According to the application scenario of counting sorting, there are many elements with equal values ​​in the list to be sorted. However, counting sorting does not compare the size of the data in the list to be sorted, nor does it perform position exchange, and the relative order of equal data remains unchanged. So counting sorting is a stable sorting algorithm.

 

 

Guess you like

Origin blog.csdn.net/weixin_43790276/article/details/107398262