欢迎来到天天文库
浏览记录
ID:40500757
大小:15.80 KB
页数:8页
时间:2019-08-03
《常用排序算法的C语言实现》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、常用排序算法的C语言实现//QuickSort快速排序//BubbleSort冒泡排序//InsertSort插入排序//ShellSort希尔排序//MegeSort归并排序//HeapSort堆排序//BucketSort桶排序//RadixSort基数排序#include#include/*快速排序*/voidQuickSort(int*a,intp,intq){intk,m,n,tmp;if(p2、;}}a[q]=a[n];a[n]=k;QuickSort(a,p,n-1);QuickSort(a,n+1,q);}}/*冒泡排序*/voidBubbleSort(int*a,intn){inti,j;inttmp;if(n<2)return;for(i=0;ia[j+1]){tmp=a[j];a[j]=a[j+1];a[j+1]=tmp;}}}}/*插入排序*/voidInsertSort(int*a,intn){inti,j,k;if(n<2)return;for(i=1;i3、or(j=i-1;j>=0;j--){if(k1){d=(d+1)/2;for(i=0;i4、tM=i;inttmp;if(i<=(n/2-1)){if(lca[i]){M=lc;}if(rca[M]){M=rc;}if(i!=M){tmp=a[M];a[M]=a[i];a[i]=tmp;HeapAdjust(a,M,n);}}}staticvoidBuildHeap(int*a,intn){inti;for(i=(n/2-1);i>=0;i--){HeapAdjust(a,i,n);}}voidHeapSort(int*a,intn){inti,tp;BuildHeap(a,n);for(i=n-1;i>=0;i--){tp=a[i];5、a[i]=a[0];a[0]=tp;HeapAdjust(a,0,i);PrintA(a,13);}}/*桶排序(桶排序是一个已知范围排序,这里假设范围为0-99)*/voidBucketSort(int*a,intn){intb[100]={0};//使用10个桶分别表示0-9inti,j;for(i=0;i6、rt(int*a,intn,intMax_w){typedefstructLIST{intval;structLIST*next;}List;inti,j,d;Listr[10];List*p,*q;for(i=0;i<10;i++){r[i].val=0;r[i].next=NULL;}for(i=0;inext!=NULL){p=p->next;}p->next=(List*)malloc(sizeof(structLIST));p->next->val=a[i];p->next->next=NULL;}for(i=7、0,j=0;i<10;i++){p=&r[i];while(p->next!=NULL){a[j++]=p->next->val;p=p->next;}while(r[i].next!=NULL)//回收内存{p=&r[i];q=p;while(p->next!=NULL){q=p;p=p->next;}if(p!=q){free(p);q->next=NULL;}}}for(j=1,d=1;j
2、;}}a[q]=a[n];a[n]=k;QuickSort(a,p,n-1);QuickSort(a,n+1,q);}}/*冒泡排序*/voidBubbleSort(int*a,intn){inti,j;inttmp;if(n<2)return;for(i=0;ia[j+1]){tmp=a[j];a[j]=a[j+1];a[j+1]=tmp;}}}}/*插入排序*/voidInsertSort(int*a,intn){inti,j,k;if(n<2)return;for(i=1;i3、or(j=i-1;j>=0;j--){if(k1){d=(d+1)/2;for(i=0;i4、tM=i;inttmp;if(i<=(n/2-1)){if(lca[i]){M=lc;}if(rca[M]){M=rc;}if(i!=M){tmp=a[M];a[M]=a[i];a[i]=tmp;HeapAdjust(a,M,n);}}}staticvoidBuildHeap(int*a,intn){inti;for(i=(n/2-1);i>=0;i--){HeapAdjust(a,i,n);}}voidHeapSort(int*a,intn){inti,tp;BuildHeap(a,n);for(i=n-1;i>=0;i--){tp=a[i];5、a[i]=a[0];a[0]=tp;HeapAdjust(a,0,i);PrintA(a,13);}}/*桶排序(桶排序是一个已知范围排序,这里假设范围为0-99)*/voidBucketSort(int*a,intn){intb[100]={0};//使用10个桶分别表示0-9inti,j;for(i=0;i6、rt(int*a,intn,intMax_w){typedefstructLIST{intval;structLIST*next;}List;inti,j,d;Listr[10];List*p,*q;for(i=0;i<10;i++){r[i].val=0;r[i].next=NULL;}for(i=0;inext!=NULL){p=p->next;}p->next=(List*)malloc(sizeof(structLIST));p->next->val=a[i];p->next->next=NULL;}for(i=7、0,j=0;i<10;i++){p=&r[i];while(p->next!=NULL){a[j++]=p->next->val;p=p->next;}while(r[i].next!=NULL)//回收内存{p=&r[i];q=p;while(p->next!=NULL){q=p;p=p->next;}if(p!=q){free(p);q->next=NULL;}}}for(j=1,d=1;j
3、or(j=i-1;j>=0;j--){if(k1){d=(d+1)/2;for(i=0;i4、tM=i;inttmp;if(i<=(n/2-1)){if(lca[i]){M=lc;}if(rca[M]){M=rc;}if(i!=M){tmp=a[M];a[M]=a[i];a[i]=tmp;HeapAdjust(a,M,n);}}}staticvoidBuildHeap(int*a,intn){inti;for(i=(n/2-1);i>=0;i--){HeapAdjust(a,i,n);}}voidHeapSort(int*a,intn){inti,tp;BuildHeap(a,n);for(i=n-1;i>=0;i--){tp=a[i];5、a[i]=a[0];a[0]=tp;HeapAdjust(a,0,i);PrintA(a,13);}}/*桶排序(桶排序是一个已知范围排序,这里假设范围为0-99)*/voidBucketSort(int*a,intn){intb[100]={0};//使用10个桶分别表示0-9inti,j;for(i=0;i6、rt(int*a,intn,intMax_w){typedefstructLIST{intval;structLIST*next;}List;inti,j,d;Listr[10];List*p,*q;for(i=0;i<10;i++){r[i].val=0;r[i].next=NULL;}for(i=0;inext!=NULL){p=p->next;}p->next=(List*)malloc(sizeof(structLIST));p->next->val=a[i];p->next->next=NULL;}for(i=7、0,j=0;i<10;i++){p=&r[i];while(p->next!=NULL){a[j++]=p->next->val;p=p->next;}while(r[i].next!=NULL)//回收内存{p=&r[i];q=p;while(p->next!=NULL){q=p;p=p->next;}if(p!=q){free(p);q->next=NULL;}}}for(j=1,d=1;j
4、tM=i;inttmp;if(i<=(n/2-1)){if(lca[i]){M=lc;}if(rca[M]){M=rc;}if(i!=M){tmp=a[M];a[M]=a[i];a[i]=tmp;HeapAdjust(a,M,n);}}}staticvoidBuildHeap(int*a,intn){inti;for(i=(n/2-1);i>=0;i--){HeapAdjust(a,i,n);}}voidHeapSort(int*a,intn){inti,tp;BuildHeap(a,n);for(i=n-1;i>=0;i--){tp=a[i];
5、a[i]=a[0];a[0]=tp;HeapAdjust(a,0,i);PrintA(a,13);}}/*桶排序(桶排序是一个已知范围排序,这里假设范围为0-99)*/voidBucketSort(int*a,intn){intb[100]={0};//使用10个桶分别表示0-9inti,j;for(i=0;i6、rt(int*a,intn,intMax_w){typedefstructLIST{intval;structLIST*next;}List;inti,j,d;Listr[10];List*p,*q;for(i=0;i<10;i++){r[i].val=0;r[i].next=NULL;}for(i=0;inext!=NULL){p=p->next;}p->next=(List*)malloc(sizeof(structLIST));p->next->val=a[i];p->next->next=NULL;}for(i=7、0,j=0;i<10;i++){p=&r[i];while(p->next!=NULL){a[j++]=p->next->val;p=p->next;}while(r[i].next!=NULL)//回收内存{p=&r[i];q=p;while(p->next!=NULL){q=p;p=p->next;}if(p!=q){free(p);q->next=NULL;}}}for(j=1,d=1;j
6、rt(int*a,intn,intMax_w){typedefstructLIST{intval;structLIST*next;}List;inti,j,d;Listr[10];List*p,*q;for(i=0;i<10;i++){r[i].val=0;r[i].next=NULL;}for(i=0;inext!=NULL){p=p->next;}p->next=(List*)malloc(sizeof(structLIST));p->next->val=a[i];p->next->next=NULL;}for(i=
7、0,j=0;i<10;i++){p=&r[i];while(p->next!=NULL){a[j++]=p->next->val;p=p->next;}while(r[i].next!=NULL)//回收内存{p=&r[i];q=p;while(p->next!=NULL){q=p;p=p->next;}if(p!=q){free(p);q->next=NULL;}}}for(j=1,d=1;j
此文档下载收益归作者所有