Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions sorting/quick_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <cstdlib>
#include <iostream>
#include <vector>

namespace sorting {
/**
Expand All @@ -34,7 +35,7 @@ namespace sorting {
*
*/

int partition(int arr[], int low, int high) {
int partition(std::vector<int> &arr, int low, int high) {
int pivot = arr[high]; // taking the last element as pivot
int i = (low - 1); // Index of smaller element

Expand All @@ -60,7 +61,7 @@ int partition(int arr[], int low, int high) {
* low --> Starting index,
* high --> Ending index
*/
void quickSort(int arr[], int low, int high) {
void quickSort(std::vector<int> &arr, int low, int high) {
if (low < high) {
int p = partition(arr, low, high);
quickSort(arr, low, p - 1);
Expand All @@ -73,29 +74,28 @@ void quickSort(int arr[], int low, int high) {
using sorting::quickSort;

// prints the array after sorting
void show(int arr[], int size) {
void show(const std::vector<int> &arr, int size) {
for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
std::cout << "\n";
}

/** Driver program to test above functions */
int main() {
int size;
int size = 0;
std::cout << "\nEnter the number of elements : ";

std::cin >> size;

int *arr = new int[size];
std::vector<int> arr(size);

std::cout << "\nEnter the unsorted elements : ";

for (int i = 0; i < size; ++i) {
std::cout << "\n";
std::cin >> arr[i];
}
quickSort(arr, 0, size);
quickSort(arr, 0, size - 1);
std::cout << "Sorted array\n";
show(arr, size);
delete[] arr;
return 0;
}