![]() |
|
#1
|
|||
|
|||
Selection Sorting--Help Urgently NeededFirst of all, I'd like to thank those of you who helped me with my last problem.
Unfortunately, I now have a very perplexing problem with a function I am trying to create. The function is supposed to run a selection sort on a two-dimensional array, sorting with the first column (the numbers in the first column are called the department numbers). I have managed to do that part, but the requirements of the program demand that I sort the array again, and this time sort within the department number, but by the numbers in the second column of the array. As an example, this is how it the array should look. Without sorting: 2 9 389 5 2 083 3 4 382 2 5 732 3 6 892 2 7 297 5 1 874 After the first sort: 2 9 389 2 5 732 2 7 297 3 4 382 3 6 892 5 2 083 5 1 874 After the second sort: 2 5 732 2 7 297 2 9 389 3 4 382 3 6 892 5 1 874 5 2 083 The problem that I have is that I cannot make the second sort work without sorting the entire array by the values in the second column. I need to figure out a way of making it stop when it encounters the end of the 2's, 3's, or 5's, and then on the next pass start with the next section of numbers (that is, within the 2, 3, or 5 section). The code I am using follows: CPP / C++ / C Code:
Any help would be greatly appreciated, as this is due tomorrow. |
|||
|
#2
|
||||
|
||||
|
Hello,
I believe I understand what you are asking. I have rewritten the last part of your sorting code that should allow you to accomplish this task. I'll show you the code, and explain it step by step: CPP / C++ / C Code:
This is the new code that you can replace after the first two commented lines you will find in the sortem function. I just revised those few lines. The first line of code you will notice a loop that loops through all the elements. This will allow us to sort appropriately. The second line is an if statement. The if statement basically means this: If the next index does not equal the limit, and this index is equal to the next index The third line starts the sort scan at i, our current location. From there we increment until the end; but not exactly. We increment until the index of emp is no longer the same to the next. Giving us the advantage of only dealing with that one value. When the loop starts, we sort the array. I took your first sort code and modified it to sort [1] instead of [0] against minValue1; rather than minValue0. It's that simple. My output: CPP / C++ / C Code:
If you have further questions, please feel free to ask. - Stack Overflow __________________
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [C] / [C++] tags. Your question may have been asked before, try the search facility. Last edited by Stack Overflow : 02-May-2005 at 12:40.
Reason: Updated code example
|
|
#3
|
|||
|
|||
|
Many thanks for the assisstance, Stack. Your code worked perfectly for me.
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Free 1st month / Free setup / No credit card needed...Plans start at 4.95 | LarryIsaac | Web Hosting Advertisements & Offers | 0 | 11-Oct-2003 14:03 |
| Sorting columns by selection | dean | MySQL / PHP Forum | 1 | 03-Oct-2003 06:29 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The