Advertisement
4rlen

Untitled

Apr 20th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. SORTOWANIE BOMBELKOWE
  2. -------------------------------------------------------------------------------------------------------------------------
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <time.h>
  6. using namespace std;
  7.  
  8. int bombelek(int s, int t[])
  9. {
  10.     int a, b=0;
  11.  
  12.     for(int i=0; i<s-1; i++)
  13.     {
  14.         for(int j=0; j<s; j++)
  15.         {
  16.             if(j!=s-1)
  17.             {
  18.                 if(t[j]>t[j+1])
  19.                 {
  20.                     a = t[j];
  21.                     t[j] = t[j+1];
  22.                     t[j+1] = a;
  23.                 }
  24.                 else {b++;}
  25.             }
  26.  
  27.         }
  28.         if(b==s-1) break;
  29.         else b=0;
  30.     }
  31.     return* t;
  32. }
  33.  
  34. int main()
  35. {
  36.     int n;
  37.     cin>>n;
  38.     int bsort[n];
  39.    
  40.     srand(time(NULL));
  41.    
  42.     for(int i=0; i<n; i++)
  43.     {
  44.         bsort[i] = rand()%10;
  45.         cout<<bsort[i]<<" ";
  46.     }
  47.  
  48.     cout<<endl;
  49.     int f = bombelek(n, bsort);
  50.  
  51.     for(int i=0; i<n; i++)
  52.     {
  53.         cout<<bsort[i]<<" ";
  54.     }
  55.    
  56.     return 0;
  57. }
  58. -------------------------------------------------------------------------------------------------------------------------
  59. SELECT SORT
  60. -------------------------------------------------------------------------------------------------------------------------
  61. #include <iostream>
  62. #include <cstdlib>
  63. #include <time.h>
  64. using namespace std;
  65.  
  66. int select(int s, int t[])
  67. {
  68.     int MIN, y=0 , j, x;
  69.     bool b;
  70.     for(int i=0; i<s-1; i++)
  71.     {  
  72.         MIN = t[i];
  73.         b = false;
  74.         for(j=i+1; j<s; j++)
  75.         {
  76.             if(t[j]<=MIN)
  77.             {
  78.                 MIN = t[j];
  79.                 y = j;
  80.                 b = true;
  81.             }
  82.         }
  83.         if(b == false) break;
  84.         else swap(t[i], t[y]);
  85.     }
  86.     return *t;
  87. }
  88.  
  89. int main()
  90. {
  91.     int n;
  92.     cin>>n;
  93.     int ssort[n];
  94.  
  95.     srand(time(NULL));
  96.  
  97.     for(int i=0; i<n; i++)
  98.     {
  99.         ssort[i] = rand()%10+1;
  100.         cout<<ssort[i]<<" ";
  101.     }
  102.     cout<<endl;
  103.  
  104.     select(n, ssort);
  105.     cout<<endl;
  106.  
  107.     for(int i=0; i<n; i++)
  108.     {
  109.         cout<<ssort[i]<<" ";
  110.     }
  111.     return 0;
  112. }
  113. -------------------------------------------------------------------------------------------------------------------------
  114. INSERTION SORT
  115. -------------------------------------------------------------------------------------------------------------------------
  116. #include <iostream>
  117. #include <cstdlib>
  118. #include <time.h>
  119. using namespace std;
  120.  
  121. int INSERT(int s, int t[])
  122. {
  123.    int z;
  124.    bool b;
  125.  
  126.     for(int i=0; i<s-1; i++)
  127.     {
  128.         for(int j=i+1; j<s; j++)
  129.         {
  130.             if(t[i] > t[j])
  131.             {
  132.                 z = t[j];
  133.                 t[j] = t[i];
  134.                 t[i] = z;
  135.             }
  136.         }
  137.     }
  138.     return *t;
  139. }
  140.  
  141. int main()
  142. {
  143.     int n;
  144.     cin>>n;
  145.     int isort[n];
  146.  
  147.     srand(time(NULL));
  148.  
  149.     for(int i=0; i<n; i++)
  150.     {
  151.         isort[i] = rand()%10+1;
  152.         cout<<isort[i]<<" ";
  153.     }
  154.  
  155.     cout<<endl;
  156.     INSERT(n, isort);
  157.     cout<<endl;
  158.  
  159.     for(int i=0; i<n; i++)
  160.     {
  161.         cout<<isort[i]<<" ";
  162.     }
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement