Created
August 29, 2012 06:00
-
-
Save murikadan/3507302 to your computer and use it in GitHub Desktop.
Quick Sort Of An Integer Array
This file contains 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
10 | |
5 | |
7 | |
3 | |
0 | |
9 | |
8 | |
2 | |
1 | |
6 | |
4 |
This file contains 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> | |
#include <stdlib.h> | |
int partition(int *,int,int); | |
void quicksort(int *,int,int); | |
void swap(int*,int*); | |
int main() | |
{ | |
int *a,n,i,p=0,r; | |
FILE *fp; | |
fp=fopen("input00","r"); | |
fscanf(fp,"%d",&n); //No elements in the array | |
a=malloc(sizeof(int)*n); | |
fscanf(fp,"%d",&a[0]); | |
for(i=1;i<n;i++) | |
fscanf(fp,"%d",&a[i]); | |
fclose(fp); | |
r=n-1; | |
quicksort(a,p,r); | |
for(i=0;i<n;i++) | |
printf("%d ",a[i]); | |
free(a); | |
return 0; | |
} | |
void swap(int *a,int *b) | |
{ | |
int temp=*b; | |
*b=*a; | |
*a=temp; | |
} | |
int partition(int *a,int p,int r) | |
{ | |
int i=p-1,j; | |
for(j=p;j<r;j++) | |
{ | |
if(a[j]<a[r]) | |
{ | |
i++; | |
if(i!=j) | |
swap(&a[i],&a[j]); | |
} | |
} | |
swap(&a[i+1],&a[r]); | |
return i+1; | |
} | |
void quicksort(int *a,int p,int r) | |
{ | |
if(p<r) | |
{ | |
int q=partition(a,p,r); | |
quicksort(a,p,q-1); | |
quicksort(a,q+1,r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment