Similar cu Merge Sort algoritm, algoritmul de sortare rapidă este un algoritm Divide and Conquer. Inițial selectează un element ca element pivot și parțiază matricea dată în jurul pivotului ales. Există multe versiuni diferite de quickSort care aleg pivotul în moduri diferite.
- Alegeți întotdeauna primul element ca pivot (implementat mai jos).
- Alegeți întotdeauna ultimul element ca pivot.
- Alegeți un element aleatoriu ca pivot.
- Alegeți mediana ca pivot.
Procesul cheie în quickSort este procesul partition(). Scopul funcției partition() este de a primi o matrice și un element x al matricei ca pivot, pune x în poziția sa corectă într-o matrice sortată și apoi pune toate elementele mai mici (mai mici decât x) înainte de x și pune toate elementele mai mari (mai mari decât x) după x. Toate acestea ar trebui făcute în timp liniar, adică Big O(n) .
Pseudocod pentru funcția QuickSort recursivă:
/* low -->Index de pornire, mare --> Index de sfârșit */ quickSort(arr[], low, high) { if (low Metoda-1: CPP // C++ Implementarea algoritmului de sortare rapidă. #include using namespace std; int partition(int) arr[], int start, int end) { int pivot = arr[start] int count = 0 pentru (int i = start + 1; i<= 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]>pivot) { j--; } if (i pivotIndex) { swap(arr[i++], arr[j--]); } } returnează pivotIndex; } void quickSort(int arr[], int start, int end) { // caz de bază dacă (start>= end) return; // partiţionarea tabloului int p = partition(arr, start, end); // Sortarea părții din stânga quickSort(arr, start, p - 1); // Sortează partea dreaptă quickSort(arr, p + 1, end); } int main() { int arr[] = { 9, 3, 4, 2, 1, 8 }; int n = 6; sortare rapidă (arr, 0, n - 1); pentru (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 -> mai mare decât pivotElement . Complexitatea timpului: θ(nlogn). Complexitate spațială: O(n). C++ // adăugat de Manish Sharma #include folosind namespace std; int partition(int* arr, int start, int end) { // presupunând ultimul element ca pivotElement int index = 0, pivotElement = arr[end], pivotIndex; int* temp = new int[end - start + 1]; // creează o matrice a cărei dimensiune este egală cu domeniul de partiție curent... pentru (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]; index++; } } // toate elementele acum din matricea temp sunt ordonate: // elementele din stânga sunt mai mici decât pivotElement și elementele din dreapta sunt mai mari decât pivotElement index = 0; pentru (int i = start; 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!>