Podobny do Sortowanie przez scalanie algorytmu, algorytm szybkiego sortowania jest algorytmem dziel i zwyciężaj. Początkowo wybiera element jako element obrotowy i dzieli podaną tablicę wokół wybranego elementu obrotowego. Istnieje wiele różnych wersji narzędzia QuickSort, które wybierają przestaw na różne sposoby.
- Zawsze wybieraj pierwszy element jako oś (zaimplementowano poniżej).
- Zawsze wybieraj ostatni element jako oś obrotu.
- Wybierz losowy element jako oś obrotu.
- Wybierz medianę jako oś obrotu.
Kluczowym procesem w QuickSort jest proces partycji(). Celem funkcji partycji() jest pobranie tablicy i elementu x tablicy jako elementu przestawnego, umieszczenie x we właściwej pozycji w posortowanej tablicy, a następnie umieszczenie wszystkich mniejszych elementów (mniejszych niż x) przed x i umieszczenie wszystkie większe elementy (większe niż x) po x. Wszystko to należy wykonać w czasie liniowym, czyli Big O(n).
Pseudokod dla rekurencyjnej funkcji QuickSort:
/* low -->Indeks początkowy, wysoki --> Indeks końcowy */ QuickSort(arr[], niski, wysoki) { if (low Metoda-1: CPP // Implementacja algorytmu szybkiego sortowania w C++. #include using namespace std; int partycja(int arr[], int początek, int koniec) { int przestaw = tablica [start]; int liczba = 0; for (int i = początek + 1; tj<= 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]>obrót) { j--; } if (i PivotIndex) { swap(arr[i++], arr[j--]); } } zwróć indeks obrotu; } void QuickSort(int arr[], int start, int end) { // przypadek podstawowy if (start>= end) return; // partycjonowanie tablicy int p = partycja(arr, start, end); // Sortowanie lewej części QuickSort(arr, start, p - 1); // Sortowanie właściwej części QuickSort(arr, p + 1, end); } int main() { int tablica[] = { 9, 3, 4, 2, 1, 8 }; int n = 6; szybkie sortowanie(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 ->element obrotowy -> większy niż element obrotowy. Złożoność czasowa: θ(nlogn). Złożoność przestrzenna: O(n). C++ // dodane przez Manisha Sharmę #include using namespace std; int partycja(int* arr, int start, int end) { // zakładając, że ostatni element jest elementem obrotowym int indeks = 0, element obrotowy = arr[koniec], indeks obrotowy; int* temp = nowy zamiar[koniec - początek + 1]; // utworzenie tablicy, której rozmiar jest równy zakresowi bieżącej partycji... 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]>element obrotowy) { temp[indeks] = tablica[i]; indeks++; } } // wszystkie elementy tablicy temp są teraz uporządkowane: // elementy skrajnie lewe są mniejsze od elementu obrotowego, a elementy skrajnie prawe są większe niż element obrotowy. indeks = 0; for (int i = start; tj<= 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!>