Python counting sorting and base sorting algorithms

Source: Internet
Author: User
This article mainly introduces python counting sorting and base Sorting Algorithm examples. For more information, see I. Counting sorting

Counting sort is a stable sorting algorithm.

The algorithm steps are as follows:
Find the largest and smallest elements in the array to be sorted
Counts the number of times each element with the I value appears in the array, and stores the I entry in array C.
Accumulate all counts (starting from the first element in C, each item is added to the previous one)
Backward filling of the target array: place each element I in item C (I) of the new array, and subtract 1 from each element.
When the input element is an integer between n 0 and k, the time complexity of counting sorting is O (N + K), and the space complexity is O (N + K ). When K is not very large, this is a very effective linear sorting algorithm.

The following is the test code:

The Code is as follows:

#-*-Coding: utf8 -*-
Import random

Def jishu (data, max ):
"""
Base sorting: when the input element is an integer between n 0 and k (k cannot be too large, that is, max cannot be too large)
@ Param data: array to be sorted
@ Param max: Maximum Number
"""
Result = [None for I in xrange (len (data)] # The final result
C = [0 for I in range (max + 1)]
# Use array c to count the number of elements with each value = d
For d in data:
C [d] = c [d] + 1

# C [I] indicates the number of elements in the data value <= I
For I in range (1, max + 1 ):
C [I] = c [I] + c [I-1]

# Sorting the elements in C by printing them backwards
For j in xrange (len (data)-1,-1,-1 ):
Result [c [data [j]-1] = data [j]
C [data [j] = c [data [j]-1

Return result

If _ name _ = '_ main __':

# Manufacturing 1000 numbers ranging from 0 to 100

Print jishu ([random. randint (0,100) for I in range (1000)], 100)

Ii. Base sorting

Radix sort is a non-Comparative integer Sorting Algorithm. The principle is to cut an integer into different digits by the number of digits and then compare them by the number of digits.

It is implemented in this way: all the values to be compared (positive integers) are unified into the same digit length, and the number before the shorter digits is zero. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.
The base sorting method can be LSD (Least significant digital) or MSD (Most significant digital). The LSD sorting method starts from the rightmost of the key value, while MSD is the opposite, start from the leftmost of the key value.

The following is a test case:

The Code is as follows:

#-*-Coding: utf8 -*-
Import random
Def jichu (data, length ):
"""
Base ranking lsd
@ Param data: the combination to be arranged
@ Param length: the maximum number of data records.
"""
For l in xrange (length ):
S = [[] for I in xrange (10)]
For d in data:
S [d/(10 ** l) % 10]. append (d)
Data = [d for s_list in s for d in s_list]

Return data

If _ name _ = '_ main __':

List = [random. randint (1, 99999999) for I in xrange (99)] # create 99 pieces of data
Print jichu (list, 8)

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.