Back to Home

ការតម្រៀប​បែប​ឫស

ក្បួន​ដោះស្រាយ​ការ​តម្រៀប​មិន​ប្រៀបធៀប ដែល​តម្រៀប​ចំនួន​គត់​ដោយ​ដំណើរការ​ខ្ទង់​នីមួយៗ។

Visualization

Step 1 / 8
Speed:

Details

Analogy

ลองนึกภาพการเรียงลำดับรายการคำตามตัวอักษรโดยไม่ต้องเปรียบเทียบโดยตรง ขั้นแรกคุณจะต้องจัดเรียงคำทั้งหมดลงใน 26 ถังตามตัวอักษรตัวสุดท้าย จากนั้นคุณจะต้องรวบรวมคำเหล่านั้นอีกครั้งแล้วจัดเรียงลงในถังตามตัวอักษรตัวที่สองจากท้ายสุดไปเรื่อยๆ จนกว่าจะถึงตัวอักษรตัวแรก หลังจากผ่านรอบสุดท้ายแล้ว รายการคำทั้งหมดจะถูกจัดเรียงอย่างสมบูรณ์

Purpose

เพื่อจัดเรียงจำนวนเต็มโดยการจัดกลุ่มคีย์ตามตัวเลขแต่ละตัวซึ่งมีตำแหน่งและค่าสำคัญเหมือนกัน มันประมวลผลตัวเลขจากเลขนัยสำคัญน้อยที่สุดไปยังเลขนัยสำคัญมากที่สุด

Use Cases

មាន​ប្រសិទ្ធភាព​ខ្ពស់​សម្រាប់​ការ​តម្រៀប​សំណុំ​លេខ​ពេញ​ធំ​ឬ​សោ​ខ្សែ​អក្សរ។ ដោយសារ​វា​មិន​ផ្អែក​លើ​ការ​ប្រៀបធៀប វា​អាច​លឿន​ជាង​ក្បួន​ដោះស្រាយ​តម្រៀប O(n log n)។

Algorithm Steps

Line 1
1
find max value in arr
2
exp = 1
3
while max / exp > 0:
4
countingSort(arr, exp)
5
exp *= 10
6
7
function countingSort(arr, exp):
8
create output and count arrays
9
count occurrences of digits at exp position
10
convert count to cumulative count
11
build output array from right to left
12
copy output back to arr
Current
Executed
Pending

Explanation

What's Happening?

Step 1/8

អារេដែលមិនបានតម្រៀបដំបូង។

Updates with each step • Clickfor full view

Code Implementation

def radix_sort(arr):
    max_val = max(arr)
    exp = 1
    while max_val // exp > 0:
        counting_sort_for_radix(arr, exp)
        exp *= 10
    return arr

def counting_sort_for_radix(arr, exp):
    n = len(arr)
    output = [0] * n
    count = [0] * 10
    for i in range(n):
        index = arr[i] // exp
        count[index % 10] += 1
    for i in range(1, 10):
        count[i] += count[i - 1]
    i = n - 1
    while i >= 0:
        index = arr[i] // exp
        output[count[index % 10] - 1] = arr[i]
        count[index % 10] -= 1
        i -= 1
    for i in range(n):
        arr[i] = output[i]
ការតម្រៀប​បែប​ឫស - Interactive Visualization | AlgoViz | AlgoViz