Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <algorithm>
  4. #include<cstdlib>
  5. using namespace std;
  6.  
  7. void SelectionSort()
  8. {
  9.     int n=100000,arr[n];
  10.     srand(time(0));
  11.     for(int i=0; i<n; i++)
  12.     {
  13.         arr[i]=1+rand()%100000;
  14.     }
  15.     int minimum;
  16.     clock_t t;
  17.     t=clock();
  18.     for (int i=0; i<n-1; i++)
  19.     {
  20.         minimum=i;
  21.         for(int j=1; j<n-1; j++)
  22.         {
  23.             if(arr[j]<arr[minimum])
  24.                 minimum=j;
  25.         }
  26.         swap(arr[minimum],arr[i]);
  27.     }
  28.     cout<<"Selection Sorting Time : "<<float(clock()-t)/CLOCKS_PER_SEC<<"Seconds"<<endl;
  29. }
  30. void InsertionSort ()
  31. {
  32.     int n=100000,arr[n];
  33.     srand(time(0));
  34.     for(int i=0; i<n; i++)
  35.     {
  36.         arr[i]=1+rand()%100000;
  37.     }
  38.     clock_t t;
  39.     t=clock();
  40.     for(int i=0; i<n-1; i++)
  41.     {
  42.         if(arr[i]>arr[i+1])
  43.         {
  44.             swap(arr[i],arr[i+1]);
  45.             for(int j=i; j>=0; j--)
  46.             {
  47.                 if(arr[j]<arr[j-1])
  48.                     swap(arr[j],arr[j-1]);
  49.             }
  50.         }
  51.     }
  52.  
  53.     cout<<"------------------------"<<endl<<"Insertion Sorting Time : "<<float(clock()-t)/CLOCKS_PER_SEC<<"Seconds"<<endl;
  54. }
  55. void BubbleSort()
  56. {
  57.     int n=100000,arr[n];
  58.     srand(time(0));
  59.     for(int i=0; i<n; i++)
  60.     {
  61.         arr[i]=1+rand()%100000;
  62.     }
  63.     clock_t t;
  64.     t=clock();
  65.     for(int i=0; i<n-1; i++)
  66.     {
  67.         for(int j=0; j<n-1-i; j++)
  68.         {
  69.             if(arr[j]>arr[j+1])
  70.                 swap(arr[j],arr[j+1]);
  71.             }
  72.     }
  73.     cout<<"------------------------"<<endl<<"Bubble Sorting Time : "<<float(clock()-t)/CLOCKS_PER_SEC<<"Seconds"<<endl;
  74. }
  75. void Mc(int arr[],int a,int b,int c)
  76. {
  77.     int f=c-a+1,s=b-c+1,a1[f],a2[s];
  78.     srand(time(0));
  79.     clock_t t;
  80.     t=clock();
  81.     for(int i=0; i<f; i++)
  82.         a1[f]=arr[i];
  83.     for(int j=0; j<s; j++)
  84.         a2[s]=arr[j];
  85.     int counter1=0,counter2=0,z=0;
  86.     for(int t=0; t<counter1&&t<counter2; t++)
  87.     {
  88.         if(a1[counter1]<=a2[counter2])
  89.         {
  90.             arr[t]=a1[counter1];
  91.             counter1++;
  92.         }
  93.         else
  94.         {
  95.             arr[t]=a2[counter2];
  96.             counter2++;
  97.         }
  98.         z=t;
  99.     }
  100.     while(counter1<f){
  101.         arr[z]=a1[counter1];
  102.         counter1++; z++;
  103.     }
  104.     while(counter2<s){
  105.         arr[z]=a2[counter2];
  106.         counter2++; z++;
  107.     }
  108. cout<<"------------------------ \n"<<"Merge Sorting Time : "<<float(clock()-t)/CLOCKS_PER_SEC<<"Seconds"<<endl;
  109. }
  110.  
  111.  
  112. void MergeSort (int arr[], int l, int r)
  113. {
  114.     int mid = (l + r) / 2;
  115.     if (l < r) {
  116.         MergeSort(arr,l,mid);
  117.         MergeSort(arr,mid + 1, r);
  118.         Mc(arr, l, mid, r);
  119.     }
  120. }
  121.  
  122. int main()
  123. {
  124.     srand(time(0));
  125.     const int N = 1e5;
  126.     int arr[N];
  127.     for (int i = 0; i < N; i++) {
  128.         arr[i] = rand();
  129.     }
  130.  
  131.     SelectionSort();
  132.     InsertionSort();
  133.     BubbleSort();
  134.     MergeSort(arr, 0, N - 1);
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement