![]() |
|
#1
|
|||
|
|||
Bubble Sorts with 2d ArraysHi, I've got a 2D array which I would like to sort into descending order according to the first column but keep the information in the 2nd column in the same row as the information in the first column no matter where the sort moves it to in the array. To explain more clearly:
Supposing I had this 2D array: |5 1| |6 2| |3 3| |8 4| |2 5| I want my bubble sort to be able to sort it into: |8 4| |6 2| |5 1| |3 3| |2 5| All the info I've found on bubble sorts has only been about 1D arrays. Any help or a nudge in the right direction would be really appreciated! Thanks, Lewis |
|||
|
#2
|
|||
|
|||
Re: Bubble Sorts with 2d ArraysQuote:
Most generic sorting algorithms tend to sort in ascending order rather than descending order. In your data set example, you would only have to look at the first dimension to sort it per your requirements. However, what would you expect from: 8,1 8,5 8,2 8,3 8,4 7,1 7,9 Do you want: 8,5 8,4 8,3 8,2 8,1 7,9 7,1 ...or something else? :davis: |
|
#3
|
|||
|
|||
Re: Bubble Sorts with 2d ArraysThanks, yes that is the output I'd want my algorithm to give. To give you a bit more info:
The data that is stored in the array is essentially a frequency analysis. Column two will list the number and column one will list how times that number has occured in a previously scanned document. Column 2 will always be ascending from from 0 to 255 and I want to be able to sort the array into an order according to which numbers occur the most but still be able to easily see which number it is that occured that number of times which is why ive got the 2nd column. eg. The top 7 bits of the array would look something like this: 84 0 65 1 40 2 32 3 55 4 10 5 27 6 and ideally this would be sorted to: 84 0 65 1 55 4 40 2 32 3 27 6 10 5 so I could see straight away that the number '0' occured 84 times in the document. As for descending or ascending, it doesnt make a huge difference to me but descending is preferable. Thanks again, Lewis |
|
#4
|
||||
|
||||
Re: Bubble Sorts with 2d ArraysQuote:
Then access your 2D array usingf this 'index' array: CPP / C++ / C Code:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#5
|
|||
|
|||
Re: Bubble Sorts with 2d ArraysThanks for that I'll try it out. Another problem I've got (hopefully there won't be too many more!) is in trying to read in a text file which is essentially just a list of words. I want to open the text file, read the list of words into an array, each array location holding a word.
I (naively no doubt) thought it would be as easy as to use a few lines like: Code:
but it's not working properly so I've no doubt got completley the wrong idea. As you've no doubt noticed I'm new to C and am on a fairly steep learning curve!! Thanks again, Lewis |
|
#6
|
||||
|
||||
Re: Bubble Sorts with 2d ArraysHere are some good links that you can learn more about Reading from files in C:
Explanation of Functions More Explaining Simple Example Another Example __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic vs Static Arrays | WaltP | Miscellaneous Programming Forum | 5 | 16-Feb-2006 15:54 |
| help with Bubble sort program that uses vectors instead of arrays | sasuke101 | C++ Forum | 9 | 25-Oct-2005 11:26 |
| Noob question on c arrays and functions | brett | C Programming Language | 1 | 20-Apr-2005 03:59 |
| Knight tour (arrays help needed) | dilmv | C++ Forum | 7 | 18-Oct-2004 14:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The