Rishav_hitk_cse

day 10 lab

Jul 27th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void swap(int *,int *);
  6. void cs_sort(int *,int);
  7. void bubblesort(int* ,int);
  8. void quicksort(int *,int,int);
  9. int partition(int *,int,int);
  10.  
  11. int main()
  12. {
  13.     int n,u,l,i,input;
  14.         clock_t start,end;
  15.         double total;
  16.        
  17.         printf("Enter number of elements:");
  18.         scanf("%d",&n);
  19.         printf("Enter range for generating numbers(lower bound-upper bound):");
  20.         scanf("%d %d",&l,&u);
  21.        
  22.         int *array=(int *)malloc(sizeof(int)*n);
  23.        
  24.         srand((unsigned)time(NULL));
  25.         for(i=0;i<n;i++)
  26.             array[i]=l+rand()%(u-l);
  27.            
  28.         /*printf("Before sorting:\n");
  29.         for(i=0;i<n;i++)
  30.             printf("%d,",array[i]);*/
  31.            
  32.         printf("Enter your preference:\n1:bubblesort\n2:cocktail shaker sort\n3:quicksort\n");
  33.         scanf("%d",&input);
  34.        
  35.         start=clock();
  36.         switch(input){
  37.             case 1:bubblesort(array,n);break;
  38.             case 2:cs_sort(array,n);break;
  39.             case 3:quicksort(array,0,n-1);break;
  40.             default:printf("Wrong input");break;
  41.         }
  42.         end=clock();
  43.        
  44.         printf("Sorted array:\n");
  45.         for(i=0;i<n;i++)
  46.             printf("%d,",array[i]);
  47.            
  48.         total=(double)(end-start)/CLOCKS_PER_SEC;
  49.        
  50.         printf("\n\nTime taken: %lf seconds\n",total);
  51.        
  52.     return 0;
  53. }
  54.  
  55. void swap(int *a,int *b){
  56.     int temp;
  57.     temp=*a;
  58.     *a=*b;
  59.     *b=temp;
  60. }
  61.  
  62.  
  63. void bubblesort(int *a,int n){
  64.     int i,j;
  65.    for (i=0;i<n-1;i++)    
  66.        for (j=0;j<n-i-1;j++){
  67.            if (a[j]>a[j+1])  
  68.               swap(a+j,a+j+1);
  69.        }
  70. }
  71.  
  72.  
  73. void cs_sort(int *a,int n){
  74.     int i,swapped=1,start,end;
  75.     start=0;
  76.     end=n-1;
  77.     while(swapped){
  78.         swapped=0;
  79.         for(i=start;i<end;i++){
  80.             if(a[i]>a[i+1]){
  81.                 swap(a+i,a+i+1);
  82.                 swapped++;
  83.             }    
  84.         }
  85.        
  86.        
  87.         if(!swapped)
  88.             break;
  89.            
  90.         end--;
  91.         for(i=end;i>=start;i--){
  92.             if(a[i]>a[i+1]){
  93.                 swap(a+i,a+i+1);
  94.                 swapped++;
  95.             }
  96.         }
  97.         start++;
  98.     }
  99.    
  100. }
  101.  
  102. void quicksort(int *a,int l,int r){
  103.     if(l<r){
  104.         int q=partition(a,l,r);
  105.         quicksort(a,l,q-1);
  106.         quicksort(a,q+1,r);
  107.     }
  108. }
  109.  
  110. int partition(int *a,int l,int r){
  111.     int x=a[l];
  112.     int i=l+1,j;
  113.    
  114.     for(j=l+1;j<=r;j++){
  115.         if(a[j]<x){
  116.             swap(a+i,a+j);
  117.             i++;
  118.         }
  119.     }
  120.     swap(a+l,a+i-1);
  121.     return i-1;
  122. }
Add Comment
Please, Sign In to add comment