Sorting an array of Structures
Oct 11, 2011 at 1:54am UTC
I was wondering what would be the syntax of sorting an array of structures that have names and score?
Would it be similar to a selection sort?
This is what i got. Also on the swap at the end of the code would i only have a need for one swap to keep the parallelism.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
void sortNameAndScoreArrays (char * nameArray[], unsigned int scoreArray[], int sortKey, int sortOrder, unsigned int size)
{
unsigned int indexToFix;
unsigned int tempScore, indexOfSmallestScore, indexOfLargestScore;
char * tempPtr;
unsigned int indexOfLowestAlphaName, indexOfHighestAlphaName;
int sortCase = sortKey * sortOrder;
for (indexToFix = 0; indexToFix < size -1; indexToFix++)
{
switch (sortCase)
{
case ASCENDING_NAME:
indexOfLowestAlphaName = indexToFix; // assume first unsorted location is smallest
for (unsigned int indexOfCurrentName = indexToFix + 1; indexOfCurrentName < size; indexOfCurrentName++)
{
if (strcmp (nameArray[indexOfCurrentName], nameArray[indexOfLowestAlphaName]) < 0)
indexOfLowestAlphaName = indexOfCurrentName;
}
if (indexToFix != indexOfLowestAlphaName)
{
tempPtr = nameArray[indexOfLowestAlphaName];
nameArray[indexOfLowestAlphaName] = nameArray[indexToFix];
nameArray[indexToFix] = tempPtr;
tempScore = scoreArray[indexOfLowestAlphaName];
scoreArray[indexOfLowestAlphaName] = scoreArray[indexToFix];
scoreArray[indexToFix] = tempScore;
}
break ;
Topic archived. No new replies allowed.