Wednesday 26 October 2011

implementation of quick sort algorithm

/*
ALGORITHM:

1.  low =l, high = h, key a[(l+h)/2]
2.  Repeat through step 7 while (low <= high)
3.  Repeat step 4 while (a[low] < key)
4.  low = low +1
5.  Repeat step 6 while (a[high] > key)
6.  high = high – 1
7.  If (low <= high)
a)  temp = a[low]
b)  a[low] = a[high]
c)  a[high] = temp
d)  low = low + 1
e)  high = high + 1
8.  If (l < high) quicksort (a,l,high)
9.  If (h>low) quicksort (a,low,h)
10.  Exit
*/
//program is implemented by shadab khan
#include <stdio.h>
#include <conio.h>
#define max 100
Int a[max],n,i,l,h;

void main()
{
void input(void);
input();
getch();
}

void input(void);
{
void output( int a[], int n);
void quick_sort(int a[], int l, int h);
printf("How many elements in the array : ");
scanf("%d",&n);
printf("\n");
printf("Enter the elemennts : \n");
for(i=0;i<=n-1;i++)
{
            scanf("%d",&a[i]);
}
l=0;
h=n-1;
quick_sort(a,l,h);
printf("Sorted Array :\n ");
output(a,n);
}
void quick_sort(int a[],int l, int h)
{
            Int temp,key,low,high;
low=l;
high=h;
key=a[(low+high)/2];
do
{
while(key>a[low])
{
low++;
}
while(key<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
}
while(low<=high)
{
if (l<high)
quick_sort(a,l,high);
if(low<h)
quick_sort(a,low,h);
}
}
void output(int a[], int n)
{
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}

1 comment: