// Bubble Sort
#include<stdio.h>
int main( )
{
int a[10];
int i,j,t;
printf("Pls input 10 numbers:\n");
for (i = 0; i < 10; i++)
scanf("%d",&a[i]);
printf("\n");
for (j = 0; j < 9; j++)
{
for (i = 0; i < 9-j; i++) // After finishing the first "for" circle, the largest number will be move to the last position. The second "for" circle will move the second largest number to the penultimate position.
{
if (a[i] > a[i+1])
{
t = a[i];
a[i] = a[i+1];
a[i+1] = t;
}
}
}
printf("The sorted numbers:\n");
for (i = 0; i < 10;i++)
printf("%5d",a[i]);
printf("\n");
return 0;
}
Output:
Pls input 10 numbers:
8 4 9 5 6 7 2 3 1 0
The sorted numbers:
0 1 2 3 4 5 6 7 8 9