代码拉取完成,页面将自动刷新
同步操作将从 Shapper/C Data Structure 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
//
// Radix Sort
// Created by Win10 on 2023/4/8.
//
#ifndef C_DATA_STRUCTURE_RADIX_SORT_H
#define C_DATA_STRUCTURE_RADIX_SORT_H
#include <algorithm>
template<typename T>
// A utility function to get maximum value in arr[]
T getMax(T a[], int len)
{
T mx = a[0];
for (int i = 1; i < len; i++)
if (a[i] > mx)
mx = a[i];
return mx;
}
template<typename T>
// A function to do counting sort of arr[] according to
// the digit represented by exp.
void countSort(T a[], int len, int exp)
{
T output[len]; // output array
int i, count[10] = { 0 };
// Store count of occurrences in count[]
for (i = 0; i < len; i++)
count[(a[i] / exp) % 10]++;
// Change count[i] so that count[i] now contains actual
// position of this digit in output[]
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build the output array
for (i = len - 1; i >= 0; i--) {
output[count[(a[i] / exp) % 10] - 1] = a[i];
count[(a[i] / exp) % 10]--;
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to current digit
for (i = 0; i < len; i++)
a[i] = output[i];
}
template<typename T>
//LSD(Least Significant Digit first)
void radix_sort(T a[], int len) {
// Find the maximum number to know number of digits
int m = getMax(a, len);
// Do counting sort for every digit. Note that instead
// of passing digit number, exp is passed. exp is 10^i
// where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(a, len, exp);
}
#endif //C_DATA_STRUCTURE_RADIX_SORT_H
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。