Advertisement
Kosty_Fomin

Untitled

Jul 5th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. //XCODE
  2.  
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <climits>
  8.  
  9.  
  10. int externalCycleCount = 0;
  11. int internalCycleCount = 0;
  12. int numberOfSwaps = 0;
  13.  
  14.  
  15. void bubble_sort(long *mas,long n)
  16. {
  17.     externalCycleCount = 0;
  18.     internalCycleCount = 0;
  19.     numberOfSwaps = 0;
  20.    
  21.     for(int i = 1; i < n; ++i)
  22.     {
  23.         externalCycleCount++;
  24.        
  25.         for(int j = 0; j < n - i; ++j)
  26.         {
  27.             internalCycleCount++;
  28.            
  29.             if(mas[j]>mas[j+1])
  30.             {
  31.                 std::swap(mas[j+1],mas[j]);
  32.                 numberOfSwaps++;
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38.  
  39.  
  40. void print_mas(long *mas,long n)
  41. {
  42.     std::cout << "Sorted mas: ";
  43.    
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         std::cout << mas[i] << " ";
  47.     }
  48.    
  49.     std::cout << std::endl;
  50.     std::cout << "External cycles: " << externalCycleCount << std::endl;
  51.     std::cout << "Internal cycles: " << internalCycleCount << std::endl;
  52.     std::cout << "Number of swaps: " << numberOfSwaps << std::endl;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. int main(int argc, const char * argv[]) {
  60.     long n,k,buf;
  61.     bool ch;
  62.     bool per = false;
  63.     std::srand(std::time(0));
  64.    
  65.    
  66.     do
  67.     {
  68.         std::cout<<"Input N:";
  69.         std::cin>>n;
  70.        
  71.         if (std::cin.fail())
  72.         {
  73.             std::cin.clear();
  74.             std::cin.ignore(INT_MAX, '\n');
  75.         }
  76.     }
  77.     while(n <= 0);
  78.    
  79.    
  80.     do
  81.     {
  82.         std::cout<<"If you want random generation - input 1\n If you want hand-made generation - input 0\n";
  83.         std::cin>>ch;
  84.        
  85.        
  86.         if (std::cin.fail())
  87.         {
  88.             std::cin.clear();
  89.             std::cin.ignore(INT_MAX, '\n');
  90.         }
  91.     }
  92.     while(ch != 0 && ch != 1);
  93.    
  94.    
  95.     long* mas = new long[n];
  96.    
  97.     for(int i = 0; i<n; i++){
  98.         switch (ch) {
  99.             case 0:
  100.                 std::cin>>mas[i];
  101.                 if (std::cin.fail())
  102.                 {
  103.                     std::cin.clear();
  104.                     std::cin.ignore(INT_MAX, '\n');
  105.                 }
  106.                 break;
  107.             case 1:
  108.                 mas[i] = rand();
  109.                 break;
  110.             default:
  111.                 break;
  112.         }
  113.        
  114.     }
  115.    
  116.    
  117.    
  118.     for (int i = 0; i<n; i++) {
  119.         std::cout<<mas[i]<<" ";
  120.     }
  121.     std::cout<<"\n";
  122.    
  123.     for (int i = 0; i < n; i++) {
  124.         for (int j = 0; j < n-i-1; j++) {
  125.           if (mas[j] > mas[j+1]) {
  126.               long b = mas[j];
  127.                mas[j] = mas[j+1];
  128.                mas[j+1] = b;
  129.                per = true;
  130.             }
  131.         }
  132.        
  133.         if(per==false){
  134.             break;
  135.         }
  136.     }
  137.    
  138.     for (int i = 0; i<n; i++) {
  139.         std::cout<<mas[i]<<" ";
  140.     }
  141.    
  142.     bubble_sort(mas, n);
  143.    
  144.     print_mas(mas, n);
  145.    
  146.     delete[] mas;
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement