Created
April 15, 2020 00:58
-
-
Save willianrschuck/403a8aa68d5b65c77b1adfb02ea8e385 to your computer and use it in GitHub Desktop.
Quicksort algorithm implemented in C
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 <stdio.h> | |
| #define swap(a, b) {int aux = a; a = b; b = aux;} | |
| void quicksort(int*, int, int); | |
| int partition(int*, int, int); | |
| int main() { | |
| int a[] = {10,9,8,7,6,5,4,3,2,1}; | |
| quicksort(a, 0, 9); | |
| for (int i = 0; i < 10; i++) { | |
| printf("%d ", a[i]); | |
| } | |
| return 0; | |
| } | |
| void quicksort(int *array, int p, int r) { | |
| if (p < r) { | |
| int q = partition(array, p, r); | |
| quicksort(array, p, q-1); | |
| quicksort(array, q+1, r); | |
| } | |
| } | |
| int partition(int *array, int p, int r) { | |
| int x = array[r]; | |
| int i = p-1; | |
| for (int j = p; j < r; j++) { | |
| if (array[j] <= x) { | |
| i++; | |
| swap(array[i], array[j]); | |
| } | |
| } | |
| swap(array[i+1], array[r]); | |
| return i+1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment