Order the values of an int array only between two index numbers

This is a discussion on Order the values of an int array only between two index numbers within the C Programming forums, part of the General Programming Boards category; Well, i am using the quicksort, the code is modified by me, and the code does: i have 2 arrays, ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    7

    Order the values of an int array only between two index numbers

    Well, i am using the quicksort, the code is modified by me, and the code does:

    i have 2 arrays, one of doubles and other of integers, the doubles have the result of division of two numbers and the array with the ints have numbers that will refer to another array, but it is not important for this problem.

    An example:
    doubles array: (12,44;12,44;7,22; 12,44)
    ints array: ( 4 , 2 , 3 , 1 )

    now using my quicksort function i will organize the array of doubles from the higher to the lower, and the ints array will follow the order of the doubles array, the result is :

    doubles array: (12,44;12,44;12,44; 7,22)
    ints array: ( 4 , 2 , 1 , 1 )


    Well, when i have values in the doubles array that are equal, i need to check the ints array and order the ints values, but only the ints that in the doubles array are equals, the final result will be:

    doubles array: (12,44;12,44;12,44; 7,22)
    ints array: ( 1 , 2 , 4 , 1 )

    How i can order the ints array only between an interval that is given by the interval of numbers that are equals in the doubles array?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,581
    I suggest using an array of struct; but, you post is a little vague to me.
    So, I might not be following what you want.

    NOTE: I mean do NOT use parallel arrays use only one array.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    7
    yes it is true, but now i cant get back because it is almost done and i only need this function to complete the code.

    i am using this function to order the arrays of doubles, and the arrays of ints follow the moves of the arrays of doubles.

    Code:
    void quicksort(double *a, int n, int *array)
    {
    int i = 0;
    int j = n-1;
    double p = a[n/2];
    
    assert(n>0);
    
    do
    {
    while (a[i] > p)
    i++;
    
    while (p > a[j])
    j--;
    
    if (i<=j)
    exchange(a, i++, j--,array);
    }
    
    while (i<=j);
    
    if (j > 0)
    quicksort(a, j+1,array);
    
    if (i < n-1)
    quicksort(a+i, n-i,array+i);
    }
    i need to use this function again but now only between two numbers, not from the beginning of the array to the end.
    Have you an idea how i can make it with this function?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What you want is called a multi-key sort, where anytime the first keys are equal, then the sorting decision rests with the order of the second key. qsort() does this rather easily by modifying the compare function.

    It's a bit more complex to do it with this version of Quicksort, however. You can easily do this by adding a for loop after Quicksort is done sorting, however.

    Code:
    int sorted=0;
    while(!sorted) {
       sorted=1;
       for(i=starting index;i<ending index;i++) {
          if(array[i].key1==array[i+1].k1 && array[i].key2 > array[i+1].key2) {
              swap array[i] with array[i+1]
              sorted=0;
          }   
       }
    }
    Last edited by Adak; 03-16-2013 at 10:21 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Essentially this is the same as sorting an array of structs, where your comparison breaks ties by looking at a second member of the struct.
    The difference here is that you have a structure of arrays rather than an array of structures.
    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. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  2. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  3. Outputting an Array of 5 Numbers in Order
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2009, 07:20 AM
  4. getting two lowest values in array by index
    By danielC++ in forum C++ Programming
    Replies: 8
    Last Post: 03-09-2005, 06:30 PM
  5. organize values of array in ascending order
    By incognito in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2003, 09:55 PM

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