に似ています マージソート アルゴリズムと同様に、クイック ソート アルゴリズムは分割統治アルゴリズムです。最初に要素をピボット要素として選択し、選択したピボットを中心に指定された配列を分割します。 QuickSort には、さまざまな方法でピボットを選択するさまざまなバージョンが存在します。
- 常に最初の要素をピボットとして選択します (以下で実装します)。
- 常に最後の要素をピボットとして選択します。
- ランダムな要素をピボットとして選択します。
- 中央値をピボットとして選択します。
QuickSort の主要なプロセスは、partition() プロセスです。 Partition() 関数の目的は、配列と配列の要素 x をピボットとして受け取り、ソートされた配列内の正しい位置に x を配置し、それからすべての小さい要素 (x より小さい) を x の前に配置し、 x 以降のすべての大きい要素 (x より大きい)。これはすべて線形時間、つまり Big O(n) で実行される必要があります。
再帰的 QuickSort 関数の疑似コード:
/* low -->開始インデックス, high --> 終了インデックス */quickSort(arr[], low, high) { if (low Method-1 : CPP // クイック ソート アルゴリズムの C++ 実装。 #include using namespace std; int Partition(int arr[], int start, int end) { int pivot = arr[start]; int count = 0 for (int i = start + 1;<= end; i++) { if (arr[i] <= pivot) count++; } // Giving pivot element its correct position int pivotIndex = start + count; swap(arr[pivotIndex], arr[start]); // Sorting left and right parts of the pivot element int i = start, j = end; while (i pivotIndex) { while (arr[i] <= pivot) { i++; } while (arr[j]>ピボット) { j--; if (i pivotIndex) { swap(arr[i++], arr[j--]); ピボットインデックスを返します。 } void QuickSort(int arr[], int start, int end) { // 基本ケース if (start>= end) return; // 配列を分割する int p = Partition(arr, start, end); // 左側の部分をソートしますquickSort(arr, start, p - 1); // 右側の部分をソートしますquickSort(arr, p + 1, end); int main() { int arr[] = { 9, 3, 4, 2, 1, 8 }; int n = 6; クイックソート(arr, 0, n - 1); for (int i = 0; i cout<< arr[i] << ' '; } return 0; } Output 1 2 3 4 8 9 Method-2 : This method’s space complexity is O(n). As we will take an extra array in partition function like in merge function of merge sort . Algorithm explanation and steps of partition function: Make a new array of size equal to given array. push all the smaller elements than pivotElement to the new array. Push pivotElement to new array now. finally, push all the greater elements than pivotElement to the new array. Now, copy the new array to the original array. Store the index of the pivotElement from the original array. Return this index. After this, all the elements in the original array are in the order : smaller than pivotElement ->pivotElement -> pivotElement より大きい。時間計算量: θ(nlogn)。空間の複雑さ : O(n)。 C++ // 名前空間 std を使用して Manish Sharma #include によって追加されました。 int Partition(int* arr, int start, int end) { // 最後の要素を pivotElement と仮定します int Index = 0, pivotElement = arr[end], pivotIndex; int* temp = new int[end - start + 1]; // 現在のパーティション範囲と同じサイズの配列を作成します... for (int i = start; i<= end; i++) // pushing all the elements in temp which are smaller than pivotElement { if(arr[i] { temp[index] = arr[i]; index++; } } temp[index] = pivotElement; // pushing pivotElement in temp index++; for (int i = start; i // pushing all the elements in temp which are greater than pivotElement { if(arr[i]>pivotElement) { temp[index] = arr[i]; インデックス++; } } // 一時配列内のすべての要素は order : // 左端の要素は pivotElement より小さく、右端の要素は pivotElement より大きくなります。 for (int i = 開始; i<= end; i++) // copying all the elements to original array i.e arr { if(arr[i] == pivotElement) { // for getting pivot index in the original array. // we need the pivotIndex value in the original and not in the temp array pivotIndex = i; } arr[i] = temp[index]; index++; } return pivotIndex; // returning pivotIndex } void quickSort(int* arr, int start, int end) { if(start { int partitionIndex = partition(arr, start, end); // for getting partition quickSort(arr, start, partitionIndex - 1); // sorting left side array quickSort(arr, partitionIndex + 1, end); // sorting right side array } return; } int main() { int size = 9; int arr[size] = {5, 12, 7, 1, 13, 2 ,23, 11, 18}; cout << 'Unsorted array : '; for (int i = 0; i { cout << arr[i] << ' '; } printf(' '); quickSort(arr, 0, size - 1); cout << 'Sorted array : '; for (int i = 0; i { cout << arr[i] << ' '; } return 0; } Output Unsorted array : 5 12 7 1 13 2 23 11 18 Sorted array : 1 2 5 7 11 12 13 18 23 Please refer complete article on QuickSort for more details!>