#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<time.h>
#define MAX 10000
int check(int a[],int n){
for (int i=0;i<n-1;i++)
if (a[i]>a[i+1])
return 0;
return 1;
}
int bubbleSort(int a[],int n){
for (int i=0;i<n-1;i++)
for (int j=0;j<n-1-i;j++)
if (a[j]>a[j+1]){
int tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
int insertSort(int a[],int n){
for (int i=0;i<n-1;i++){
int tmp=a[n-2-i];
int j;
for (j=n-1-i;j<n;j++)
if (a[j]<tmp)
a[j-1]=a[j];
else
break;
a[j-1]=tmp;
}
}
int merge(int a[],int l1,int r1,int l2,int r2){
int tmp[MAX];
int i1=l1,i2=l2,k=0;
while (i1<=r1 && i2<=r2)
if (a[i1]<=a[i2])
tmp[k++]=a[i1++];
else
tmp[k++]=a[i2++];
for (;i1<r1;i1++)
tmp[k++]=a[i1];
for (;i2<r2;i2++)
tmp[k++]=a[i2];
for (int i=0;i<k;i++)
a[i+l1]=tmp[k];
}
int mergeSort(int a[],int n){
int len=1;
while (len<n){
int l;
for (l=0;l+2*len-1<n;l+=2*len){
merge(a,l,l+len-1,l+len,l+2*len-1);
}
if (l+len<n)
merge(a,l,l+len-1,l+len,n-1);
len*=2;
}
}
void radixSort(int a[],int n){
long base=1; //attention!
int cnt[10],stop=0,tmp[MAX];
while (stop==0){
for (int i=0;i<10;i++)
cnt[i]=0;
stop=1;
for (int i=0;i<n;i++){
int idx=a[i]/base;
if (idx!=0)
stop=0;
idx%=10;
cnt[idx]++;
}
for (int i=1;i<10;i++)
cnt[i]+=cnt[i-1];
for (int i=n-1;i>=0;i--){
int idx=a[i]/base;
idx%=10;
tmp[--cnt[idx]]=a[i];
}
for (int i=0;i<n;i++)
a[i]=tmp[i];
base*=10;
}
}
typedef struct node_t{
int data;
struct node_t *l,*r;
}node_t;
void insert(node_t ** p,int data){
if ((*p)==0){
*p=(node_t *)malloc(sizeof(node_t));
(*p)->l=(*p)->r=0;
(*p)->data=data;
return;
}
if (data>=(*p)->data)
insert(&(*p)->r,data);
else
insert(&(*p)->l,data);
}
void traverse(node_t *root,int a[],int *k){
if (root==0)
return;
traverse(root->l,a,k);
a[(*k)++]=root->data;
traverse(root->r,a,k);
}
void binarySort(int a[],int n){
node_t *root=0;
for (int i=0;i<n;i++)
insert(&root,a[i]);
int k=0;
traverse(root,a,&k);
}
int quickSort(int a[],int l,int r){
int x=a[l];
int i,j=l;
if (l>=r)
return 0;
for (i=l+1;i<=r;i++)
if (a[i]<x){
a[j++]=a[i];
a[i]=a[j];
}
a[j]=x;
quickSort(a,l,j-1);
quickSort(a,j+1,r);
}
int selectSort(int a[],int n){
for (int i=0;i<n-1;i++){
int min=a[i];
int minj=i;
for (int j=i+1;j<n;j++)
if (min>a[j]){
min=a[j];
minj=j;
}
int tmp=a[i];
a[i]=min;
a[minj]=tmp;
}
}
int shellSort(int a[],int n){
int gap=n/2;
while (gap!=0){
for (int i=gap;i<n;i++){
int tmp=a[i];
int j;
for (j=i;j>=gap && a[j-gap]>tmp;j-=gap)
a[j]=a[j-gap];
a[j]=tmp;
}
gap/=2;
}
}
int heapSort(int a[],int n){
int l=n;
for (int i=(n-2)/2;i>=0;i--){
int j=i;
int tmp=a[j];
while(1){
int maxj=j;
int max=tmp;
if (2*j+1<n && a[2*j+1]>tmp){
maxj=2*j+1;
max=a[2*j+1];
}
if (2*j+2<n && a[2*j+2]>max){
maxj=2*j+2;
max=a[2*j+2];
}
if (maxj==j)
break;
a[j]=a[maxj];
j=maxj;
}
a[j]=tmp;
}
for (int i=n-1;i>0;i--){
int tmp=a[i];
a[i]=a[0];
a[0]=tmp;
l--;
int j=0;
while(1){
int maxj=j;
int max=tmp;
if (2*j+1<l && a[2*j+1]>tmp){
maxj=2*j+1;
max=a[2*j+1];
}
if (2*j+2<l && a[2*j+2]>max){
maxj=2*j+2;
max=a[2*j+2];
}
if (maxj==j)
break;
a[j]=a[maxj];
j=maxj;
}
a[j]=tmp;
}
}
int main(void){
int a[MAX];
srand(time(0));
for (int i=0;i<MAX;i++)
a[i]=rand();
bubbleSort(a,MAX);
if (check(a,MAX))
printf("ok\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
insertSort(a,MAX);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
mergeSort(a,MAX);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
radixSort(a,MAX);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
binarySort(a,MAX);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
selectSort(a,MAX);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
shellSort(a,MAX);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
heapSort(a,MAX);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
for (int i=0;i<MAX;i++)
a[i]=rand();
quickSort(a,0,MAX-1);
if (check(a,MAX))
printf("ok\n");
else
printf("error\n");
}
排序算法大合集
最新推荐文章于 2023-08-09 21:11:49 发布