Created
December 8, 2019 20:08
-
-
Save melvincabatuan/7af6de09e76bdb1a50e6ecae917e2ea2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
const int MAX = 12; | |
void swap(int arr[], int i, int j){ | |
int temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
void display(int arr[]){ | |
for(int i = 0; i < MAX; ++i){ | |
std::cout<< arr[i] << " "; | |
} | |
std::cout<< std::endl; | |
} | |
void procedure(int arr[], int begin, int end){ | |
if (end <= begin){ | |
return; | |
} | |
int pivot = arr[begin]; | |
int less = begin + 1; | |
int more = end; | |
std::cout<< "less = " << less<<"; more = "<< more << " : "; | |
display(arr); | |
while(less < more){ | |
while( less <= more && arr[less] < pivot ) less++; | |
while( more >= less && arr[more] > pivot) more--; | |
if (less < more){ | |
swap(arr, less, more); | |
std::cout<< "less = " << less<<"; more = "<< more << " : "; | |
display(arr); | |
less++; | |
more--; | |
} | |
} | |
if (pivot > arr[more]){ | |
swap(arr, more, begin); | |
std::cout<< "less = " << less<<"; more = "<< more << " : "; | |
display(arr); | |
} | |
} | |
int main(){ | |
int arr[] = {13,19,9,5,12,8,7,4,21,2,6,11}; | |
procedure(arr, 0, MAX-1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment