![]() |
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.
:
//################################################################### |
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.
|
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))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. |
i tried this:
:
void scheduleSJF(processDataType processes[], int processCount)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. |
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