冒泡排序法 从小到大
//功能:用起泡法对10个整数从小到大排序
#include <stdio.h>
void sort(int *x, int n)
{
int i, j, t;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n -1- i; j++)
if (x[j] > x[j + 1])
{
t = x[j];
x[j] = x[j + 1];
x[j + 1] = t;
}
}
int main()
{
int i, n, a[100];
printf("please input the length of the array:\n");
scanf_s("%d", &n);
for (i = 0; i < n; i++)
scanf_s("%d", &a[i]);
sort(a, n);
printf("output the sorted array:\n");
for (i = 0; i <= n - 1; i++)
printf("%5d", a[i]);
printf("\n");
return 0;
}
运行结果
其它排序法
https://blog.csdn.net/qq_62755550/article/details/121568060?spm=1001.2014.3001.5501