Skip to content

Instantly share code, notes, and snippets.

@DWD3
Created October 27, 2015 10:48
Show Gist options
  • Save DWD3/b189c83a13684d91ef35 to your computer and use it in GitHub Desktop.
Save DWD3/b189c83a13684d91ef35 to your computer and use it in GitHub Desktop.
#include<stdio.h>
void intswap(int*a,int* b);
int less(int a,int b);
void quicksort(int *A,int start,int end);
int partition(int *A,int start,int end);
int main(void)
{
int i;
int j=0;
int k;
int A[200];
while(scanf("%d",&i)==1)
{
A[j]=i;
j++;
}
k=j;
quicksort(A,0,k-1);
printf("the number you entered is : \n");
for(j=0;j<k;j++)
{
printf("%d\n",A[j]);
}
return 0;
}
void quicksort(int *A,int start,int end)
{
int i;
if(end<=start)
return;
i=partition(A,start,end);
quicksort(A,start,i-1);
quicksort(A,i+1,end);
}
int partition(int *A,int start,int end)
{
int i=start-1;
int j=end;
int v=A[j];
for(;;)
{
while(less(A[++i],v));
while(less(v,A[--j]));
if(i>=j)
{
break;
}
intswap(&A[i],&A[j]);
}
intswap(&A[i],&A[end]);
return i;
}
void intswap(int*a,int* b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
return;
}
int less(int a,int b)
{
if(a<b)
{
return 1;
}
else
{
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment