私は配列リストのクイックソートを書きましたが、現状ではロジックは健全なようです。私が抱えている問題は、要素の交換にあります。起こっているように見えるのは、要素を交換するのではなく、メソッドが既存の要素を交換する要素に置き換えていることです。実行例は [幸せ、りんご、食べる、食べ物] のようなリストから始まり、並べ替えが実行されると [幸せ、幸せ、幸せ、食べ物] になります。私の間違いは単純だと確信していますが、あまりにも長い間それを見つめていたので、新しい目が必要です. これまでの私のコードは次のとおりです。前もって感謝します!
String pivot = list.get(0); // Choose the first element as the pivot
int low = first + 1; // Index for forward search
int high = last; // Index for backward search
while (high > low)
{ // Search forward from left
while (low <= high && list.get(low).compareTo(pivot) <= 0)
{
low++;
}
// Search backward from right
while (low <= high && list.get(high).compareTo(pivot) > 0)
{
high--;
}
// Swap two elements in the list
if (high > low)
{
String temp = list.get(high);
list.set(high,list.get(low));
list.set(low,temp);
}
}
while (high > first && list.get(high).compareTo(pivot) <= 0)
{
high--;
}
// Swap pivot with list[high]
if (list.get(high).compareTo(pivot) < 0)
{
list.set(first, list.get(high));
list.set(high,pivot);
return high;
}
else
{
return first;
}
}