Simile al Unisci ordinamento algoritmo, l'algoritmo Quick Sort è un algoritmo Divide and Conquer. Inizialmente seleziona un elemento come elemento pivot e suddivide l'array specificato attorno al pivot selezionato. Esistono molte versioni diverse di quickSort che scelgono il pivot in modi diversi.
- Scegli sempre il primo elemento come pivot (implementato di seguito).
- Scegli sempre l'ultimo elemento come pivot.
- Scegli un elemento casuale come pivot.
- Scegli la mediana come perno.
Il processo chiave in QuickSort è il processo Partition(). Lo scopo della funzione partizione() è ricevere un array e un elemento x dell'array come pivot, mettere x nella sua posizione corretta in un array ordinato e quindi mettere tutti gli elementi più piccoli (più piccoli di x) prima di x, e mettere tutti gli elementi maggiori (maggiori di x) dopo x. Tutto ciò dovrebbe essere fatto in tempo lineare, ovvero Big O(n) .
Pseudo codice per la funzione QuickSort ricorsiva:
/* low -->Indice iniziale, alto --> Indice finale */ quickSort(arr[], low, high) { if (low Metodo-1: CPP // Implementazione C++ dell'algoritmo di ordinamento rapido. #include using namespace std; int partizione(int arr[], int inizio, int fine) { int pivot = arr[inizio]; int conteggio = 0; for (int i = inizio + 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]>perno) { j--; } if (i pivotIndex) { swap(arr[i++], arr[j--]); } } restituisce indicepivot; } void quickSort(int arr[], int start, int end) { // caso base if (start>= end) return; // partizionamento dell'array int p = partizione(arr, start, end); // Ordinamento della parte sinistra quickSort(arr, start, p - 1); // Ordinamento della parte destra quickSort(arr, p + 1, end); } int main() { int arr[] = { 9, 3, 4, 2, 1, 8 }; intero n = 6; ordinamento veloce(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 -> maggiore di pivotElement . Complessità temporale: θ(nlogn). Complessità spaziale: O(n). C++ // aggiunto da Manish Sharma #include using namespace std; int partizione(int* arr, int start, int end) { // presupponendo l'ultimo elemento come pivotElement int indice = 0, pivotElement = arr[end], pivotIndex; int* temp = nuovo int[fine - inizio + 1]; // crea un array la cui dimensione è uguale all'intervallo di partizione corrente... 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]>elemento pivot) { temp[indice] = arr[i]; indice++; } } // tutti gli elementi ora nell'array temporaneo sono in ordine: // gli elementi più a sinistra sono minori di pivotElement e gli elementi più a destra sono maggiori di pivotElement indice = 0; for (int i = inizio; 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!>