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

  1. #1
    Registered User
    Join Date
    Sep 2012

    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?


  2. #2
    Registered User
    Join Date
    May 2009
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2012
    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.

    void quicksort(double *a, int n, int *array)
    int i = 0;
    int j = n-1;
    double p = a[n/2];
    while (a[i] > p)
    while (p > a[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
    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.

    int sorted=0;
    while(!sorted) {
       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]
    Last edited by Adak; 03-16-2013 at 10:21 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    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, 07:30 PM
  5. organize values of array in ascending order
    By incognito in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2003, 10:55 PM