用最小和最大值交替对数组进行排序 [英] Sorting an array with alternate smallest and largest values
本文介绍了用最小和最大值交替对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个数组,要求我对数组进行排序,以使最小值排在第一位,最大值排在第二位,第二最小值排在第三位,依此类推。但是,当我输入值时,我没有得到所需的输出。
Given an array, I was asked to sort it in a way so that the smallest values is first the, largest value second, the second smallest value third and so on. However when I input the values I do not get the required output.
由于我今天有考试,建议您多加注意。
Advise is appreciated as I have an exam today.
#include<stdio.h>
int main()
{
int i,j,k,a[6],temp,min;
for(i=0;i<6;i++)
scanf("%d",&a[i]);
for(j=0;j<6;j++)
{
if(j%2==0)
{
min=a[j];
for(k=j;k<6;k++)
{
if(a[k++]<min)
min=a[k++];
}
temp=a[j];
a[j]=min;
min=temp;
}
else
{
min=a[j];
for(k=j;k<6;k++)
{
if(a[k++]>min)
min=a[k++];
}
temp=a[j];
a[j]=min;
min=temp;
}
printf("%d ",a[j]);
}
}
推荐答案
min
用作索引( min = j
)而不是值( min = a [ j]
)
min
is used as an index(min=j
) instead of value(min=a[j]
)
就像这样:
min=j;
for(k=j;k<6;k++)
{
if(a[k]<a[min]){//if(a[k]>a[min]){
min=k;
}
}
temp=a[j];
a[j]=a[min];
a[min]=temp;
这篇关于用最小和最大值交替对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文