c++之计数排序
#include<iostream>using namespace std;void CountSort(int* data, int length){int max = 0;for (int i = 0; i < length; i++){if (max < data[i])max = data[i];}int *CountD...
·
#include<iostream>
using namespace std;
void CountSort(int* data, int length)
{
int max = 0;
for (int i = 0; i < length; i++)
{
if (max < data[i])
max = data[i];
}
int *CountData = new int[max + 1];//该指针使用类似于数组。
/*int *a,i; 定义一个指针,i表示数组个数
a = new int[i];//用new先创建他的空间,a指向这个数组
delete a;//删除*/
for (int i = 0; i <= max; ++i)
CountData[i] = 0;
for (int i = 0; i < length;i++)
{
CountData[data[i]]++;
}
int index = 0;
for (int i = 0; i < max + 1; i++)
{
for (int j = 0; j < CountData[i]; j++)
{
data[index++] = i;
}
}
delete CountData;
}
int main()
{
int v[6] = { 23, 5, 12, 14 ,3,78};
CountSort(v, 6);
for (auto i:v)
{
cout << i << ' ';
}
cout << endl;
system("pause");
return 0;
}
更多推荐
已为社区贡献1条内容
所有评论(0)