![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
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.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
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 sechudleSJFIt 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. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
alright i had some time and looked at it and figured it out. I used a simple bubble sort.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 8:19 PM |
| Sorting an array of objects | oNe8 | Java | 2 | Feb 22nd, 2006 10:59 PM |
| Sorting without a large array | sim_maroon | C | 12 | Nov 21st, 2005 8:45 PM |
| Sorting a Numeric Array | little_valaree | Java | 2 | Nov 21st, 2005 11:00 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |