mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Added breif comments on functions
This commit is contained in:
parent
69f9210c17
commit
c1764bd197
@ -26,6 +26,12 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/* 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 partition(int arr[], int low, int high) {
|
||||||
int pivot = arr[high]; // taking the last element as pivot
|
int pivot = arr[high]; // taking the last element as pivot
|
||||||
int i = (low - 1); // Index of smaller element
|
int i = (low - 1); // Index of smaller element
|
||||||
@ -46,6 +52,10 @@ int partition(int arr[], int low, int high) {
|
|||||||
return (i + 1);
|
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) {
|
void quickSort(int arr[], int low, int high) {
|
||||||
if (low < high) {
|
if (low < high) {
|
||||||
int p = partition(arr, low, high);
|
int p = partition(arr, low, high);
|
||||||
@ -54,6 +64,7 @@ void quickSort(int arr[], int low, int high) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prints the array after sorting
|
||||||
void show(int arr[], int size) {
|
void show(int arr[], int size) {
|
||||||
for (int i = 0; i < size; i++)
|
for (int i = 0; i < size; i++)
|
||||||
std::cout << arr[i] << " ";
|
std::cout << arr[i] << " ";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user