mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-12 23:15:52 +08:00
formatting source-code for fd69530515
This commit is contained in:
@@ -25,48 +25,48 @@
|
||||
#include <iostream>
|
||||
|
||||
namespace sorting {
|
||||
/**
|
||||
* This function takes last element as pivot, places
|
||||
* the pivot element at its correct position in sorted
|
||||
* array, and places all smaller (smaller than pivot)
|
||||
* to left of pivot and all greater elements to right
|
||||
* of pivot
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* This function takes last element as pivot, places
|
||||
* the pivot element at its correct position in sorted
|
||||
* array, and places all smaller (smaller than pivot)
|
||||
* to left of pivot and all greater elements to right
|
||||
* of pivot
|
||||
*
|
||||
*/
|
||||
|
||||
int partition(int arr[], int low, int high) {
|
||||
int pivot = arr[high]; // taking the last element as pivot
|
||||
int i = (low - 1); // Index of smaller element
|
||||
int partition(int arr[], int low, int high) {
|
||||
int pivot = arr[high]; // taking the last element as pivot
|
||||
int i = (low - 1); // Index of smaller element
|
||||
|
||||
for (int j = low; j < high; j++) {
|
||||
// If current element is smaller than or
|
||||
// equal to pivot
|
||||
if (arr[j] <= pivot) {
|
||||
i++; // increment index of smaller element
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
int temp = arr[i + 1];
|
||||
arr[i + 1] = arr[high];
|
||||
arr[high] = temp;
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* The main function that implements QuickSort
|
||||
* arr[] --> Array to be sorted,
|
||||
* low --> Starting index,
|
||||
* high --> Ending index
|
||||
*/
|
||||
void quickSort(int arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int p = partition(arr, low, high);
|
||||
quickSort(arr, low, p - 1);
|
||||
quickSort(arr, p + 1, high);
|
||||
for (int j = low; j < high; j++) {
|
||||
// If current element is smaller than or
|
||||
// equal to pivot
|
||||
if (arr[j] <= pivot) {
|
||||
i++; // increment index of smaller element
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
int temp = arr[i + 1];
|
||||
arr[i + 1] = arr[high];
|
||||
arr[high] = temp;
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* The main function that implements QuickSort
|
||||
* arr[] --> Array to be sorted,
|
||||
* low --> Starting index,
|
||||
* high --> Ending index
|
||||
*/
|
||||
void quickSort(int arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int p = partition(arr, low, high);
|
||||
quickSort(arr, low, p - 1);
|
||||
quickSort(arr, p + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sorting
|
||||
|
||||
|
||||
Reference in New Issue
Block a user