Skip to content

Instantly share code, notes, and snippets.

@rizky
Created June 4, 2018 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rizky/14272e69dadc2e1f9c5205e80142aa12 to your computer and use it in GitHub Desktop.
Save rizky/14272e69dadc2e1f9c5205e80142aa12 to your computer and use it in GitHub Desktop.
#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