C++ 计数排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HarvestWu/article/details/81159225

 要求排序的关键字互不相同。

#include <bits/stdc++.h>
#define MaxSize 100
#define ArrayLen(array) sizeof(array)/sizeof(array[0])
#define TypeName template<class T>
/*
* Created by HarvestWu on 2018/07/22.
*/

using namespace std;

//计数排序
TypeName
void CountSort(T R[],T A[],int n)
{
	int count;
	for (int i = 0; i < n; i++)
	{
		count = 0;
		for (int j = 0; j < n; j++)
			if (R[i]>R[j])
				++count;
		A[count] = R[i];
	}
}


//打印
TypeName
void Visit(T R[],int n)
{
	for (int i = 0; i < n; i++)
		cout << R[i] << " ";
	cout << endl;
}

int main()
{
	int R[10] = { 1, 8, 115, 6, 7, 12, 4, 83, 9,0 };
	int len = ArrayLen(R);
	int A[10];
	CountSort(R, A, len);
	Visit(A, len);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/HarvestWu/article/details/81159225