0% found this document useful (0 votes)
39 views2 pages

Quick Sort Algorithm in C++

Uploaded by

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

Quick Sort Algorithm in C++

Uploaded by

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

QUICK SORT ALGORITHM

#include <iostream>

using namespace std;

int partition(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(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);

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

}
int main() {

int arr[] = {10, 7, 8, 9, 1, 5};

int size = sizeof(arr) / sizeof(arr[0]);

printArray(arr, size);

quickSort(arr, 0, size - 1);

printArray(arr, size);

return 0;

OUTPUT

You might also like