冒泡排序
#include <stdio.h>
int main(int argc,char *argv[])
{
int a[5] = {2,5,1,4,3};
int n = 5;
int tmp = 0;
int i=0,j=0;
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1-i);j++)
{
if(a[j] > a[j+1])
{
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
选择排序
#include <stdio.h>
int main(int argc,char *argv[])
{
int a[5] = {2,5,1,4,3};
int n = 5;
int tmp = 0;
int i=0,j=0;
for(i=0;i<(n-1);i++)
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
图示
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/d2c84873245eccf6b1ac1c1779238aa8.png)