选择法:
int main()
{
int a,b;
int arr[10];
int tmp[1];
for (a = 0; a < 10; a++)
{
scanf("%d", &arr[a]);
}
for (b = 0; b < 10; b++)
{
for (a = b; a < 10; a++)
{
if (arr[b] > arr[a])
{
tmp[0] = arr[a];
arr[a] = arr[b];
arr[b] = tmp[0];
}
}
}
for (a = 0; a < 10; a++)
{
printf("%d ", arr[a]);
}
return 0;
}
冒泡法
void mpf_sort(int arr[10])
{
int i = 0;
int tmp;
for (i = 0; i < 9; i++)
{
int j = 0;
for (j = 0; j < 9 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int a;
int arr[10];
for (a = 0; a < 10; a++)
{
scanf("%d", &arr[a]);
}
mpf_sort(arr);
for (a = 0; a < 10; a++)
{
printf("%d\n", arr[a]);
}
return 0;
}
冒泡法(精简
void mpf_sort(int arr[10])
{
int i = 0;
int tmp;
for (i = 0; i < 9; i++)
{
int j = 0;
int result = 1;
for (j = 0; j < 9 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = tmp;
result = 0;
}
if (result)
break;
}
}
}
int main()
{
int a;
int arr[10];
for (a = 0; a < 10; a++)
{
scanf("%d", &arr[a]);
}
mpf_sort(arr);
for (a = 0; a < 10; a++)
{
printf("%d\n", arr[a]);
}
return 0;
}