From c1764bd1970031f153fa0f5f2456565706b31d94 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 27 May 2020 18:43:57 +0530 Subject: [PATCH] Added breif comments on functions --- sorting/quick_sort.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 5a568014d..965d117cc 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -26,6 +26,12 @@ #include #include +/* 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 @@ -46,6 +52,10 @@ int partition(int arr[], int low, int high) { 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); @@ -54,6 +64,7 @@ void quickSort(int arr[], int low, int high) { } } +// prints the array after sorting void show(int arr[], int size) { for (int i = 0; i < size; i++) std::cout << arr[i] << " ";