Thread: Sorting elements in 2d array by their index

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    2

    Question Sorting elements in 2d array by their index

    Hi!

    I need to sort the elements in a 2d array by their index (starting from 1) for example:

    Code:
    1  5  3 
    4  7  8
    4 10  2
    10 is the biggest element and its index is 32, after 10 comes 8 with index 23 etc etc... Please help?
    EDIT: If you can give me examples for two orders ... By descending and ascending order... Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your description isn't clear. what do you mean buy 'index'? if each element has a value and an index, your example should show that. and it would help to show the initial state and the final sorted state that you are looking for.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    2
    For the given matrix in my example number 1 is on position 11 number ,5 - position 12 etc etc, so... i need to sort the elements in this style:

    32 (that's 10), 23(that's 8), 22 (that's 7) ... etc etc until it comes to the smallest element. Understand now?

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    nope

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    You need to get used to the idea that arrays in C/C++ are zero based.

    Next you have a starting matrix of:
    Code:
    1  5  3
    4  7  8
    4 10  2
    Which you wish to sort with the result being:
    Code:
    10 8  7
    5  4  4
    3  2  1
    Is that correct?

    What have you tried?

    Jim

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It seems the OP wants to output the (1-based) indices of the matrix elements in order of the element value. So this input:
    Code:
    1  5  3 
    4  7  8
    4 10  2
    would give this output (assuming descending order is wanted) :
    Code:
    32  (row number, column number)
    23
    22
    12
    21  (not sure how to handle equal ...)
    31  (... element values (both these are 4))
    13
    33
    11

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2013, 08:17 AM
  2. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  3. [C] Sorting an array and preserving the original index
    By frodo_jedi in forum C Programming
    Replies: 10
    Last Post: 04-06-2009, 06:51 AM
  4. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  5. accessing user entered array elements by index?
    By richdb in forum C Programming
    Replies: 10
    Last Post: 04-08-2006, 11:10 AM