用C语言编个程序:写一函数,用“选择法”对8个整数进行排序(降序) ,怎么编?哪个高手可以帮下啊

如题所述

#include "stdio.h"

void select_sort(int a[],int n)
{
int i,j,k,temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(a[j]>a[k])//将这里改成a[j]<a[k]就是按升序排序
{
k=j;
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
}//定义选择排序函数

int main()
{
int i;
int a[8];
printf("Please input 8 nubmers:\n");
for(i=0;i<8;i++)
{
scanf("%d",&a[i]);
}//从键盘读入8个整数
printf("The 8 numbers you input is:\n");
for(i=0;i<8;i++)
{
printf("%d ",a[i]);
}
printf("\n");
select_sort(a,8);//调用选择排序函数
printf("The sorted numbers is\n");
for(i=0;i<8;i++)
{
printf("%d ",a[i]);
}//对排好序的数组进行输出
printf("\n");
getchar();

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-02
#ifndef ARRAY_BASED_SORTING_FUNCTIONS
#define ARRAY_BASED_SORTING_FUNCTIONS
template <class T>
void Swap(T &x,T &y)
{
T temp;
temp=x;
x=y;
y=temp;
}
template <class T>
void Selection(T A[],int n)
{
int samllIndex;
int i,j;
for(i=0;i,n-1;i++)
{
scanf(A[i]);
}
for(i=0;i,n-1;i++)
{
smallIndex=i;
for(j=i+1;j<n;j++)
if(A[j]<A[smallIndex])
samllIndex=j;
Swap(A[i],A[samllIndex])
printf(A[samllIndex]);
}
}
#endef
你自己再看看,我想大致是这样的
相似回答