我正在编写一个基于调查的程序,其中存在一种用于比较人与用户的兼容性的方法。
我有一个快速排序算法,可以从最大到最不高效地对整数进行排序,但是我需要对一个人数组和一个整数数组(这是人们的兼容性数组)进行排序。如何对人员数组进行排序,以使其与各自的整数兼容性匹配?
快速排序算法:
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);
}
}
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);
}
}