《算法导论》9、三个线性时间排序(C++)

1、计数排序

#include <iostream>
#include <stdlib.h>  
using namespace std;
int* countingSort(int *A, int n, int k)
{
	int *B = new int[n];
	int *C = new int[k];
	for (int i = 0; i < k; i++)
		C[i] = 0;
	for (int i = 0; i < n; i++)
		C[A[i]]++;
	for (int i = 1; i < k; i++)
		C[i] = C[i] + C[i - 1];
	for (int i = n-1; i >=0; i--)
		B[(C[A[i]]--)-1] = A[i];
	delete[] C;
	return B;
}
void main()
{
	int n = 10;  //排序元素长度    
	int* A = new int[n];
	for (int i = 0; i <n; i++)
	{
		A[i] = rand() % 10;
	}
	int *B = countingSort(A, n, 10);
	for (int i = 0; i < n; i++)
	{
		cout << B[i] << "  ";
	}
	cout << endl;
	system("pause");
}

2、基数排序

#include <iostream>  
#include <stdlib.h>  
#include <time.h> 
using namespace std;
void countingSort(int *A,int *bit, int n, int k)
{
	int *B = new int[n];
	int *C = new int[k];
	for (int i = 0; i < k; i++)
		C[i] = 0;
	for (int i = 0; i < n; i++)
		C[bit[i]]++;
	for (int i = 1; i < k; i++)
		C[i] = C[i] + C[i - 1];
	for (int i = n - 1; i >= 0; i--)
		B[(C[bit[i]]--) - 1] = A[i];
	for (int i = 0; i < n; i++)
		A[i] = B[i];
	delete[] C;
	delete[] B;
}
void radixSort(int *A, int n)
{
	bool allzero = false;
	int radixbit = 1;
	int *radixdata = new int[n];
	while (allzero == false)
	{	
		allzero = true;
		radixbit *= 10;
		for (int i = 0; i < n; i++)
		{
			radixdata[i] = A[i] % radixbit;
			radixdata[i] /= (radixbit / 10);
			if (radixdata[i]>0)
				allzero = false;
		}
		countingSort(A, radixdata, n, 10);
	}
	delete[] radixdata;
}
void main()
{
	int n = 100;  //排序元素长度      
	int* A = new int[n];
	for (int i = 0; i <n; i++)
	{
		A[i] = rand() % 100;
	}
	clock_t start, finish;
	double totaltime;
	start = clock();
	radixSort(A, n);
	finish = clock();
	totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
	cout << "排序的运行时间为" << totaltime << "秒!" << endl;
	system("pause");
	delete[] A;
}

3、桶排序

#include<iostream>
using namespace std;
int a[] = { 1, 255, 8, 6, 25, 47, 14, 35, 58, 75, 96, 158, 657 };
const int len = sizeof(a) / sizeof(int);
int b[10][len + 1] = { 0 };//将b全部置0
void bucketSort(int a[]);//桶排序函数
void distributeElments(int a[], int b[10][len + 1], int digits);
void collectElments(int a[], int b[10][len + 1]);
int numOfDigits(int a[]);
void zeroBucket(int b[10][len + 1]);//将b数组中的全部元素置0
int main()
{
	cout << "原始数组:";
	for (int i = 0; i<len; i++)
		cout << a[i] << ",";
	cout << endl;
	bucketSort(a);
	cout << "排序后数组:";
	for (int i = 0; i<len; i++)
		cout << a[i] << ",";
	cout << endl;
	system("pause");
	return 0;
}
void bucketSort(int a[])
{
	int digits = numOfDigits(a);
	for (int i = 1; i <= digits; i++)
	{
		distributeElments(a, b, i);
		collectElments(a, b);
		if (i != digits)
			zeroBucket(b);
	}
}
int numOfDigits(int a[])
{
	int largest = 0;
	for (int i = 0; i<len; i++)//获取最大值
		if (a[i]>largest)
			largest = a[i];
	int digits = 0;//digits为最大值的位数
	while (largest)
	{
		digits++;
		largest /= 10;
	}
	return digits;
}
void distributeElments(int a[], int b[10][len + 1], int digits)
{
	int divisor = 10;//除数
	for (int i = 1; i<digits; i++)
		divisor *= 10;
	for (int j = 0; j<len; j++)
	{
		int numOfDigist = (a[j] % divisor - a[j] % (divisor / 10)) / (divisor / 10);
		//numOfDigits为相应的(divisor/10)位的值,如当divisor=10时,求的是个位数
		int num = ++b[numOfDigist][0];//用b中第一列的元素来储存每行中元素的个数
		b[numOfDigist][num] = a[j];
	}
}
void collectElments(int a[], int b[10][len + 1])
{
	int k = 0;
	for (int i = 0; i<10; i++)
		for (int j = 1; j <= b[i][0]; j++)
			a[k++] = b[i][j];
}
void zeroBucket(int b[][len + 1])
{
	for (int i = 0; i<10; i++)
		for (int j = 0; j<len + 1; j++)
			b[i][j] = 0;
}

 以上这种算法,虽然称之为桶排序,但是其实际思想是先按位做10基数排序,在基数排序内部,使用桶排序确定各个元素的位置。而且,这种排序方式并没有使用链表,而是使用了一个b[10][len+1]的辅助数组,和b[10][0]这个计数变量,指示每一行有多少个元素。这个程序设计的非常巧妙。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值