-1

I have three vectors

vector<string> a_words0(75000);
vector<string> a_words1(75000);
vector<string> a_words2(75000);

Consider below how the elements look like of the same row:

.
.
.
It's    raining  outside
The     warrior  is
It's    raining  heavily
It's    raining  outside
.
.
.

I was using the quicksort function to sort according to the first vector, and it's giving me the following results

.
.
.
It's    raining  outside
It's    raining  heavily
It's    raining  outside
The     warrior  is
.
.
.

The thing is I want the duplicates to be together like this:

.
.
.
It's    raining  outside
It's    raining  outside
It's    raining  heavily
The     warrior  is
.
.
.

This is my quick sort function:

void quicksort(vector<string> &arr0, vector<string> &arr1,vector<string> &arr2,int low, int high)
{
int i = low;
int j = high;
string y = "Z";
/* compare value */
string z = arr0[(low + high) / 2];

/* partition */
do {
    /* find member above ... */
    while(arr0[i] < z) i++;

    /* find element below ... */
    while(arr0[j] > z) j--;

    if(i <= j) {
        /* swap two elements */
        y = arr0[i];
        arr0[i] = arr0[j]; 
        arr0[j] = y;
        y = arr1[i];
        arr1[i] = arr1[j]; 
        arr1[j] = y;
        y = arr2[i];
        arr2[i] = arr2[j]; 
        arr2[j] = y;
        i++; 
        j--;

    }
} while(i <= j);


/* recurse */
if(low < j) 
    quicksort(arr0,arr1,arr2, low, j);

if(i < high) 
    quicksort(arr0,arr1,arr2, i, high); 
}
user1804029
  • 67
  • 1
  • 6

1 Answers1

1

You can change the compare to compare two elements of arr0 and if the same, then compare two elements of arr1 and if still the same, compare two elements of arr2. You might consider changing the name of Y to T (for temp), and then using X, Y, Z as the pivot points for arr0, arr1, and arr2.

Quick sort isn't stable, so you can't use multiple sort passes from least significant to most signifcant. You could use std::stable_sort() (normally implemented as a merge sort), and again use a custom compare that compares arr0, arr1, and arr2 if needed, or sort the data 3 times, by arr2 first, then arr1, then arr0.

rcgldr
  • 27,407
  • 3
  • 36
  • 61