冒泡法排序,哪位高手能帮我找出问题来,没有语法错误的,只是实现不了我们所要的功能而已

来源:百度知道 编辑:UC知道 时间:2024/06/08 13:58:19
/* HELLO.C -- Hello, world */

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "time.h"

main()
{ int a[]={0};
int n,i,temp,j;
printf("How many students in your class:\n");
scanf("%d",&n);

printf("Enter the mark:\n");
for(i=0;i<n;i++)
scanf("%d",a[i]);
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("After sort:\n");
for(i=0;i<n;i++)
{printf("%d\n",a[i]);}
getch();
}

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "malloc.h" //malloc函数头文件
main()
{ int a[100]={0}; //这里你定义的是个1个元素的数组。。 你要定个足够大的数组,或用动态数组 ;记住a[]={}括号里有几个元素,这个数组就是多少大,因为你这个是静态数组。
//可以用动态数组
// int *arr;
// arr=(int *)malloc(sizeof(int));
int n,i,temp,j;
printf("How many students in your class:\n");
scanf("%d",&n);

printf("Enter the mark:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);//输入没 用 & 取地址 // scanf("%d",&arr[i]);//动态数组解法,下面把a都对应改成arr
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("After sort:\n");
for(i=0;i<n;i++)
{printf("%d\n",a[i]);}
getch();
}<