1 - Quick Sort
1 - Quick Sort
#include<stdio.h>
int partition(int a[], int low, int high)
{
int pivot = a[low], i=low, j= high+1;
int temp;
while(i<j)
{
do
{
i++;
}while(pivot>=a[i] && i<high);
do
{
j--;
}while(pivot<a[j]);
if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[low] = a[j];
a[j] = pivot;
return j;
}
void quick_sort(int a[], int low, int high)
{
int s;
if(low<high)
{
s = partition(a,low,high);
quick_sort(a,low,s-1);
quick_sort(a,s+1,high);
}
}
int main()
{
int a[50],n,low,high,i;
printf("Enter no. of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
quick_sort(a,low,high);
printf("Sorted Array :");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
#include<stdio.h>
#include<time.h>
a[i]=rand()%100;
printf("%d\t",a[i]);
}
low=0;
high=n-1;
st=clok();
quick_sort(a,low,high);
end=clock();