1.计数排序
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
#include <time.h>
using namespace std;
int* countSort(int a[],int n,int k)//n means the length of a,k means the max of a[i]+1
{
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];
c[a[i]] --;
}
return b;
}
int main()
{
char s;
do{
int N = 10;
int *a = new int[N];
//int a[10]= {1,4,5,6,4,2,7,4,8,4};
//int a[10] = { 8 , 8 , 9 , 5 , 9 , 9, 5, 4, 3, 2};
srand(time(0));
for(int i=0;i<N;i++)
{
a[i] = rand()%N;
printf("%5d",a[i] );
}
cout<<endl;
long long begin = clock();
int* b = countSort(a,N,N);
long long end = clock();
cout<<"time:"<<(end-begin)/(float)CLOCKS_PER_SEC*1000<<"ms"<<endl;
for(int i=0;i<N;i++)
printf("%5d",b[i] );
cout<<endl;
cin>>s;
}while(s == 'y' || s == 'Y');
return 0;
}
2.基数排序(调用计数排序对每一位进行排序)
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
#include <time.h>
using namespace std;
//d是排序的依据,根据排序结果去改变a中元素的顺序,将结果存储在b中
int* countSort(int d[],int a[],int n,int k)//n means the length of a,k means the max of a[i]+1
{
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[d[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[d[i]]-1] = a[i];//必须用一个新数组去存储 如果直接在a上原址操作会互相覆盖,无法得到正确结果
c[d[i]] --;
}
delete c;
return b;
}
void radixSort(int a[],int n,int k)//k表示元素最多的位数
{
int *d = new int[n];//保存当前要排序的位
for(int i=1;i<=k;i++)//有多少位就进行多少次排序
{
for(int j=0;j<n;j++)//获得每一个元素对应的位的值
{
int x = (int)pow((float)10,(float)(i-1));
d[j] = a[j]/x%10;
}
int *b = countSort(d,a,n,n);
for(int l=0;l<n;l++)
a[l] = b[l];
delete b;
}
}
int main()
{
char s;
do{
int N = 10;
int *a = new int[N];
srand(time(0));
for(int i=0;i<N;i++)
{
a[i] = rand()%1000;
printf("%5d",a[i] );
}
cout<<endl;
long long begin = clock();
radixSort(a,N,3);//3表示元素最多有三位数
long long end = clock();
cout<<"time:"<<(end-begin)/(float)CLOCKS_PER_SEC*1000<<"ms"<<endl;
for(int i=0;i<N;i++)
printf("%5d",a[i] );
cout<<endl;
cin>>s;
}while(s == 'y' || s == 'Y');
return 0;
}
3.桶排序(利用插入排序为每个桶内的元素排序)
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
#include <time.h>
using namespace std;
struct Node
{
float value;
Node *next;
Node();
};
Node::Node()
{
next = 0;
}
void bucketSort(float a[],int n)
{
Node* node = new Node[n];//这相当于是一列索引 它们自身不保存元素
for(int i=0;i<n;i++)
{
Node* temp = &node[(int)(10*a[i])];
while(temp->next!=0 && temp->next->value<a[i])//找到当前元素应该插入的位置
{
temp = temp->next;
}
//插入此元素
Node* newnode = new Node;
newnode->value = a[i];
newnode->next = temp->next;
temp->next = newnode;
}
int j = 0;
for(int i=0;i<n;i++)
{
//输出元素
Node* temp = node[i].next;
while(temp != 0)
{
a[j++] = temp->value;
temp = temp->next;
}
//释放指针
Node* now = node[i].next;
while(now != 0)
{
temp = now->next;//保存指向的下一个node
delete now;//释放此node
now = temp;//重新进行循环
}
}
delete node;
}
int main()
{
char s;
do{
int N = 10;
float *a = new float[N];
//int *a = new int[N];
srand(time(0));
for(int i=0;i<N;i++)
{
a[i] = (float)(rand()%10)/N;
printf("%6.3f",a[i] );
//printf("%5f",a[i] );
}
cout<<endl;
long long begin = clock();
bucketSort(a,N);//
long long end = clock();
cout<<"time:"<<(end-begin)/(float)CLOCKS_PER_SEC*1000<<"ms"<<endl;
for(int i=0;i<N;i++)
//printf("%5f",a[i] );
printf("%6.3f",a[i] );
cout<<endl<<"please input y or Y to continue,any other means quit:"<<endl;
cin>>s;
}while(s == 'y' || s == 'Y');
return 0;
}