Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   sorting an array by field (http://www.programmingforums.org/showthread.php?t=12933)

cwl157 Apr 3rd, 2007 11:23 PM

sorting an array by field
 
Hi I need to know how to sort an array of stucts based on one of the fields. I have to go from smallest to largest. The struct has 4 fields in it: int processNbr; int totalTime; int remainingTime; int stopTime; int isFinished;. I think i'm really only concerned with processNbr and totalTime I have it to where it finds and prints the smallest ok but from there i dont know how to "scratch that one off the list" and find the next one and print it and then find the next largest and print it. I have a for loop go through the array to find the smallest but then how do i run through it again to find the next smallest. I'll post the function that i'm talking about. It is for a cpu time schedule simulator for shortest job first job processing method thats why i need to sort by the totalTime field and put the smallest first, followed by the next smallest and so on and then somehow keep track of the order so i can print the processNbr and totalTime for each of them in the order they would finish at the end.
:

//###################################################################
void scheduleSJF(processDataType processes[], int processCount)
{
  int i;
  int minimumProcessTimeIndex;
  int minimumProcessRunningTime = processes[0].totalTime;
  for (i = 1; i < processCount; i++)
  {
    if (processes[i].totalTime < minimumProcessRunningTime)
    {
      minimumProcessRunningTime = processes[i].totalTime;
      minimumProcessTimeIndex = i;
    } // end if
  } // end for
  printf("the lowestRunning time is %d\n", minimumProcessRunningTime);
  printf("the minimumProcessTimeIndex is %d\n", minimumProcessTimeIndex);

} // end sechudleSJF


DaWei Apr 3rd, 2007 11:38 PM

Your job isn't to find the smallest, but to arrange the entire contents in order. There is a thread on the forum that covers a multitude of sorting algorithms, pretty nice thread. You can find it here. If that doesn't do the trick, post back.

Jimbo Apr 3rd, 2007 11:39 PM

Hm... it depends a little on how you're going to operate. If you were to sort the entire list each time you made a decision, you could do something like the following:
:

void someComparisonBasedSort(processDataType pdt [], size_t len, int (*compare)(processDataType, processDataType))
where compare would be a function pointer used to determine the relative values of its parameters based on however you want. So you could have one function, compareByTotalTime(processDataType, processDataType) which would return an int for the comparison result and call your sort by passing &compareByTotalTime.

Another way to do it is to keep the list of processes sorted as you add processes to the list, just doing an insertion as appropriate each time.

cwl157 Apr 4th, 2007 12:01 AM

i tried this:
:

void scheduleSJF(processDataType processes[], int processCount)
{
  int i;
  int j = 0;
  int minimumProcessTimeIndex;
  int minimumProcessRunningTime = processes[0].totalTime;
  for (i = 1; i < processCount; i++)
  {
    if (processes[i].totalTime < minimumProcessRunningTime)
    {
      processes[j].processNbr = processes[i].processNbr;
      processes[j].totalTime = processes[i].totalTime;
      processes[j].remainingTime = processes[i].remainingTime;
      processes[j].stopTime = processes[i].stopTime;
      j++;
      minimumProcessRunningTime = processes[i].totalTime;
      minimumProcessTimeIndex = i;
    } // end if
  } // end for

  for (j = 0; j < processCount; j++)
  {
    printf("processes[j].processNbr is %d\n", processes[j].processNbr);
    printf("processes[j].totalTime is %d\n", processes[j].totalTime);
    printf("processes[j].remainingTime is %d\n", processes[j].remainingTime);
    printf("processes[j].stopTime is %d\n", processes[j].stopTime);
  }  // end for
} // end sechudleSJF


It doesn't work right but i think its not that far off. I think what it does is print the shortest one twice and then skip the longest one. Thats when i tried with 3 inputs. I tried it with 4 and it skipped one in the middle but still printed the shortest twice.

EDIT: Wait i tried it with a bunch of them and no i dont think it works at all.

cwl157 Apr 4th, 2007 2:48 PM

alright i had some time and looked at it and figured it out. I used a simple bubble sort.


All times are GMT -5. The time now is 4:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC