quick_Sort
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<time.h>
void swap(int *x,int *y){
int temp = *x;
*x = *y;
*y = temp;
}
int partition(int a[],int l,int h){
int pivot = a[l];
int i = l ,j=h;
do{
do{i++;}while(a[i]<=pivot);
do{j--;}while(a[j]>pivot);
if(i<j){
swap(&a[i],&a[j]);
}
}while(i<j);
swap(&a[l],&a[j]);
return j;
}
void Quicksort(int a[],int l,int h){
int j;
if(l<h){
j=partition(a,l,h);
Quicksort(a,l,j);
Quicksort(a,j+1,h);
}
}
int main(){
clock_t start_time = clock();
int n;
printf("Enter the no. of elements: ");
scanf("%d",&n);
int a[n];
a[n-1] = INT32_MAX;
srand(time(NULL));
for(int i=0;i<n;i++){
a[i] = rand() % n;
}
Quicksort(a,0,n-1);
clock_t stop_time = clock();
printf("Sorted array is \n");
for(int i=0;i<n;i++){
printf("%d ",&a[i]);
}
double timetake = (double)(stop_time-start_time)/CLOCKS_PER_SEC;
printf("%lf",timetake);
return 0;
}
Comments
Post a Comment