/* ***************************************************
23. TO SEARCH ELEMENTS USING QUICK SORT .
************************************************** */
#include<*stdio.h>
#include<*conio.h>
#define MAXSIZE 50
void quicksort(int a[],int n);
void sort(int a[],int left,int right);
int a[MAXSIZE],n;
void main()
{
int i,n;
clrscr();
printf("\n how many elements are there : ");
scanf("%d",&n);
printf("\n\n enter the elements : ");
for(i=0;i
{
scanf("%d", &a[i]);
}
printf("\n\n elements before sorting : ");
for(i=0;i
{
printf("%d\t",a[i]);
}
quicksort(a,n);
printf("\n\n elements after sorting : ");
for(i=0;i
{
printf("%d\t",a[i]);
}
getch();
}
void quicksort(int a[],int n)
{
sort(a,0,n-1);
}
void sort(int a[],int left,int right)
{
int pivot,l,r;
l=left;
r=right;
pivot=a[l];
while(left
{
while((a[right]>=pivot)&&(left
right--;
if(left!=right)
{
a[left]=a[right];
left++;
}
while((a[left]<=pivot)&&(left
left++;
if(left!=right)
{
a[left]=a[right];
right--;
}
}
a[left]=pivot;
pivot=left;
left=l;
right=r;
if(left
sort(a,left,pivot-1);
if(right>pivot)
sort(a,pivot+1,right);
}
output
how many elements are there : 4
enter the elements : 9 4 8 5
elements before sorting : 9 4 8 5
elements after sorting : 4 5 8 9
23. TO SEARCH ELEMENTS USING QUICK SORT .
************************************************** */
#include<*stdio.h>
#include<*conio.h>
#define MAXSIZE 50
void quicksort(int a[],int n);
void sort(int a[],int left,int right);
int a[MAXSIZE],n;
void main()
{
int i,n;
clrscr();
printf("\n how many elements are there : ");
scanf("%d",&n);
printf("\n\n enter the elements : ");
for(i=0;i
{
scanf("%d", &a[i]);
}
printf("\n\n elements before sorting : ");
for(i=0;i
{
printf("%d\t",a[i]);
}
quicksort(a,n);
printf("\n\n elements after sorting : ");
for(i=0;i
{
printf("%d\t",a[i]);
}
getch();
}
void quicksort(int a[],int n)
{
sort(a,0,n-1);
}
void sort(int a[],int left,int right)
{
int pivot,l,r;
l=left;
r=right;
pivot=a[l];
while(left
{
while((a[right]>=pivot)&&(left
right--;
if(left!=right)
{
a[left]=a[right];
left++;
}
while((a[left]<=pivot)&&(left
left++;
if(left!=right)
{
a[left]=a[right];
right--;
}
}
a[left]=pivot;
pivot=left;
left=l;
right=r;
if(left
sort(a,left,pivot-1);
if(right>pivot)
sort(a,pivot+1,right);
}
output
how many elements are there : 4
enter the elements : 9 4 8 5
elements before sorting : 9 4 8 5
elements after sorting : 4 5 8 9
0 comments:
Post a Comment