Last active
April 8, 2021 11:42
-
-
Save pvcodes-zz/55e57e110b2dc6353e29bb72f1e23a9b to your computer and use it in GitHub Desktop.
Quick Sort Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void quickSort(int *array, int low, int high) { | |
int i = low; | |
int j = high; | |
int pivot = array[(i + j) / 2]; | |
int temp; | |
while (i <= j) { | |
while (array[i] < pivot) | |
i++; | |
while (array[j] > pivot) | |
j--; | |
if (i <= j) { | |
temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
i++; | |
j--; | |
} | |
} | |
if (j > low) | |
quickSort(array, low, j); | |
if (i < high) | |
quickSort(array, i, high); | |
} | |
int main() { | |
int A[] = {8, 5, 7, 3, 2}; | |
for (auto i : A) { | |
cout << i << " "; | |
} | |
puts(""); | |
quickSort(A, 0, 5); | |
for (auto i : A) { | |
cout << i << " "; | |
} | |
puts(""); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment