By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

2D array sorting

P: 5
So, I've been working on this project where I am supposed to sort a 2D array [5][5] (where the values go in descending order) in descending and ascending order. I've tried doing variations of selection sort and have been getting erroneous results. How do I do this? What I have is two for loops that
Expand|Select|Wrap|Line Numbers
  1. void broadSort (int array1 [][5], int& startRows, int& startCols, bool sortType)
  2. {
  3.    for (startRows=0;startRows<5;startRows++)
  4.    {
  5.       for (startCols=0;startCols<5;startCols++)
  6.          valueSearch (array1, startRows, startCols, sortType);
  7.    }
  8. }
  9.  
  10. void valueSearch (int array1 [][5], int startRows, int startCols, bool sortType)
  11. {
  12.    for (int iSmallerRows=startRows;iSmallerRows<5;iSmallerRows++)
  13.    {
  14.       for (int iSmallerCols=startCols;iSmallerCols<5;iSmallerCols++)
  15.       {
  16.          if (array1 [iSmallerRows][iSmallerCols]<array1[startRows][startCols])
  17.             valueChange (array1, startRows, startCols, iSmallerRows, iSmallerCols);
  18.       }
  19.    }
  20.    switch (sortType)
  21.    {
  22.       case false: 
  23.          for (int iSmallerRows=startRows;iSmallerRows<5;iSmallerRows++)
  24.          {
  25.             for (int iSmallerCols=startCols;iSmallerCols<5;iSmallerCols++)
  26.             {
  27.                if (array1 [iSmallerRows][iSmallerCols]<array1[startRows][startCols])
  28.                   valueChange (array1, iSmallerRows, iSmallerCols, startRows , startCols );
  29.             }
  30.          }
  31.          break;
  32.       case true:
  33.          for (int iLargerRows=startRows;iLargerRows<5;iLargerRows++)
  34.          {
  35.             for (int iLargerCols=startCols;iLargerCols<5;iLargerCols++)
  36.             {
  37.                   if (array1 [iLargerRows][iLargerCols]>array1[startRows][startCols])
  38.                      valueChange (array1, iLargerRows, iLargerCols, startRows, startCols);
  39.             }
  40.          }
  41.    }
  42. }
  43.  
  44.  
  45. void valueChange (int array1 [][5], int vR1, int vC1, int vR2, int vC2)
  46. {
  47.    int temp=0;
  48.    temp=array1[vR1][vC1];
  49.    array1[vR1][vC1]=array1[vR2][vC2];
  50.    array1[vR2][vC2]=temp;
  51. }
This is what I have for the functions. It works for descending order, but not ascending. Really weird.
Feb 25 '07 #1
Share this Question
Share on Google+
5 Replies


P: 5
oops! that's supposed to read "where the values go in randomly"

All that brain bio study this evening has made me make awful typos.
Feb 25 '07 #2

P: 5
can anybody help w/ this?
Feb 25 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Not exactly sure how you wanted to sort the 2D array, or which sorting method you are implementing. Can you tell me how you would sort a 5x5 array, for example?
Feb 25 '07 #4

P: 5
I think what I was doing up there was a selection sort? See, the professor did not explain any of this stuff and the book has nothing on 2D array sorting.

What I figured was I'd have one function (called broadSort in my program) consist of two for loops that selected an x and y index for array1 which was passed onto a function (lets call it valueSearch) with two for loops that would generate indexes to compare against. If the values contained by the indexes of array1 generated by the loops in valueSearch were greater than the values in the indexes passed by broadSort, then a swap would be made.

Does that make better sense :-)?
Feb 25 '07 #5

P: 5
Oh, and here's the code (the first one I posted contained some typos):
Expand|Select|Wrap|Line Numbers
  1. void broadSort (int array1 [][5], int& startRows, int& startCols, bool sortType)
  2. {
  3.  
  4.     for (startRows=0;startRows<5;startRows++)
  5.     {
  6.     for (startCols=0;startCols<5;startCols++)
  7.     valueSearch (array1, startRows, startCols, sortType);
  8.     }
  9. }
  10.  
  11. void valueSearch (int array1 [][5], int startRows, int startCols, bool sortType)
  12. {
  13.  
  14. switch (sortType)
  15. {
  16. case false: 
  17.     for (int iSmallerRows=startRows;iSmallerRows<5;iSmallerRows++)
  18.     {
  19.     for (int iSmallerCols=startCols;iSmallerCols<5;iSmallerCols++)
  20.     {
  21.     if (array1 [iSmallerRows][iSmallerCols]>array1[startRows][startCols])
  22.     valueChange (array1, startRows, startCols, iSmallerRows , iSmallerCols );
  23.     }
  24.     }
  25. break;
  26. case true:
  27.     for (int iLargerRows=startRows;iLargerRows<5;iLargerRows++)
  28.     {
  29.     for (int iLargerCols=startCols;iLargerCols<5;iLargerCols++)
  30.     {
  31.     if (array1 [iLargerRows][iLargerCols]<array1[startRows][startCols])
  32.     valueChange (array1, iLargerRows, iLargerCols, startRows, startCols);
  33.     }
  34.     }
  35.     }
  36. }
  37.  
  38.  
  39. void valueChange (int array1 [][5], int vR1, int vC1, int vR2, int vC2)
  40. {
  41. int temp=0;
  42. temp=array1[vR1][vC1];
  43. array1[vR1][vC1]=array1[vR2][vC2];
  44. array1[vR2][vC2]=temp;
  45. }
Feb 25 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.