最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
c/c++ 数据结构和算法-选择排序/冒泡排序
时间:2022-06-25 08:04:30 编辑:袖梨 来源:一聚教程网
冒泡排序
#include
void BubbleSort(int *a,int n);
int main(void)
{
int k;
int a[10]={2,4,6,8,0,1,3,5,7,9};
for(k=0;k<10;k++)
{
if(k==9)
printf("%dn",a[k]);
else
printf("%d,",a[k]);
}
BubbleSort(a,10);
for(k=0;k<10;k++)
{
if(k==9)
printf("%dn",a[k]);
else
printf("%d,",a[k]);
}
return 0;
}
void BubbleSort(int *a,int n)
{
int i,j,t;
for(i=0;i
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
选择排序
#include
void SelectSort(int *a,int n);
int main(void)
{
int k;
int x[10]={2,4,6,8,0,1,3,5,7,9};
for(k=0;k<10;k++)
printf("%d ",x[k]);
printf("n");
SelectSort(x,10);
for(k=0;k<10;k++)
printf("%d ",x[k]);
printf("n");
return 0;
}
void SelectSort(int *a,int n)
{
int i,j,t;
int min;
for(i=0;i
min=i;
for(j=i+1;j
if(a[j]
min=j;
}
t=a[i];
a[i]=a[min];
a[min]=t;
}
}
好了以上就是一聚教程小伙伴为各位整理的一篇关于c语言中排序的方法的例子,希望这两种算法能够对各位初学者有帮助了。
相关文章
- 纪念碑谷3 生命花园关卡详解与生命之园全要素介绍 04-01
- 免费ppt成品网站哪里找-精选免费ppt资源导航合集 04-01
- 土豪漫画在线阅读免费下载安装最新版-土豪漫画在线阅读免费下载入口官方正版 04-01
- z-library最新网址-zlibrary官方入口 04-01
- 最终幻想纷争决战好玩吗|最终幻想纷争决战核心玩法与特色系统详解 04-01
- 植物大战僵尸怎么创建新账号 03-31