基于计数排序子程序的基数排序实现
2015-07-24 00:43
323 查看
#include "stdafx.h" #include <iostream> #include <math.h> using namespace std; #define RADIX 3 #define LENGTH 7 int A[LENGTH] = { 329, 457, 657, 839, 436, 720, 355 }; // copy a[] into b[] void CopyArray(int* a, int* b) { for (int i = 0; i < LENGTH; ++i) { b[i] = a[i]; } } void PrintArray(int* a) { for (int i = 0; i < LENGTH; ++i) { cout << a[i] << " "; } cout << endl; } //返回整型a的第i位数 //GetDigitI(790,3) = 7 //GetDigitI(790,2) = 9 //GetDigitI(790, 1) = 0 int GetDigitI(int a, int i) { return (a / (int)pow(10, i-1)) % 10; } //计数排序 void CountingSort(int* a, int i) { int B[LENGTH] = { 0 }; //存本次的排序结果 int C[10] = { 0 }; //用于统计的辅助数组 //统计第i位上的值出现次数 for (int k = 0; k < LENGTH; ++k) { int t = GetDigitI(a[k], i); C[t] += 1; } //累加 for (int k = 1; k < 10; ++k) { C[k] = C[k] + C[k - 1]; } //根据累加数组C,对a进行排序,结果在B中 for (int k = LENGTH-1; k >= 0; --k) { int t = GetDigitI(a[k], i); B[C[t] - 1] = a[k]; C[t] -= 1; } CopyArray(B,a); } //基数排序 void RadixSort() { int* t = A; for (int i = 1; i <= RADIX; ++i) { //Use Counting sort to sort array A on digit i CountingSort(t, i); cout << "Result " << i <<": \n"; PrintArray(A); } } int _tmain(int argc, _TCHAR* argv[]) { PrintArray(A); RadixSort(); return 0; }
相关文章推荐
- CSS Reset方法及它的滥用
- Ubuntu connect rjsupplicant
- codeforces 306 div.2 B. Preparing Olympiad
- slave宕机master重启出现问题解决办法
- zookeeper权威的应用案例
- xliff:g 错误 error: Error parsing XML: unbound prefix
- Java_命名规范
- android listview中checkbox(购物车,批量操作)
- ImageLoader 可以加载的图片的地址类型
- 【快速筛可能是大费马素数的数】
- Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess 定理Lucas求大组合数
- 不规则混排实现(实现图文环绕的效果)
- Delph i2010
- mysql给数据库表里某个字段赋随机值
- [嵌入式Linux驱动]S5PV210的烟雾传感器Linux驱动
- hdu5308 I Wanna Become A 24-Point Master 含 spj
- JavaScript 中的相等检测
- 黑马程序员——学习一维数组的地址
- EularProject 48: 利用数组求和
- C++11新特性