Created
June 4, 2018 21:04
-
-
Save rizky/14272e69dadc2e1f9c5205e80142aa12 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> | |
void | |
swap (int *a, int *b) | |
{ | |
int temp; | |
temp = *a; | |
*a = *b; | |
*b = temp; | |
} | |
void | |
quick_sort(int *tab, int size, int first, int last) | |
{ | |
int i; | |
int j; | |
int pivot; | |
if (last < first) | |
return; | |
i = first; | |
j = last; | |
pivot = (first + last) / 2; | |
while (i < j) | |
{ | |
while (tab[i] <= tab[pivot]) | |
i++; | |
while (tab[j] > tab[pivot]) | |
j--; | |
if (i < j) | |
swap(&(tab[i]), &(tab[j])); | |
} | |
swap(&(tab[j]), &(tab[pivot])); | |
quick_sort(tab, size, first, j - 1); | |
quick_sort(tab, size, j + 1, last); | |
} | |
int | |
main() | |
{ | |
int size; | |
int tab[9] = {1 ,3 ,5 ,7 ,9 ,2 ,4 ,6 , 8}; | |
int i; | |
size = 9; | |
quick_sort(tab, size, 0, size - 1); | |
i = 0; | |
while (i < size) | |
{ | |
printf("%d, ", tab[i]); | |
i++; | |
} | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment