Līdzīgi kā Sapludināt Kārtot algoritms, ātrās kārtošanas algoritms ir sadali un iekaro algoritms. Sākotnēji tas atlasa elementu kā šarnīra elementu un sadala doto masīvu ap izvēlēto pagrieziena punktu. Ir daudz dažādu QuickSort versiju, kas izvēlas pagriezienu dažādos veidos.
- Vienmēr izvēlieties pirmo elementu kā šarnīra elementu (ieviests tālāk).
- Vienmēr izvēlieties pēdējo elementu kā pagrieziena punktu.
- Izvēlieties izlases elementu kā rakursu.
- Izvēlieties vidējo kā rakursu.
QuickSort galvenais process ir partition() process. Funkcijas partition() mērķis ir saņemt masīvu un masīva elementu x kā šarnīra punktu, sakārtotajā masīvā ievietot x pareizajā vietā un pēc tam visus mazākos elementus (mazākus par x) ievietot pirms x un ievietot visi lielākie elementi (lielāki par x) aiz x. Tas viss jādara lineārā laikā, t.i., lielajā O(n) .
Pseido kods rekursīvai QuickSort funkcijai:
/* low -->Sākuma indekss, augsts --> beigu indekss */ quickSort(arr[], zems, augsts) { if (zems, 1. metode : CPP // C++ Ātrās kārtošanas algoritma ieviešana. #include izmantojot nosaukumvietas std; int partition(int) arr[], int sākums, int beigas) { int pivot = arr[sākums] int count = 0 for (int i = sākums + 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]>pagrieziens) { j--; } if (i pivotIndex) { swap(arr[i++], arr[j--]); } } return pivotIndex; } void quickSort(int arr[], int sākums, int beigas) { // bāzes reģistrs if (sākums>= beigas) return; // masīva sadalīšana int p = partition(arr, start, end); // Kreisās daļas kārtošana quickSort(arr, start, p - 1); // Labās daļas kārtošana 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 -> lielāks par pivotElement . Laika sarežģītība: θ(nlogn). Telpas sarežģītība: O(n). C++ // pievienoja Manish Sharma #include izmantojot namespace std; int partition(int* arr, int sākums, int beigas) { // pieņemot, ka pēdējais elements ir pivotElement int index = 0, pivotElement = arr[end], pivotIndex; int* temp = jauns int[beigas - sākums + 1]; // izveidojot masīvu, kura izmērs ir vienāds ar pašreizējo nodalījuma diapazonu... for (int i = sākums; 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[indekss] = arr[i]; indekss++; } } // visi elementi pašlaik pagaidu masīvā ir sakārtoti secībā: // galējie kreisie elementi ir mazāki par pivotElement un galēji labējie elementi ir lielāki par pivotElement index = 0; for (int i = sākums; 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!>