Panašus į Sujungti Rūšiuoti algoritmas, greitojo rūšiavimo algoritmas yra „Skaldyk ir valdyk“ algoritmas. Iš pradžių jis pasirenka elementą kaip sukimosi elementą ir padalija nurodytą masyvą aplink pasirinktą sukimosi tašką. Yra daug skirtingų „quickSort“ versijų, kurios parenkamos įvairiais būdais.
- Visada pasirinkite pirmąjį elementą kaip sukimosi tašką (įgyvendinta toliau).
- Visada pasirinkite paskutinį elementą kaip sukimąsi.
- Pasirinkite atsitiktinį elementą kaip sukimąsi.
- Pasirinkite medianą kaip sukimąsi.
Pagrindinis „QuickSort“ procesas yra skaidinio () procesas. Partition() funkcijos tikslas yra gauti masyvą ir masyvo elementą x kaip sukimosi tašką, įdėti x į teisingą vietą surūšiuotame masyve ir tada įdėti visus mažesnius elementus (mažesnius nei x) prieš x ir įdėti visi didesni elementai (didesni už x) po x. Visa tai turėtų būti daroma tiesiniu laiku, ty Big O(n) .
Rekursinės QuickSort funkcijos pseudo kodas:
/* low -->Pradinis indeksas, aukštas --> pabaigos indeksas */ quickSort(arr[], žemas, aukštas) { if (low Method-1 : CPP // C++ Greitojo rūšiavimo algoritmo įgyvendinimas. #include naudojant vardų erdvę std; int skaidinys (int arr[], int pradžia, int pabaiga) { int pivot = arr[pradžia] int count = 0 for (int i = pradžia + 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]>suktis) { j--; } if (i pivotIndex) { swap(arr[i++], arr[j--]); } } return pivotIndex; } void quickSort(int arr[], int pradžia, int pabaiga) { // pagrindinės raidės if (pradžia>= pabaiga) return; // masyvo skaidymas int p = partition(arr, start, end); // Kairiosios dalies rūšiavimas quickSort(arr, start, p - 1); // Dešinės dalies rūšiavimas quickSort(arr, p + 1, end); } int main() { int arr[] = { 9, 3, 4, 2, 1, 8 }; int n = 6; QuickSort(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 -> didesnis nei pivotElement . Laiko sudėtingumas: θ(nlogn). Erdvės sudėtingumas: O(n). C++ // pridėjo Manish Sharma #include naudojant vardų erdvę std; int skirsnis(int* arr, int pradžia, int pabaiga) { // darant prielaidą, kad paskutinis elementas yra pivotElement int index = 0, pivotElement = arr[end], pivotIndex; int* temp = naujas int[pabaiga - pradžia + 1]; // sudaromas masyvas, kurio dydis lygus esamam skaidinio diapazonui... 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]; indeksas++; } } // visi elementai dabar laikinajame masyve yra išdėstyti tvarka : // kairieji elementai yra mažesni už pivotElement, o labiausiai dešinieji elementai yra didesni už pivotElement index = 0; for (int i = pradžia; 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!>