Guest User

Untitled

a guest
Apr 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. using namespace std;
  4. int n;
  5. int a[100];
  6. int temp[100];
  7. void slivaem(int l,int c,int r)
  8. {
  9.     int n1=l,n2=c+1,n3=0,i;
  10.     while(n1<=c && n2<=r)
  11.     {
  12.         if(a[n1]<a[n2]) {temp[n3]=a[n1]; n3++; n1++;}
  13.         else
  14.         {
  15.             temp[n3]=a[n2];
  16.             n3++;
  17.             n2++;
  18.         }
  19.  
  20.     }
  21.     while(n1<=c)
  22.     {
  23.         temp[n3]=a[n1]; n3++; n1++;
  24.     }
  25.     while(n2<=r)
  26.     {
  27.         temp[n3]=a[n2]; n3++; n2++;
  28.     }
  29.  
  30.     int j=0;
  31.     for(i=l;i<=r;i++)
  32.     {
  33.         a[i]=temp[j];
  34.         j++;
  35.     }
  36.  
  37.  
  38.  
  39.  
  40. }
  41. void sortsl(int n1,int n2)
  42. {
  43.     if (n1>=n2) {return;}
  44.     int c;
  45.     c=(n1+n2)/2;
  46.     sortsl(n1,c);
  47.     sortsl(c+1,n2);
  48.     slivaem(n1,c,n2);
  49.     for(int i=0;i<n;i++)
  50.     {
  51.         cout << a[i] << " ";
  52.     }
  53.     cout << "\n";
  54. }
  55.  
  56. int main()
  57. {
  58.     int i;
  59.  
  60.     cin >> n;
  61.     srand(time(0));
  62.  
  63.     for(i=0;i<n;i++)
  64.     {
  65.         a[i] = rand() %100;
  66.  
  67.     }
  68.  
  69.  
  70.     sortsl(0,n-1);
  71.     for(i=0;i<n;i++)
  72.     {
  73.         cout << a[i] << " ";
  74.  
  75.     }
  76.     return 0;
  77. }
Add Comment
Please, Sign In to add comment