Given an array I am required to sort it in such a way that the first element is the smallest value, the second element is the largest, the third element is the second smallest element and so on.
But my code just prints the original array and I am not able to figure out why. Any advice would be appreciated.
#include <stdio.h>
void swap(int m, int n);
int main()
{
int i,j,A[10],n;
printf ("enter the number of array elements
");
scanf ("%d", &n);
for (i=0;i<n;i++){
scanf ("%d", &A[i]);
}
for (i=0;i<n;i++){
if (i%2 == 0){
for (j=i;j<n;j++){
if (A[j] < A[i]){
swap(A[i],A[j]);
}
}
}
else if (i%2 != 0){
for (j=i;j<n;j++){
if (A[j] > A[i]){
swap (A[i],A[j]);
}
}
}
}
for(i=0;i<n;i++){
printf ("%d
", A[i]);
}
return 0;
}
void swap( int m, int n)
{
int temp;
temp = m;
m = n;
n = temp;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…