Strange Sorting Method

This is a discussion on Strange Sorting Method within the C Programming forums, part of the General Programming Boards category; Hello, I have an array, declared as such: Code: double a[8]; a[0] through a[7] contain numbers that I would like ...

  1. #1
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94

    Question Strange Sorting Method

    Hello,

    I have an array, declared as such:
    Code:
    double a[8];
    a[0] through a[7] contain numbers that I would like to sort. Were this simply the issue, I'm sure I could find a function on Google. But what's important here is that I don't care about the final sorted array: I need a list of indecies from the original array in the order of the final array. For instance, say:
    Code:
    a[0]=5;
    a[1]=7;
    a[2]=2;
    Then I don't want my result to be "2,5,7", I want it to be 2,0,1: the original indicies.

    Does this make sense? How would I write this? I'm sure there must be a simple way that I'm missing.

    Thanks a lot for your help,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    one trick I have used is to use a second array of integers that contain the index values in the array you want to sort. When its time to swap elements, swap the integers in this second array, which keeps the original array in its original order.

    In the code below you should use const int to represent array sizes, not hard-code them like I did.
    Code:
    double a[8];
    int i,j;
    int index[8] = {0,1,2,3,4,5,6,7};
    
    // simple bubble-sort algorithm
    for( i = 0; i < 6; i++)
    {
       for( j = i+1; j < 7; j++)
       {
           if( a[index[i]] > a[index[j]])
           {
               int t = index[i];
               index[i] = index[j];
               index[j] = t;
           }
      }
    }
    
    // now list the array in sorted order
    for(i = 0; i < 7; i++)
       printf("%f  ", a[index[i]]);
    Last edited by Ancient Dragon; 11-12-2005 at 07:37 PM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by crepincdotcom
    Then I don't want my result to be "2,5,7", I want it to be 2,0,1: the original indicies.

    Does this make sense? How would I write this? I'm sure there must be a simple way that I'm missing.
    Something similar, FWIW, pretty much the same idea as AD.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Name of sorting method
    By noodles in forum Tech Board
    Replies: 6
    Last Post: 10-07-2006, 02:54 AM
  2. Sorting Method
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2005, 11:06 PM
  3. change sorting method using polymorphism
    By Forever82 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 02:21 PM
  4. Best sorting method to use
    By pelp in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2003, 12:30 AM
  5. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 05:21 PM

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