1:12 PM

(0) Comments

Sorting number in C

Jean-Michel

,

Stumble Upon Toolbar
I was looking in the code of my homemade linkedlist in C and i thought some people could use my algorithm so i transformed it to make it works with arrays

Here it comes




#include
#include
#include
int compareTo(int iNumber1, int iNumber2)
{
if( iNumber1 > iNumber2)
{
return 1;
}
else if ( iNumber1 == iNumber2)
{
return 0;
}
return -1;
}

void sortIntArray(int iArray[], int arraySize, int boolIsDecreasing)
{
int sortOrder = -1;
int cptIndex ;
if( boolIsDecreasing == 0 )
{
sortOrder = 1;
}
//For each item in the list, I compare the item to each item before him
for(cptIndex = 0; cptIndex < cptindex2 ="0" item =" iArray[cptIndex];" cptindex3 =" cptIndex;">= cptIndex2 && cptIndex3 != 0; cptIndex3--)
{
iArray[cptIndex3 ] = iArray[cptIndex3 -1];
}
iArray[cptIndex2] = item;
}
}
}
return iArray;
}
int main()
{
//Little demo of it working
int cptItem;
int intList[] = {3,2,6,1,4,2,9,45,2221,-21};
printf("Array in original order \n");
for(cptItem = 0; cptItem < (sizeof(intList) / sizeof(int)); cptItem++)
{
printf("%d\n", intList[cptItem]);
}
sortIntArray( intList,(sizeof(intList) / sizeof(int)), 0);
printf("\nArray in increasing order\n");
for(cptItem = 0;cptItem < (sizeof(intList) / sizeof(int)); cptItem++)
{
printf("%d \n", intList[cptItem]);
}
sortIntArray( intList,(sizeof(intList) / sizeof(int)), 1);
printf("\nArray in decreasing order\n");
for(cptItem = 0;cptItem < (sizeof(intList) / sizeof(int)); cptItem++)
{
printf("%d \n", intList[cptItem]);
}
_getch();
}
Stumble Upon Toolbar
0 Responses to "Sorting number in C"

Post a Comment

Have an opinion say it!