1.冒泡排序



2.简单选择排序


3.插入排序


4.希尔排序





5.基数排序




void radixSort(int arr[], int length, int MaxLength) {
// 初始化桶数组
int **buckets = (int **)malloc(sizeof(int*) * 10);
for (int i = 0; i < 10; i++) {
buckets[i] = (int *)malloc(sizeof(int) * length);
memset(buckets[i], 0, sizeof(int) * length);
}
int *bucketCounts = (int*)malloc(sizeof(int) * 10);
// 基数排序主循环
int n = 1;
for (int d = 1; d < MaxLength; d++) {
memset(bucketCounts, 0, sizeof(int) * 10); // 清空计数器
// 分配元素到桶中
for (int i = 0; i < length; i++) {
int digit = (arr[i] / n) % 10;
int pos = bucketCounts[digit]++;
buckets[digit][pos] = arr[i];
}
// 收集桶中元素回原数组
int arrIndex = 0;
for (int k = 0; k < 10; k++) {
for (int h = 0; h < bucketCounts[k]; h++) {
arr[arrIndex++] = buckets[k][h];
}
}
n *= 10; // 处理下一位
}
}

被折叠的 条评论
为什么被折叠?



