Thread: Selection Sort on a 2D Array

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    36

    Selection Sort on a 2D Array

    Hello-
    I need to do a selection sort on two different 2d arrays, sorting by the last column of one of them (thirdarray). The first column of thirdarray is the effective column size for the first (morearray) array. I am so confused. I cant find any examples to help me. Here is what I have so far:



    Code:
    int selectionsort(int morearray[][5], int thirdarray[][3],int total){
           int temp[5], index_of_largest;
           for ( ;total>0;total--){
               index_of_largest=0;
              for (int index=1;index<=total;index++){
                  if (thirdarray[index][2]>thirdarray[index+1][2]){
                     index_of_largest=index;
                  }
                  if (total!=index_of_largest){
                     for (int f=0;f<thirdarray[index][0];f++){
                     temp[index]=morearray[index][total];
                     morearray[index][total]=morearray[index][index_of_largest];
                     morearray[index][index_of_largest]=temp[index];
                     }
                     }
              }
          }
           return(twoarray[total][4]);
           return(morearray[total][5]);

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to do a selection sort huh? Must be homework...

    You really can't find any examples of one of the most common sorting algorithms? Did you try using google?

    Can you tell us what the problem is so that every person who reads this doesn't have to create a project and provide sample data and compile and run the program, please? It makes sense for you to do this once rather than expecting everyone else to do it.

    Why would you think that you need to return anything? Is it supposed to sort the data and leave it sorted where it was, or is it supposed to make a sorted copy and leave the original arrays unchanged?
    The second return can of course never be reached.

    There are several buffer overruns in that code. E.g. An array of [5] elements has valid indexes of 0, 1, 2, 3 and 4, count those and you'll see there are 5 of them. So if you tried using index 5 that would be a sixth element, but you told it there were only 5.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. sort 2D array
    By astro_not in forum C Programming
    Replies: 2
    Last Post: 04-13-2003, 02:55 AM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM