0% found this document useful (0 votes)
35 views1 page

Quick Sort Algorithm in C++

Uploaded by

munirre7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views1 page

Quick Sort Algorithm in C++

Uploaded by

munirre7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//quick sort

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int partition(vector<int>& arr, int low, int high) {

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j < high; ++j) {

if (arr[j] < pivot) {


i++;
swap(arr[i], arr[j]);
}
}

swap(arr[i + 1], arr[high]);


return (i + 1);
}

void quicksort(vector<int>& arr, int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}

int main() {
vector<int> data;
int n, temp;

cin >> n;

for (int i = 0; i < n; ++i) {


cin >> temp;
data.push_back(temp);
}

quicksort(data, 0, n - 1);

for (int val : data) {


cout << val << " ";
}
cout << endl;

return 0;
}

You might also like