Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #define max 30000
- #define ntimes 2000
- void swap(int *a,int *b){
- int temp=*a;
- *a=*b;
- *b=temp;
- }
- int partition(int a[],int lb,int ub){
- int pivot=a[lb];
- int start=lb;
- int end=ub;
- while(start<end){
- while(a[start]<=pivot){
- start++;
- }
- while(a[end]>pivot){
- end--;
- }
- if(start<end){
- swap(&a[start],&a[end]);
- }
- }
- swap(&a[lb],&a[end]);
- return end;
- }
- void quicksort(int a[],int lb,int ub){
- if(lb<ub){
- int loc=partition(a,lb,ub);
- quicksort(a,lb,loc-1);
- quicksort(a,loc+1,ub);
- }
- }
- int main(){
- int a[max],k,i,n;
- double runtime=0;
- clock_t start,end;
- printf("enter the num of values");
- scanf("%d",&n);
- srand(time(NULL));
- for(i=0;i<n;i++){
- a[i]=rand();
- }
- start=clock();
- quicksort(a,0,n-1);
- end=clock();
- runtime=(double)(end-start)/CLOCKS_PER_SEC;
- printf("the elements are:");
- for(i=0;i<n;i++)
- {
- printf("%d\n",&a[i]);
- }
- printf("the runtime=%lf",runtime);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment