1
我想返回字符比較的數目。在while()循環中,我比較字符的索引並更新計數器。我的問題是,這樣做是對的還是我必須比較這些角色本身。我認爲比較索引和更新計數器與比較字符本身是一樣的。任何想法?求解算法中的幫助
需要幫助。
以下是該算法的代碼。
// Sort an array of strings using quick sort
// Always pivot on the first element
// Return the number of CHARACTER comparisons
public int stringQuickSort(ComparableByte[][] strings) {
Counter nCompares = new Counter();
sortStringQuickSort(strings, 0, strings.length-1, 0, nCompares, false);
return nCompares.value;
}
public void sortStringQuickSort(ComparableByte[][] strings, int lo, int hi, int d, Counter nCompares, boolean switchToOtherAlgorithm){
if(!switchToOtherAlgorithm){
if(hi <= lo)
return;
}else if(hi <= lo+10){
stringInsertionSort(strings);
return;
}
int lt = lo, gt = hi;
int v = characterAt(ComparableByte.toString(strings[lo]), d);
int i = lo+1;
while(i <= gt){
int t = characterAt(ComparableByte.toString(strings[i]), d);
nCompares.value++;
if (t < v){
swapTwoComparableByteElements(strings, lt++, i++);
nCompares.value++;
}
else if(t > v){
swapTwoComparableByteElements(strings, i, gt--);
}
else
i++;
}
sortStringQuickSort(strings, lo, lt-1, d, nCompares, switchToOtherAlgorithm);
if(v >= 0){
sortStringQuickSort(strings, lt, gt, d+1, nCompares, switchToOtherAlgorithm);
}
sortStringQuickSort(strings, gt+1, hi, d, nCompares, switchToOtherAlgorithm);
}
感謝您的幫助