India Forums .Info

Discuss Cars, Bikes, Jobs, Romance, Marriage, Jobs, Real Estate, Programming and more..
It is currently Tue Dec 02, 2008 1:23 am

All times are UTC + 5:30 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Recursive Balanced Quick Sort
PostPosted: Wed Feb 07, 2007 2:31 pm 
Offline
Newbie

Joined: Tue Feb 06, 2007 4:50 pm
Posts: 37
Recursive Balanced Quick Sort

A variation of quick sort .
it performs very well with time complexity of O(nlogn) always.
it assures that in one pass 3 data will be sorted.


Code:
#include<stdio.h>
#include<conio.h>
void qsort(int a[],int,int);
void partition(int a[],int,int,int *);
void print(int *,int);
void swap(int *,int *);

void main()
{
int a[30],i,n;
flushall();
clrscr();
printf("
enter how many data u want :");
scanf("%d",&n);

printf("
enter the data :");
for(i=0;i<n;i++)
{
printf("
%d .",i);
scanf("%d",&a[i]);
}
qsort(a,0,n-1);
printf("
SORTED DATA:
");
print(a,n-1);
getch();
}
void qsort(int a[],int lb,int ub)
{
int j;
if(ub>lb)
{
partition(a,lb,ub,&j);
qsort(a,lb,j-2);
qsort(a,j+2,ub);
}
}
void partition(int a[],int lb,int ub,int *j)
{

int mid=(lb+ub)/2,temp,up,down,pivot;
pivot=a[mid];
up=ub;
down=lb;
while(down<up)
{
while( a[down] <= pivot && down <= ub )
{
down++;
if(a[down]<a[down-1]&&down>lb)
swap(&a[down],&a[down-1]);
}
while(a[up]>pivot)
{
up--;
if(a[up]>a[up+1]&&up<ub)
swap(&a[up],&a[up+1]);
}
if(down<up)
{
swap(&a[down],&a[up]);
if(a[down]<a[down-1]&&down>0)
swap(&a[down],&a[down-1]);
if(a[up]>a[up+1]&&up<ub)
swap(&a[up],&a[up+1]);
}

}
for(int i=0;i<ub-1;i++)
if(a[i]>a[i+1])
swap(&a[i],&a[i+1]);
*j=up;
}
void print(int a[],int n)
{
for(int i=0;i<=n;i++)
printf("
%d . %d",i,a[i]);
}
void swap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}


For more source codes visit SourceCodesWorld.Com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 5:30 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO