I am trying to get my program to sort in ascending order but it will only sort in descending order. I have read all the articles, watched all the youtube videos I can find about bubble sort. I would will really appreciate someone taking a look and tell me what I am doing wrong. I know to change from ascending to descending all you have to do is change the signs. If I change the signs from how I have them now the array won't sort for me.
I am using Cygwin on Windows 7 64bit.
Code:
/* One Dimensional Array Source Code*/
#include <stdio.h>
main(void)
{
//Declare and Initialize Variables
int iArray[100] = {0};
int iX = 0;
int iNum = 0;
int iCount = 0;
int iTemp = 0;
int iJ = 0;
//Input Data Module
printf("\n Enter a number:");
printf("\n Enter 0 when done:\n");
scanf("%d", &iNum);
while(iNum != 0)
{
iArray[iX] = iNum;
printf("\n Enter a number:");
printf("\n Enter 0 when done:\n");
scanf("%d", &iNum);
iX++;
iCount++;
} //while
//Sorting Data Module
for (iX = 0; iX < iCount; iX++)
{
for (iJ= 0; iJ < (iCount - 1); iJ++)
{
if (iArray[iX] > iArray[iJ])
{
iTemp = iArray[iX];
iArray[iX] = iArray[iJ];
iArray[iJ] = iTemp;
}//end if
} //end for
} //end for
//Display Data Module
printf("\n The sorted list:\n\n\t");
for (iX = 0; iX <iCount; iX++)
{
printf("%d\n\t", iArray[iX]);
} //end for
return 0;
}//end program
Test Data
AL@Alecia-PC ~
$ ./a
Enter a number:
Enter 0 when done:
99
Enter a number:
Enter 0 when done:
34
Enter a number:
Enter 0 when done:
16
Enter a number:
Enter 0 when done:
1
Enter a number:
Enter 0 when done:
42
Enter a number:
Enter 0 when done:
0
The sorted list:
99
42
34
16
1
Thanks so much.