Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/5f9820a6c1d184b19d14 to your computer and use it in GitHub Desktop.
Save zsrinivas/5f9820a6c1d184b19d14 to your computer and use it in GitHub Desktop.
A lame implementaion of Quick Sort
/*Test Program*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include "quicksort.h"
#define MAXSIZ 100000
int main(int argc, char const *argv[])
{
long long int a[MAXSIZ];
long long int n,i;
scanf("%lld", &n);
for (i = 0; i < n; ++i)
scanf("%lld", &a[i]);
quicksort(a,0,n-1);
for (i = 0; i < n; ++i)
printf("%lld\n", a[i]);
return 0;
}
/*Header File for the Quick Sort*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
int quicksort(long long int unsortedarray[], long long int low, long long int high)
{
long long int pivot=unsortedarray[low];
long long int k,temp,i;
if (low<high)
{
k=low+1;
for (i = low+1; i <= high; ++i)
{
if (unsortedarray[i]<pivot)
{
temp=unsortedarray[i];
unsortedarray[i]=unsortedarray[k];
unsortedarray[k++]=temp;
}
}
temp=unsortedarray[low];
unsortedarray[low]=unsortedarray[--k];
unsortedarray[k]=temp;
quicksort(unsortedarray,low,k-1);
quicksort(unsortedarray,k+1,high);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment