Created
April 24, 2010 17:28
-
-
Save justjkk/377797 to your computer and use it in GitHub Desktop.
Implementation of Quicksort in C language
This file contains hidden or 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
// Implementation of Quicksort in C language | |
#include<stdio.h> | |
int i,j,n,pivot,a[20]; | |
void quick(int *a, int left, int right); | |
void swap(int *a, int i, int j); | |
void main() | |
{ | |
int a[20]; | |
printf("Enter the limits: "); | |
scanf("%d",&n); | |
printf("Enter the elements: "); | |
for(i=0;i<n;i++) | |
scanf("%d",&a[i]); | |
quick(a,0,n-1); | |
printf("The sorted list is ... \n"); | |
for(i=0;i<n;i++) | |
printf("%d\n",a[i]); | |
} | |
void quick(int *a, int first, int last) | |
{ | |
if(first<last) | |
{ | |
pivot=a[first]; | |
i=first; | |
j=last; | |
while(i<j) | |
{ | |
while(a[i]<=pivot && i<last) | |
i++; | |
while(a[j]>=pivot && j>first) | |
j--; | |
if(i<j) | |
swap(a,i,j); | |
} | |
swap(a,first,j); | |
quick(a,first,j-1); | |
quick(a,j+1,last); | |
} | |
} | |
void swap(int *a, int i, int j) | |
{ | |
int temp; | |
temp=a[i]; | |
a[i]=a[j]; | |
a[j]=temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment