Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- void quickSort(int arr[], int size);
- void quickSort(int arr[], int leftIndex, int rightIndex);
- int quickSortPartition(int arr[], int leftIndex, int rightIndex);
- int main(){
- int arr[10] = {15, 6, 8, -1, 0, 23, 1, 42, 32, 44};
- quickSort(arr, 10);
- for(auto i : arr)
- cout << i << " ";
- }
- void quickSort(int arr[], int size){
- quickSort(arr, 0, size - 1);
- }
- void quickSort(int arr[], int leftIndex, int rightIndex){
- if(leftIndex < rightIndex){
- int partitionIndex = quickSortPartition(arr, leftIndex, rightIndex);
- quickSort(arr, leftIndex, partitionIndex - 1);
- quickSort(arr, partitionIndex + 1, rightIndex);
- }
- }
- int quickSortPartition(int arr[], int leftIndex, int rightIndex){
- int p = arr[rightIndex];
- int smallIndex = leftIndex;
- for(int i = leftIndex; i < rightIndex; i++){
- if(arr[i] < p){
- swap(arr[smallIndex++], arr[i]);
- }
- }
- swap(arr[smallIndex], arr[rightIndex]);
- return smallIndex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement