-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathrxsort.c
71 lines (52 loc) · 1.48 KB
/
rxsort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// rxsort.c
// Algorithms - Radix sort
//
// Created by YourtionGuo on 11/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "sort.h"
#pragma mark - Public
int rxsort(int *data, int size, int p, int k)
{
int *counts, *temp;
int index, pval, i, j, n;
/// 初始化 counts 空间
if ((counts = (int *)malloc(k * sizeof(int))) == NULL) return -1;
/// 初始化空间用于排序结果
if ((temp = (int *)malloc(size * sizeof(int))) == NULL) return -1;
/// 从低位到高位对数据进行排序
for (n = 0; n < p; n++) {
/// 初始化 counts
for (i = 0; i < k; i++) {
counts[i] = 0;
}
/// 计算当前位数值
pval = (int)pow((double)k, (double)n);
/// 对当前位置进行计数
for (j = 0; j < size; j++) {
index = (int)(data[j] / pval) % k;
counts[index] = counts[index] + 1;
}
/// 调整计数值,加上前一个元素出现次数
for (i = 1; i < k; i++) {
counts[i] = counts[i] + counts[i - 1];
}
/// 使用 counts 将元素放到对应位置
for (j = size - 1; j >= 0; j--) {
index = (int)(data[j] / pval) % k;
temp[counts[index] - 1] = data[j];
counts[index] = counts[index] - 1;
}
/// 将当前已排序数据放回
memcpy(data, temp, size * sizeof(int));
}
/// 销毁用于排序的临时空间
free(counts);
free(temp);
return 0;
}