Topic: Sort 10 integers
background:
Idea: Use an array, use two for loop nests (using the bubbling method), and then use a loop to print the array
code:
#include<stdio.h>
int main()
{
int arr[10];
int i = 0;
int j = 0;
int temp = 0;
//输入 10 个整数
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
//判断
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10 - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//输出
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Details: When i % j == 0 to jump out of the loop.