欢迎来到天天文库
浏览记录
ID:44703002
大小:132.01 KB
页数:14页
时间:2019-10-25
《C语言所有内部排序算法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C语言所有内部排序算法冒泡法,选择法,插入法,快排法,希尔,归并,...1冒泡法:#include#includevoidmao_pao(int*a,intn){inti,j,temp,flag;for(i=0;ia[j+1]) { flag=1; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; }}}}voidmain(){int*a,i,n;a=(int*)malloc(
2、100);if(NULL==a){ printf("allocationfailture"); exit(1);}printf("请输入你要排序的元素的个数");scanf("%d",&n);printf("现在开始输入%d个元素",n);for(i=0;i!=n;++i)scanf("%d",&a[i]);mao_pao(a,n);printf("排序后为:");for(i=0;i!=n;++i)printf("%d",a[i]);printf("");free(a);}2,选择排序法#include#inc
3、ludevoidxuan_zhe(int*a,intn){inti,j,temp,max;for(i=0;i4、输入你要排序的元素的个数");scanf("%d",&n);printf("现在开始输入%d个元素",n);for(i=0;i!=n;++i)scanf("%d",&a[i]);xuan_zhe(a,n);printf("排序后为:");for(i=0;i!=n;++i)printf("%d",a[i]);printf("");free(a);}3,插入排序#include#includevoidcha_ru(int*a,intn){inti,j,temp;for(i=0;i5、mp=a[i+1];for(j=i;j>=0&&temp6、tf("排序后为:");for(i=0;i!=n;++i)printf("%d",a[i]);printf("");free(a);}4..快速排序#include#includevoidkuai_pai(int*a,intlow,inthigh){intleft,right,middle,i,j,temp;left=low;right=high;middle=(left+right)/2;while(left7、++;while(right>low&&a[right]>a[middle]) right--;if(left<=right){temp=a[left];a[left]=a[right];a[right]=temp;left++;right--;}}if(leftlow) kuai_pai(a,low,right);}voidmain(){int*a,i,n;a=(int*)malloc(100);if(NULL==a){ printf("allocationfailt8、ure"); exit(1);}printf("请输入你要排序的元素的个数");sc
4、输入你要排序的元素的个数");scanf("%d",&n);printf("现在开始输入%d个元素",n);for(i=0;i!=n;++i)scanf("%d",&a[i]);xuan_zhe(a,n);printf("排序后为:");for(i=0;i!=n;++i)printf("%d",a[i]);printf("");free(a);}3,插入排序#include#includevoidcha_ru(int*a,intn){inti,j,temp;for(i=0;i5、mp=a[i+1];for(j=i;j>=0&&temp6、tf("排序后为:");for(i=0;i!=n;++i)printf("%d",a[i]);printf("");free(a);}4..快速排序#include#includevoidkuai_pai(int*a,intlow,inthigh){intleft,right,middle,i,j,temp;left=low;right=high;middle=(left+right)/2;while(left7、++;while(right>low&&a[right]>a[middle]) right--;if(left<=right){temp=a[left];a[left]=a[right];a[right]=temp;left++;right--;}}if(leftlow) kuai_pai(a,low,right);}voidmain(){int*a,i,n;a=(int*)malloc(100);if(NULL==a){ printf("allocationfailt8、ure"); exit(1);}printf("请输入你要排序的元素的个数");sc
5、mp=a[i+1];for(j=i;j>=0&&temp6、tf("排序后为:");for(i=0;i!=n;++i)printf("%d",a[i]);printf("");free(a);}4..快速排序#include#includevoidkuai_pai(int*a,intlow,inthigh){intleft,right,middle,i,j,temp;left=low;right=high;middle=(left+right)/2;while(left7、++;while(right>low&&a[right]>a[middle]) right--;if(left<=right){temp=a[left];a[left]=a[right];a[right]=temp;left++;right--;}}if(leftlow) kuai_pai(a,low,right);}voidmain(){int*a,i,n;a=(int*)malloc(100);if(NULL==a){ printf("allocationfailt8、ure"); exit(1);}printf("请输入你要排序的元素的个数");sc
6、tf("排序后为:");for(i=0;i!=n;++i)printf("%d",a[i]);printf("");free(a);}4..快速排序#include#includevoidkuai_pai(int*a,intlow,inthigh){intleft,right,middle,i,j,temp;left=low;right=high;middle=(left+right)/2;while(left7、++;while(right>low&&a[right]>a[middle]) right--;if(left<=right){temp=a[left];a[left]=a[right];a[right]=temp;left++;right--;}}if(leftlow) kuai_pai(a,low,right);}voidmain(){int*a,i,n;a=(int*)malloc(100);if(NULL==a){ printf("allocationfailt8、ure"); exit(1);}printf("请输入你要排序的元素的个数");sc
7、++;while(right>low&&a[right]>a[middle]) right--;if(left<=right){temp=a[left];a[left]=a[right];a[right]=temp;left++;right--;}}if(leftlow) kuai_pai(a,low,right);}voidmain(){int*a,i,n;a=(int*)malloc(100);if(NULL==a){ printf("allocationfailt
8、ure"); exit(1);}printf("请输入你要排序的元素的个数");sc
此文档下载收益归作者所有