logo

Program C++ za QuickSort

Podobno kot pri Spoji Razvrsti algoritma, je algoritem hitrega razvrščanja algoritem razdeli in vladaj. Sprva izbere element kot vrtilni element in dano matriko razdeli okoli izbranega vrtišča. Obstaja veliko različnih različic QuickSort, ki izberejo vrtišče na različne načine.

  1. Za vrtišče vedno izberite prvi element (implementirano spodaj).
  2. Za vrtišče vedno izberite zadnji element.
  3. Izberite naključni element kot vrtišče.
  4. Izberite mediano kot vrtišče.

Ključni proces v QuickSort je proces partition(). Namen funkcije partition() je sprejeti matriko in element x matrike kot vrtišče, postaviti x na pravilen položaj v razvrščeni matriki in nato postaviti vse manjše elemente (manjše od x) pred x ter postaviti vsi večji elementi (večji od x) za x. Vse to je treba narediti v linearnem času, tj. Big O(n).
Psevdo koda za rekurzivno funkcijo QuickSort:



/* low -->Začetni indeks, visoko --> Končni indeks */ quickSort(arr[], low, high) { if (low Metoda-1 : CPP // C++ Implementacija algoritma za hitro razvrščanje. #include using namespace std; int partition(int arr[], int start, int end) { int pivot = arr[start]; int count = 0; for (int i = start + 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--]);  } } vrni pivotIndex; } void quickSort(int arr[], int start, int end) { // osnovni primer if (start>= end) return;  // particioniranje matrike int p = particija(arr, začetek, konec);  // Razvrščanje levega dela quickSort(arr, start, p - 1);  // Razvrščanje desnega dela quickSort(arr, p + 1, end); } int main() { int arr[] = { 9, 3, 4, 2, 1, 8 };  int n = 6;  hitro razvrščanje (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 -> večji od pivotElement. Časovna kompleksnost : θ(nlogn). Kompleksnost prostora: O(n). C++ // dodal Manish Sharma #include using namespace std; int partition(int* arr, int start, int end) { // ob predpostavki, da je zadnji element pivotElement int index = 0, pivotElement = arr[end], pivotIndex;  int* temp = novo int[konec - začetek + 1]; // ustvarjanje matrike, katere velikost je enaka trenutnemu obsegu particije... 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[indeks] = arr[i];  indeks++;  } } // vsi elementi zdaj v začasni matriki so vrstni red : // skrajno levi elementi so manjši od pivotElement in skrajno desni elementi so večji od pivotElement index = 0;  za (int i = začetek; 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!>