Created
November 10, 2025 20:58
-
-
Save mgabrielmarin/c409018a97ed0f3ecfbb359374589848 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 <stdio.h> | |
| #include <stdlib.h> | |
| void swap(int *a, int *b) { | |
| int tmp = *a; | |
| *a = *b; | |
| *b = tmp; | |
| } | |
| int partition(int *a, int l, int h) { | |
| int i = l; | |
| int j; | |
| int p = a[h]; | |
| for (j = l; j < h; ++j) | |
| if (a[j] < p) | |
| swap(&a[i++], &a[j]); | |
| swap(&a[i], &a[j]); | |
| return i; | |
| } | |
| void quick_sort(int *a, int l, int h) { | |
| if (l < h) { | |
| int p = partition(a, l, h); | |
| quick_sort(a, l, p-1); | |
| quick_sort(a, p, h); | |
| } | |
| } | |
| void print_arr(int *a, int len) { | |
| for (int i = 0; i < len; ++i) | |
| printf("%d " , a[i]); | |
| printf("\n"); | |
| } | |
| int main() { | |
| int a[5] = {3, 1, 5, 10, 7}; | |
| print_arr(a, 5); | |
| quick_sort(a, 0, 4); | |
| print_arr(a, 5); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment