|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/* 使用选择排序给输入的数据排序 */
#include<stdio.h>
#define N 3
void selectionSort(int a[],int);
int main(void)
{
int a[N];
int i;
printf("Enter %d numbers to be sorted:",N);
for(i = 0; i < N; i++)
{
scanf("%d",&a[i]);
}
selectionSort(a,N);
printf("In order:\n");
for(i = 0; i < N; i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
void selectionSort(int a[],int n)
{
int temp;
int max = 0;
int index;
int i;
while(n >= 2)
{
for(i = 0; i < n; i++)
{
if(a[i] > max)
{
max = a[i];
index = i;
}
}
n--;
if(index != n)
{
temp = a[n];
a[n] = a[index];
a[index] = temp;
}
selectionSort(a,n);
}
}
请问哪里错了,问什么不能正确地排序? |
|