#include <stdio.h>
void bubble_sort(int a[], int n)
{
int i, j, tmp;
for(j = 0; j < n - 1; j++)
{
for(i = 0; i < n - 1 - j; i++)
{
if (a[i] > a[i + 1])
{
tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
}
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
}
void sellect_sort(int a[], int n)
{
int i, j, min_idx, min;
for(j = 0; j < n - 1; j++)
{
min = a[j];
min_idx = j;
for(i = j + 1; i < n; i++)
{
if (min > a[i])
{
min = a[i];
min_idx = i;
}
}
a[min_idx]=a[j];
a[j]=min;
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
}
int main()
{
int a[10] = {3, 6, 7, 2, 4, 9, 0, 5, 8, 1};
int i;
printf("before sort:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\n");
printf("\n");
//bubble_sort(a, 10);
sellect_sort(a, 10);
printf("\nafter sort:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
冒泡排序法和选择排序法
最新推荐文章于 2021-11-25 15:32:00 发布