how to sort a 2D array

This is a discussion on how to sort a 2D array within the C Programming forums, part of the General Programming Boards category; hi there, i am trying to sort a 2d array with no luck... this is what i have for a ...

  1. #1
    Unregistered
    Guest

    how to sort a 2D array

    hi there,
    i am trying to sort a 2d array with no luck... this is what i have
    for a single array.
    any help would be appreciated.

    int array[7] = {'6','3','1','4','2','5','7'};
    for(i=0;i<7;i++)

    for(j=0;j<7;j++)
    {
    if(array[j+1] < array[j])
    {
    temp = array[j];
    array[j] = array[j+1];
    array[j+1] = temp;

    }

    }

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    int arrays are not stored like what you did, you in fact put it as a character array because of the 's
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    granted.....but any pointers for me on the 2d array side of things.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some code that you have used to try and sort the 2d array, and we'll have a look at whats going on.

    Also, what type of array is it ? chars, ints or what?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    try this
    Code:
    int array[7] = {6,3,1,4,2,5,7}; 
    for(i=0;i<7;i++) 
    	for(j=0;j<7;j++) 
    	{ 
    		if(array[j+1] < array[j]) 
    		{ 
    			temp = array[j]; 
    			array[j] = array[j+1]; 
    			array[j+1] = temp; 
    
    		} 
    
    	}
    I think thats right...
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Question bubble sort for 2d array problems?

    i am using an int array. this is how i am trying to solve the problem, but to no avail.....

    int array[3][2] = {{3,6},{5,2},{1,4}};

    for(i=0;i<3;i++)

    for(i=0;i<3;i++)
    for(j=0;j<2;j++)
    {
    if(array[i+1][j+1] < array[i][j])
    {
    temp = array[i][j];
    array[i][j] = array[i+1][j+1];
    array[i+1][j+1] = temp;
    }
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > {{3,6},{5,2},{1,4}};
    Are you trying to get to

    {{1,4},{3,6},{5,2}};
    ie, the first number is the sort key, and you want to keep the associated second number with it?

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    no, all i want to do is sort the array, starting from the lowest up
    to the top.

    int array[3][2] = {{3,6},{5,2},{1,4}};

    so i want to finish with the 2d array like the following,

    int array[3][2] = {{1,2},{3,4},{5,6}};


    Sean.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Lynux-Penguin
    try this
    Code:
    int array[7] = {6,3,1,4,2,5,7}; 
    for(i=0;i<7;i++) 
    	for(j=0;j<7;j++) 
    	{ 
    		if(array[j+1] < array[j]) 
    		{ 
    			temp = array[j]; 
    			array[j] = array[j+1]; 
    			array[j+1] = temp; 
    
    		} 
    
    	}
    I think thats right...
    I think you've exceeded the array bounds when you did array[j+1] when j was 6.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    This should do
    Code:
    #include <stdio.h>
    
    #define N_COLS  2
    #define N_ROWS  3
    
    /* Sort a 2D array */
    /* Treat the array as a linear array, but in doing so, we */
    /* need to reconstuct the effective row and column indices */
    /* back into the 2D array */
    void sort ( int arr[][N_COLS], int size ) {
        int lim = size * N_COLS;
        int i, j;
        for ( i = lim-1 ; i >= 0 ; i-- ) {
            int ri = i / N_COLS;
            int ci = i % N_COLS;
            for ( j = 0 ; j < i; j++ ) {
                int rj = j / N_COLS;
                int cj = j % N_COLS;
                if ( arr[ri][ci] < arr[rj][cj] ) {
                    int temp    = arr[ri][ci];
                    arr[ri][ci] = arr[rj][cj];
                    arr[rj][cj] = temp;
                }
            }
        }
    }
    void print ( int arr[][N_COLS], int size ) {
        int r, c;
        for ( r = 0 ; r < size ; r++ ) {
            for ( c = 0 ; c < N_COLS ; c++ ) {
                printf( "%d ", arr[r][c] );
            }
            printf( "\n" );
        }
    }
    
    int main ( ) {
        int array[N_ROWS][N_COLS] = {{3,6},{5,2},{1,4}}; 
        printf( "Unsorted\n" );
        print( array, N_ROWS );
        sort( array, N_ROWS );
        printf( "\nsorted\n" );
        print( array, N_ROWS );
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:02 PM
  2. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 03:02 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM

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