Skip to content

Instantly share code, notes, and snippets.

@MrDave1999
Created August 16, 2019 23:19
Show Gist options
  • Save MrDave1999/360c2f1b332d7ece877de8b6f22f1dcd to your computer and use it in GitHub Desktop.
Save MrDave1999/360c2f1b332d7ece877de8b6f22f1dcd to your computer and use it in GitHub Desktop.
Algoritmo quicksort en C
#include <stdio.h>
#define MAX_NUM 50
void quicksort(int* const a, int begin, int end)
{
int i, j, medium, aux;
int pivote;
medium = (begin + end) / 2;
pivote = a[medium];
i = begin;
j = end;
do
{
while (a[i] < pivote) i++;
while (a[j] > pivote) j--;
if (i <= j)
{
aux = a[i];
a[i] = a[j];
a[j] = aux;
i++;
j--;
}
} while (i <= j);
if (begin < j)
quicksort(a, begin, j);
if (i < end)
quicksort(a, i, end);
}
int main(void)
{
srand(time(NULL));
int num[MAX_NUM];
for(int i = 0; i != MAX_NUM; ++i)
num[i] = rand() % 100;
quicksort(num, 0, MAX_NUM-1);
for(int i = 0; i != MAX_NUM; ++i)
printf("%d\n", num[i]);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment