GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Apr-2006, 04:05
y2l y2l is offline
New Member
 
Join Date: Apr 2006
Posts: 4
y2l is on a distinguished road

Bubble Sorts with 2d Arrays


Hi, 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  
Old 14-Apr-2006, 06:41
davis
 
Posts: n/a

Re: Bubble Sorts with 2d Arrays


Quote:
Originally Posted by y2l
Hi, 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

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  
Old 14-Apr-2006, 07:13
y2l y2l is offline
New Member
 
Join Date: Apr 2006
Posts: 4
y2l is on a distinguished road

Re: Bubble Sorts with 2d Arrays


Thanks, 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  
Old 14-Apr-2006, 07:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Bubble Sorts with 2d Arrays


Quote:
Originally Posted by y2l
Thanks, 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
Create an array to be used as an index array. In your example, an array of 7 elements initialized with 0,1,2,3,4,5,6. Call it idx for example.

Then access your 2D array usingf this 'index' array:
CPP / C++ / C Code:
if (freq[idx[j]] > freq[idx[j+1]])
{
    // sort the idx array instead of 2 arrays
}
This way you can sort multiple columns simply by sorting the index array. The other columns never actually change.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #5  
Old 14-Apr-2006, 10:38
y2l y2l is offline
New Member
 
Join Date: Apr 2006
Posts: 4
y2l is on a distinguished road

Re: Bubble Sorts with 2d Arrays


Thanks 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:
char *colourname[255]; for(i=0; i < ctotal; i++) { fscanf(colours, "%s ", &colourname[i]); }

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  
Old 14-Apr-2006, 13:01
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: Bubble Sorts with 2d Arrays


Here 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 GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:27.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.