Reputation: 49
I am writing a survey based program in which there is a method for comparing the compatibilities of people with that of the user.
I have a quick sort algorithm that sorts integers from greatest to least efficiently, however I need to sort an array of people alongside the array of integers (which is an array of people's compatibilities). How do I sort the array of people so that they match with their respective integer compatibility?
quick sort algorithm:
public static void qsort(People [] ppl, Integer [] a, Integer si, Integer ei){
//base case
if(ei<=si || si>=ei){}
else{
Integer pivot = a[si];
Integer i = si+1; Integer tmp;
//partition array
for(Integer j = si+1; j<= ei; j++){
if(pivot < a[j]){
tmp = a[j];
a[j] = a[i];
a[i] = tmp;
i++;
}
}
//put pivot in right position
a[si] = a[i-1];
a[i-1] = pivot;
//call qsort on right and left sides of pivot
qsort(a, si, i-2);
qsort(a, i, ei);
}
}
Upvotes: 0
Views: 28
Reputation: 131
public static void qsort(People [] ppl, Integer [] a, Integer si, Integer ei){
//base case
if(ei<=si || si>=ei){}
else{
Integer pivot = a[si];
People pPivot = ppl[si];
Integer i = si+1; Integer tmp; People pTmp;
//partition array
for(Integer j = si+1; j<= ei; j++){
if(pivot < a[j]){
tmp = a[j];
pTmp = ppl[j];
a[j] = a[i];
ppl[j]=ppl[i];
a[i] = tmp;
ppl[i]=pTmp;
i++;
}
}
//put pivot in right position
a[si] = a[i-1];
ppl[si] = ppl[i-1];
a[i-1] = pivot;
ppl[i-1]=pPivot;
//call qsort on right and left sides of pivot
qsort(ppl, a, si, i-2);
qsort(ppl, a, i, ei);
}
}
Upvotes: 2