Created
July 19, 2022 15:31
-
-
Save jurajantas/d0bee1b93ba3da65645f525c1faa90db to your computer and use it in GitHub Desktop.
simple quick sort
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> | |
void swap(int* a, int* b) { | |
int t = *a; | |
*a = *b; | |
*b = t; | |
} | |
int partition(int arr[], int low, int high ) { | |
int pivot = arr[high]; | |
int i = (low - 1); | |
for (int j = low; j <= high -1; 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++) { | |
printf("%d ", arr[i]); | |
} | |
printf("\n"); | |
} | |
int main(int argc, const char * argv[]) { | |
printf("start\n"); | |
int sz = 100000000; | |
int* arr = new int[sz]; | |
for (int i = 0; i < sz; i++) { | |
arr[i] = rand()%100; | |
} | |
printf("sorting\n"); | |
quicksort(arr, 0, sz-1); | |
printf("Sorted:\n"); | |
printArray(arr, sz); | |
delete [] arr; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment