在本教程中,您将学习基数排序的工作方式。此外,您还将找到C语言的示例。
基数排序是一种排序技术,它首先将相同位置的各个数字分组,从而对元素进行排序。然后,根据元素的递增/递减顺序对元素进行排序。
假设我们有一个由8个元素组成的数组。首先,我们将根据个位的值对元素进行排序。然后,我们将根据十位的值对元素进行排序。这个过程一直持续到最后一个有效的数位。
初始数组为 [121、432、564、23、1、45、788] 。按基数排序,如下图所示。
在阅读这篇文章之前,请先看一下计数排序,因为计数排序在基数排序中用作中间排序。
基数排序如何工作?
- 找出数组中最大的元素,即max。设X为max中的位数。计算X是因为我们必须遍历所有元素的所有有效位。
在数组 [121,432,564,23,1,45,788] 中,我们有最大的数字788。它有3个数字。因此,循环应该执行到百位(3次)。 - 现在,逐一检查每个有效数位。
可以使用任何稳定的排序技术对每个有效位置的数字进行排序。我们用了计数排序法。
根据个位数字对元素排序。
- 现在,根据十位数字对元素进行排序。
- 最后,根据百位数字对元素进行排序。
基数排序算法
radixSort(array)
d <- maximum number of digits in the largest element
create d buckets of size 0-9
for i <- 0 to d
sort the elements according to ith place digits using countingSort
countingSort(array, d)
max <- find largest element among dth place elements
initialize count array with all zeros
for j <- 0 to size
find the total count of each unique digit in dth place of elements and
store the count at jth index in count array
for i <- 1 to max
find the cumulative sum and store it in count array itself
for j <- size down to 1
restore the elements to array
decrease count of each element restored by 1
C示例
// Radix Sort in C Programming
#include <stdio.h>
// Function to get the largest element from an array
int getMax(int array[], int n) {
int max = array[0];
for (int i = 1; i < n; i++)
if (array[i] > max)
max = array[i];
return max;
}
// Using counting sort to sort the elements in the basis of significant places
void countingSort(int array[], int size, int place) {
int output[size + 1];
int max = (array[0] / place) % 10;
for (int i = 1; i < size; i++) {
if (((array[i] / place) % 10) > max)
max = array[i];
}
int count[max + 1];
for (int i = 0; i < max; ++i)
count[i] = 0;
// Calculate count of elements
for (int i = 0; i < size; i++)
count[(array[i] / place) % 10]++;
// Calculate cummulative count
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Place the elements in sorted order
for (int i = size - 1; i >= 0; i--) {
output[count[(array[i] / place) % 10] - 1] = array[i];
count[(array[i] / place) % 10]--;
}
for (int i = 0; i < size; i++)
array[i] = output[i];
}
// Main function to implement radix sort
void radixsort(int array[], int size) {
// Get maximum element
int max = getMax(array, size);
// Apply counting sort to sort elements based on place value.
for (int place = 1; max / place > 0; place *= 10)
countingSort(array, size, place);
}
// Print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
// Driver code
int main() {
int array[] = {
121, 432, 564, 23, 1, 45, 788};
int n = sizeof(array) / sizeof(array[0]);
radixsort(array, n);
printArray(array, n);
}
复杂度
由于基数排序是一种非比较排序算法,因此它比比较排序算法有优势。
对于使用计数排序作为中间稳定排序的基数排序,时间复杂度为O(d(n+k))。
这里,d是循环次数,O(n+k)是计数排序的时间复杂度。
因此,基数排序具有线性时间复杂度,优于比较排序算法的O(nlog n)。
如果我们采用非常大的数字或其他基数的数字,如32位或64位数字,虽然它可以在线性时间内执行,但是中间排序占用很大的空间。
这使得基数排序空间效率低下。这就是为什么在软件库中不使用这种方法的原因。
基数排序应用
基数排序应用在
- DC3算法创建后缀数组时。
- 有大范围数字的地方。
参考文档
[1]Parewa Labs Pvt. Ltd.Radix Sort Algorithm[EB/OL].https://www.programiz.com/dsa/radix-sort,2020-01-01.