My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.
Showing posts with label Sort Using Quick Sort. Show all posts
Showing posts with label Sort Using Quick Sort. Show all posts

Monday, 15 August 2011

Sort Using Quick Sort

Write a Program in C to Accept 5 Numbers from the User and Sort Using Quick Sort
#include <stdio.h>
#include <conio.h>
int split(int lo,int up) ;
void quicksort(int lo, int up) ;
int arr[10]={90,80,70,60,50,40,30,20,10,0};
void main( )
{
int i ;
clrscr( ) ;
printf("Original Array\n");
for(i=0;i<=9;i++)
{
printf("%d\t",arr[i]);
}
quicksort(0,9);
printf ( "\nArray after using Quick Sort\n") ;
for(i=0;i<=9;i++)
{
printf("%d\t",arr[i]) ;
}
getch( ) ;
}
void quicksort(int lo,int up)
{
int i;
if(up>lo) //check whether upper is greater than lower
{
i=split(lo,up);
quicksort(lo,i-1);
quicksort(i+1,up);
}
}
int split(int lo,int up)
{
int i,p,q,t;
p=lo+1; //lower limit
q=up; //upper limit
i=arr[lo];
while(q>=p) //check index p crosses q
{
while(arr[p]<i)
{
p++;
}
while(arr[q]>i)
{
q--;
}
if(q>p)
{
t= arr[p];
arr[p]=arr[q];
arr[q]=t;
}
}
t=arr[lo] ;
arr[lo]=arr[q] ;
arr[q]=t ;
return q;
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls