跪求c语言编程高手,题目——输入十个数,从大到小排序(两种方法)
发布网友
发布时间:2024-10-04 16:06
我来回答
共2个回答
热心网友
时间:2024-10-04 17:44
#include <stdio.h>
void sort1(int * a, int n) //选择排序
{
int i, j, max, tmp;
for (i = 0; i < n; i++)
{
max = i;
for (j = i + 1; j < n; j++)
if (a[max] < a[j])
max = j;
tmp = a[i];
a[i] = a[max];
a[max] = tmp;
}
}
void sort2(int * a, int n) //冒泡排序
{
int i, j, change, tmp;
for (i = 0; i < n; i++)
{
change = 0;
for (j = 1; j < n - i; j++)
{
if (a[j - 1] < a[j])
{
tmp = a[j - 1];
a[j - 1] = a[j];
a[j] = tmp;
change = 1;
}
}
if (change == 0) break;
}
}
int main()
{
int a[100], b[100], n;
int i;
printf("Input the n: ");
scanf("%d", &n);
printf("Input %d number: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
b[i] = a[i];
}
sort1(a, n);
sort2(b, n);
printf("sort1: ");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nsort2: ");
for (i = 0; i < n; i++)
printf("%d ", b[i]);
printf("\n");
return 0;
}
热心网友
时间:2024-10-04 17:44
//冒泡排序
main()
{int i,j,n,a[10];
printf("输入10个数:");
for(i=0;i<10;i++)
scanf("%d,",&a[i]);//记得输入的时候后面加“,”
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
if(a[i]<a[j])
{ n=a[i];a[i]=a[j];a[j]=a[i];}
printf("\n由小到大排序结果为:");
for(i=0;i<10;i++)
printf("%d,",a[i]);
}追问还有呐??俩种方法呐!!