[Help][Urgent]Selection sort a 2D array

This is a discussion on [Help][Urgent]Selection sort a 2D array within the C Programming forums, part of the General Programming Boards category; I need help with selection sorting a 2D array let's say i have an array like 1 2 3 4 ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    [Help][Urgent]Selection sort a 2D array

    I need help with selection sorting a 2D array
    let's say i have an array like

    1 2 3 4 //4 elements
    1 2 // 2 elements
    1 2 3 4 5 //5 elements
    1 2 3 //3 elements
    1 //1 element

    and i want to do a selection sort it in descending order which the row with 5 elements will come first then 4 then 3 and so on
    so that it would look like this

    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1

    here's my code

    Code:
    void selectionSortDescending(int list[MAX_ROW], int size){
        int    temp;
        int    walk;
        int    curr;
        int    large;  // index of the largest element
    
    
        for (walk = 0; walk < size - 1; walk++)
        {
            // Look for the largest value and find its location
            large = walk;
            for (curr = walk + 1; curr < size; curr++)
                if (list[curr] > list[large])
                    large = curr;
    
    
            // Exchange
            temp        = list[walk];
            list[walk]  = list[large];
            list[large] = temp;
    
    
        } // end of for: repeat the above two steps n-1 times
    
    
        return;
    
    
    }// selectionSortAscending
    please help

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not clear on where a row with one element, with a value of 7, would go in your sort.

    Looks like you're doing a sort based on two keys: the number of elements in the row with a value > 0, in descending value, and also each row is sorted in ascending value.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Quote Originally Posted by Adak View Post
    I'm not clear on where a row with one element, with a value of 7, would go in your sort.

    Looks like you're doing a sort based on two keys: the number of elements in the row with a value > 0, in descending value, and also each row is sorted in ascending value.
    i just put those numbers up there as example
    what i need to sort are the rows
    let's say i have a 2D array with 3 rows and each row with different number of elements
    row 1: 8 elements
    row 2: 3 elements
    row 3: 5 elements

    after selection sort, it would be
    row 1: 8 elements
    row 3
    row 2

    here's my call function but it wouldn't sort for some reason idk..
    Code:
    void sortList(int** table, int nrow){ //nrow is the size/number of rows
        int current;
    
    
        for(current = 0; current < nrow; current++)
        {
            selectionSortDescending(table[current] + 1, table[current][0]);
        }
        return;
    }//sortList
    this is my output
    Enter the number of rows: 4
    Enter the number of integer(s) in row 1: 5
    Enter the number of integer(s) in row 2: 2
    Enter the number of integer(s) in row 3: 3
    Enter the number of integer(s) in row 4: 8


    Unsorted:
    5: -44 -57 93 27 -49
    2: -93 -89
    3: -5 -21 -20
    8: -31 27 -85 12 52 40 -39 84


    Result:
    5: 93 27 -44 -49 -57
    2: -89 -93
    3: -5 -20 -21
    8: 84 52 40 27 12 -31 -39 -85


    Process returned 0 (0x0) execution time : 3.738 s
    Press any key to continue.
    the actual result should be
    8: 84 52 40 27 12 -31 -39 -85
    5: 93 27 -44 -49 -57
    3: -5 -20 -21
    2: -93 -89
    Last edited by pcshano; 02-13-2013 at 11:32 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > void sortList(int** table, int nrow)
    So where is the information telling you that one row has 8 elements, and another row has 3 elements?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Quote Originally Posted by Salem View Post
    > void sortList(int** table, int nrow)
    So where is the information telling you that one row has 8 elements, and another row has 3 elements?
    you initialize them from another function which i called it buildTable
    Code:
    int** buildTable (int* nrows){
        int rowNum;
        int colNum;
        int** table;
        int row;
    
    
        printf("Enter the number of rows: ");
        scanf("%d", &rowNum);
        while(rowNum < MIN_ROW || rowNum > MAX_ROW)
        {
            printf("Invalid, enter a number of row between 1 and 10: ");
            scanf("%d", &rowNum);
        }
        table = (int**) calloc(rowNum + 1, sizeof(int*));
        for(row = 0; row < rowNum; row++)
        {
            printf("Enter the number of integer(s) in row %d: ", row+1);
            scanf("%d", &colNum);
            while(colNum < MIN_ENTRY || colNum > MAX_ENTRY)
            {
                printf("Invalid, enter the number between 1 and 15: ");
                scanf("%d", &colNum);
            }
            table[row] = (int*) calloc(colNum + 1, sizeof(int));
            table[row][0] = colNum;
        }
        table[row] = NULL;
        *nrows = rowNum;
        return table;
    }//buildTable
    and another function to fill in random integers to each row

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    i figured out what my problem is but i can't figure out the way to fix it
    the problem is in my code, instead of sorting in descending from row with the most elements to the row with the least element(s)
    it would sort the elements in each row instead...

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by pcshano View Post
    the problem is in my code, instead of sorting in descending from row with the most elements to the row with the least element(s)
    it would sort the elements in each row instead...
    That's because your sort function only takes an one-dimensional array:
    Code:
    void selectionSortDescending(int list[MAX_ROW], int size){
    Bye, Andreas

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To put the table in order according to the number of elements in the row, you don't need to sort the table itself.

    Make an int array with one element per row of the 2D table. Assign a value to array[i] equal to the number of elements in table[i][]. Then sort the array, instead of the table[i], in descending order. Now arrange the rows in table[][], according to the numbers in array[], and you'll be set.

    Let's say the array[] order is 3,0,2,1. Meaning that table[row3] has the most elements, and table[row1], has the fewest. So in a loop, swap row 0, with table[row3], table[1] with table[0], table[2] stays where it is, and table[row1], becomes table[row3].

    The number of elements in the rows of table[i][] now match the value of array[i], and your sort of the table rows by the number of elements, is done.

    Note: when you learn to sort through an index or pointer array, you'll avoid all this swapping, but it's confusing to beginners, so I'm not discussing it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-06-2009, 12:27 PM
  2. Selection Sort on a 2D Array
    By veronicak5678 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2007, 05:37 PM
  3. using selection sort with a 2D char array
    By kristentx in forum C++ Programming
    Replies: 12
    Last Post: 04-08-2006, 08:45 AM
  4. HELP with file input with array, selection sort...
    By MyglyMP2 in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2005, 11:24 PM
  5. Selection Sort on 2-D character array
    By TankCDR in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2003, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21