logo

Programa C++ per a QuickSort

Similar a la Fusionar Ordenar algorisme, l'algoritme d'ordenació ràpida és un algorisme Divide and Conquer. Inicialment selecciona un element com a element pivot i divideix la matriu donada al voltant del pivot triat. Hi ha moltes versions diferents de quickSort que trien el pivot de diferents maneres.

  1. Trieu sempre el primer element com a pivot (implementat a continuació).
  2. Trieu sempre l'últim element com a pivot.
  3. Trieu un element aleatori com a pivot.
  4. Trieu la mitjana com a pivot.

El procés clau a quickSort és el procés partition(). L'objectiu de la funció partition() és rebre una matriu i un element x de la matriu com a pivot, posar x a la seva posició correcta en una matriu ordenada i després posar tots els elements més petits (menys que x) abans de x i posar tots els elements més grans (superiors a x) després de x. Tot això s'ha de fer en temps lineal, és a dir, Big O(n).
Pseudocodi per a la funció QuickSort recursiva:



/* low -->Índex inicial, alt --> Índex final */ quickSort(arr[], low, high) { if (baix Mètode-1: CPP // C++ Implementació de l'algoritme d'ordenació ràpida. #include using namespace std; int partition(int arr[], int inici, int final) { int pivot = arr[inici] int count = 0 per (int i = inici + 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--]);  } } retornar pivotIndex; } void quickSort(int arr[], int inici, int final) { // cas base si (inici>= final) retorn;  // particionant la matriu int p = partition(arr, start, end);  // Ordenant la part esquerra quickSort(arr, start, p - 1);  // Ordenant la part correcta quickSort(arr, p + 1, end); } int main() { int arr[] = { 9, 3, 4, 2, 1, 8 };  int n = 6;  quickSort(arr, 0, n - 1);  per (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 -> més gran que pivotElement . Complexitat temporal: θ(nlogn). Complexitat espacial: O(n). C++ // afegit per Manish Sharma #include using namespace std; int partition(int* arr, int start, int end) { // assumint l'últim element com a pivotElement int index = 0, pivotElement = arr[end], pivotIndex;  int* temp = nou int[final - inici + 1]; // fent una matriu la mida de la qual sigui igual a l'interval de partició actual... per (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[índex] = arr[i];  índex++;  } } // tots els elements ara a la matriu temporal són d'ordre: // els elements més a l'esquerra són menors que pivotElement i els elements més a la dreta són més grans que pivotElement index = 0;  per (int i = inici; 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!>