public static void quickSort(Integer[] arr, int low, int high)
{
//check for empty or null array
if (arr == null || arr.length == 0){
return;
}
if (low >= high){
return;
}
//Get the pivot element from the middle of the list
int middle = low + (high - low)/2;
int pivot = arr[middle];
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j)
{
//Check until all values on left side array are lower than pivot
while (arr[i] < pivot)
{
i++;
}
//Check until all values on left side array are greater than pivot
while (arr[j] > pivot)
{
j--;
}
//Now compare values from both side of lists to see if they need swapping
//After swapping move the iterator on both lists
//NUMBER (1)
if (i <= j)
{
swap (arr, i, j);
i++;
j--;
}
}
//Do same operation as above recursively to sort two sub arrays
//NUMBER (2)
if (low < j){
quickSort(arr, low, j);
}
//NUMBER (3)
if (high > i){
quickSort(arr, i, high);
}
}
私はクイックソートアルゴリズムの初心者です。誰かが条件の目的が何であるか教えてもらえますか?すなわち、上記のクイックソートアルゴリズムで数字(1)、数字(2)、数字(3)ならば?クイックソートはいくつかの条件を削除します
Number(1)の場合、私はjよりも小さいか等しいと考えられるので、スワップはただ実行する必要があります。
数字(2)と数字(3)についても同様です。 i
とj
インデックス位置を計算するときに、2つの元for
のループだけで、そう(左と右から)交換する権利要素に対してこの条件をチェック - 私はあなたに
を持っています。ありがとう – Desmond
実際には、現在書かれているように、 'i <= j'は常に' NUMBER(1) 'に当てはまります。私はこのアルゴリズムが正しく実装されているとは思わない。 – khelwood