Table of Contents
Principle example of radix sort algorithm
Python code to implement the radix sorting algorithm
Home Backend Development Python Tutorial An example of using Python to implement the principles of radix sorting algorithm

An example of using Python to implement the principles of radix sorting algorithm

Jan 22, 2024 pm 01:36 PM
Algorithm concept

The radix sorting algorithm is a type of bucket sorting algorithm, which sorts values ​​based on the same position into groups. Maybe it's a bit hard to understand. You can look at the following example of the principle of radix sorting algorithm.

Principle example of radix sort algorithm

Specify the array [121,432,564,23,1,45,788], and sort the array by radix, as shown in the figure:

基数排序算法原理实例 Python实现基数排序算法

#First sort the single-digit values, then sort the tens-digit values, and finally sort the hundreds-digit values, and finally output the sorted The array is [001,023,045,121,432,564,788]

Python code to implement the radix sorting algorithm

def countingSort(array, place):
    size = len(array)
    output = [0] * size
    count = [0] * 10

    for i in range(0, size):
        index = array[i] // place
        count[index % 10] += 1

 
    for i in range(1, 10):
        count[i] += count[i - 1]

    i = size - 1
    while i >= 0:
        index = array[i] // place
        output[count[index % 10] - 1] = array[i]
        count[index % 10] -= 1
        i -= 1

    for i in range(0, size):
        array[i] = output[i]

def radixSort(array):
    # Get maximum element
    max_element = max(array)

    place = 1
    while max_element // place > 0:
        countingSort(array, place)
        place *= 10

data = [121, 432, 564, 23, 1, 45, 788]
radixSort(data)
print(data)
Copy after login

The above is the detailed content of An example of using Python to implement the principles of radix sorting algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An in-depth analysis of the Gray Wolf Optimization Algorithm (GWO) and its strengths and weaknesses An in-depth analysis of the Gray Wolf Optimization Algorithm (GWO) and its strengths and weaknesses Jan 19, 2024 pm 07:48 PM

An in-depth analysis of the Gray Wolf Optimization Algorithm (GWO) and its strengths and weaknesses

Analyze the principles, models and composition of the Sparrow Search Algorithm (SSA) Analyze the principles, models and composition of the Sparrow Search Algorithm (SSA) Jan 19, 2024 pm 10:27 PM

Analyze the principles, models and composition of the Sparrow Search Algorithm (SSA)

Explore the basic principles and implementation process of nested sampling algorithms Explore the basic principles and implementation process of nested sampling algorithms Jan 22, 2024 pm 09:51 PM

Explore the basic principles and implementation process of nested sampling algorithms

What is the role of information gain in the id3 algorithm? What is the role of information gain in the id3 algorithm? Jan 23, 2024 pm 11:27 PM

What is the role of information gain in the id3 algorithm?

Numerical optimization principles and analysis of the Whale Optimization Algorithm (WOA) Numerical optimization principles and analysis of the Whale Optimization Algorithm (WOA) Jan 19, 2024 pm 07:27 PM

Numerical optimization principles and analysis of the Whale Optimization Algorithm (WOA)

Introduction to Wu-Manber algorithm and Python implementation instructions Introduction to Wu-Manber algorithm and Python implementation instructions Jan 23, 2024 pm 07:03 PM

Introduction to Wu-Manber algorithm and Python implementation instructions

Scale Invariant Features (SIFT) algorithm Scale Invariant Features (SIFT) algorithm Jan 22, 2024 pm 05:09 PM

Scale Invariant Features (SIFT) algorithm

Explore the concepts of Bayesian methods and Bayesian networks in depth Explore the concepts of Bayesian methods and Bayesian networks in depth Jan 24, 2024 pm 01:06 PM

Explore the concepts of Bayesian methods and Bayesian networks in depth

See all articles