Advertisement
Guest User

Untitled

a guest
May 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using std::string;
  7.  
  8. void quickSort(int*, int, int);
  9. void bubbleSort();
  10. void countingSort(int*, int*, int, int&);
  11. void insertionSort(int*, int&);
  12. void mergeSort(int, int, int*, int*);
  13.  
  14. int maxNumber(int*, int&);
  15.  
  16. int main() {
  17.     setlocale(LC_ALL, "polish");
  18.     srand(time(NULL));
  19.    
  20.     int usrCh = 0;
  21.    
  22.     const int tabSize = 7;
  23.     string choices[tabSize] = {
  24.         "Bubble sort",
  25.         "Quick sort",
  26.         "Counting sort",
  27.         "Insertion sort",
  28.         "Heapsort",
  29.         "Shell sort",
  30.         "Merge sort"
  31.     };
  32.    
  33.     for (int i = 0; i < tabSize; ++i)
  34.         std::cout << i+1 << ". " << choices[i] << std::endl;
  35.    
  36.     std::cout << "Twój wybór: ";
  37.         std::cin >> usrCh;
  38.    
  39.     switch(usrCh) {
  40.         case 1: {
  41.            
  42.             bubbleSort();
  43.  
  44.             break;
  45.         }
  46.         case 2: {
  47.            
  48.             int howMuch = 0;
  49.  
  50.             std::cout << "Ile liczb chcesz losować: ";
  51.                 std::cin >> howMuch;    
  52.             int tab[howMuch];  
  53.            
  54.             for (int i = 0; i < howMuch; ++i)  
  55.                 tab[i] = rand()%1000+1;    
  56.                
  57.             std::cout << "Wylosowane liczby to: " << std::endl;
  58.             for (int i = 0; i < howMuch; ++i)  
  59.                 std::cout << i+1 << ". " << tab[i] << std::endl;  
  60.  
  61.             quickSort(tab, 0, howMuch-1);  
  62.             std::cout << "Posortowane liczby to: " << std::endl;
  63.             for (int i = 0; i < howMuch; ++i)
  64.                 std::cout << i+1 << ". " << tab[i] << std::endl;
  65.  
  66.             break;        
  67.         }
  68.         case 3: {
  69.  
  70.             int howMuch = 0;
  71.  
  72.             std::cout << "Ile liczb chcesz losować: ";
  73.                 std::cin >> howMuch;    
  74.             int tab[howMuch];
  75.             int tab2[howMuch];  
  76.            
  77.             for (int i = 0; i < howMuch; ++i)  
  78.                 tab[i] = rand()%1000+1;    
  79.                
  80.             std::cout << "Wylosowane liczby to: " << std::endl;
  81.             for (int i = 0; i < howMuch; ++i)  
  82.                 std::cout << i+1 << ". " << tab[i] << std::endl;  
  83.  
  84.             int maxNum = maxNumber(tab, howMuch);
  85.             countingSort(tab, tab2, maxNum, howMuch);
  86.             std::cout << "Posortowane liczby to: " << std::endl;
  87.             for (int i = 0; i < howMuch; ++i)
  88.                 std::cout << i+1 << ". " << tab2[i] << std::endl;
  89.  
  90.             break;
  91.         }
  92.         case 4: {
  93.  
  94.             int howMuch = 0;
  95.  
  96.             std::cout << "Ile liczb chcesz losować: ";
  97.                 std::cin >> howMuch;    
  98.             int tab[howMuch];  
  99.            
  100.             for (int i = 0; i < howMuch; ++i)  
  101.                 tab[i] = rand()%1000+1;    
  102.                
  103.             std::cout << "Wylosowane liczby to: " << std::endl;
  104.             for (int i = 0; i < howMuch; ++i)  
  105.                 std::cout << i+1 << ". " << tab[i] << std::endl;  
  106.            
  107.             insertionSort(tab, howMuch);
  108.             std::cout << "Posortowane liczby to: " << std::endl;
  109.             for (int i = 0; i < howMuch; ++i)
  110.                 std::cout << i+1 << ". " << tab[i] << std::endl;
  111.  
  112.             break;
  113.         }
  114.         case 5: {
  115.            
  116.             int howMuch = 0;
  117.             int buffX = 0, buffY = 0, buffZ = 0;
  118.  
  119.             std::cout << "Ile liczb chcesz losować: ";
  120.                 std::cin >> howMuch;    
  121.             int tab[howMuch];
  122.            
  123.             for (int i = 0; i < howMuch; ++i)  
  124.                 tab[i] = rand()%1000+1;    
  125.                
  126.             std::cout << "Wylosowane liczby to: " << std::endl;
  127.             for (int i = 0; i < howMuch; ++i)  
  128.                 std::cout << i+1 << ". " << tab[i] << std::endl;
  129.  
  130.             for (int i = 1; i < howMuch; ++i) {
  131.                 buffX = tab[i];
  132.                 buffY = i;
  133.  
  134.                 while (1) {
  135.                     buffZ = buffY;
  136.                     buffY = (buffZ - 1) / 2;
  137.  
  138.                     if ((buffZ == 0) || (tab[buffY] >= buffX)) break;
  139.                     tab[buffZ] = tab[buffY];
  140.                 }
  141.  
  142.                 tab[buffZ] = buffX;
  143.             }
  144.  
  145.             std::cout << "Posortowane liczby to: " << std::endl;
  146.             for (int i = 0; i < howMuch; ++i)
  147.                 std::cout << i+1 << ". " << tab[i] << std::endl;
  148.  
  149.             break;        
  150.         }
  151.         case 6: {
  152.            
  153.             int howMuch = 0, iterI = 0, iterJ = 0, iterH = 0;
  154.             int buffX = 0;
  155.  
  156.             std::cout << "Ile liczb chcesz losować: ";
  157.                 std::cin >> howMuch;    
  158.             int tab[howMuch];  
  159.            
  160.             for (int i = 0; i < howMuch; ++i)  
  161.                 tab[i] = rand()%1000+1;    
  162.                
  163.             std::cout << "Wylosowane liczby to: " << std::endl;
  164.             for (int i = 0; i < howMuch; ++i)  
  165.                 std::cout << i+1 << ". " << tab[i] << std::endl;  
  166.  
  167.             for (iterH = 1; iterH < howMuch; iterH = 3*iterH+1);
  168.             iterH /= 9;
  169.             if (!iterH) iterH++;
  170.  
  171.             while (iterH) {
  172.                 for (iterJ = howMuch-iterH-1; iterJ >= 0; --iterJ) {
  173.                     buffX = tab[iterJ];
  174.                     iterI = iterJ + iterH;
  175.                     while ((iterI < howMuch) && (buffX > tab[iterI])) {
  176.                         tab[iterI - iterH] = tab[iterI];
  177.                         iterI += iterH;
  178.                     }
  179.                     tab[iterI - iterH] = buffX;
  180.                 }
  181.                 iterH /= 3;
  182.             }
  183.  
  184.             std::cout << "Posortowane liczby to: " << std::endl;
  185.             for (int i = 0; i < howMuch; ++i)
  186.                 std::cout << i+1 << ". " << tab[i] << std::endl;
  187.  
  188.             break;
  189.         }
  190.  
  191.         case 7: {
  192.  
  193.             int howMuch = 0;
  194.  
  195.             std::cout << "Ile liczb chcesz losować: ";
  196.                 std::cin >> howMuch;    
  197.             int tab[howMuch];  
  198.             int tab2[howMuch];
  199.  
  200.             for (int i = 0; i < howMuch; ++i)  
  201.                 tab[i] = rand()%1000+1;    
  202.                
  203.             std::cout << "Wylosowane liczby to: " << std::endl;
  204.             for (int i = 0; i < howMuch; ++i)  
  205.                 std::cout << i+1 << ". " << tab[i] << std::endl;  
  206.  
  207.             mergeSort(0, howMuch-1, tab, tab2);
  208.             std::cout << "Posortowane liczby to: " << std::endl;
  209.             for (int i = 0; i < howMuch; ++i)
  210.                 std::cout << i+1 << ". " << tab2[i] << std::endl;
  211.  
  212.             break;
  213.         }
  214.  
  215.         default: {
  216.             return 1;  
  217.         }
  218.     }
  219.    
  220.     return 0;  
  221. }
  222.  
  223. void quickSort(int tab[], int left, int right) {
  224.    
  225.     int firstIterator = left;
  226.     int secondIterator = right;
  227.     int center = tab[(left+right) / 2];
  228.  
  229.     while (firstIterator <= secondIterator) {
  230.         while (tab[firstIterator] < center)
  231.             firstIterator++;
  232.        
  233.         while (tab[secondIterator] > center)
  234.             secondIterator--;
  235.  
  236.         if (firstIterator <= secondIterator) {
  237.             std::swap(tab[firstIterator], tab[secondIterator]);
  238.  
  239.             firstIterator++;
  240.             secondIterator--;
  241.         }
  242.  
  243.     }
  244.  
  245.     if (left < secondIterator) quickSort(tab, left, secondIterator);
  246.     if (right > firstIterator) quickSort(tab, firstIterator, right);
  247.  
  248.     return;
  249. }
  250.  
  251. void bubbleSort() {
  252.  
  253.         // bubble sort
  254.         int howMuch = 0;
  255.        
  256.         std::cout << "Ile liczb chcesz losować: ";
  257.         std::cin >> howMuch;    
  258.         int tab[howMuch];  
  259.            
  260.         for (int i = 0; i < howMuch; ++i)
  261.             tab[i] = rand()%1000+1;    
  262.                
  263.         std::cout << "Wylosowane liczby to: " << std::endl;
  264.         for (int i = 0; i < howMuch; ++i)  
  265.             std::cout << i+1 << ". " << tab[i] << std::endl;  
  266.                
  267.         // sortowanie
  268.         for (int i = 0; i < howMuch; ++i) {
  269.             for (int j = 0; j < howMuch-1; ++j) {
  270.                 if (tab[j] > tab[j+1]) std::swap(tab[j], tab[j+1]);  
  271.              }
  272.          }
  273.            
  274.         std::cout << "Posortowane liczby to: " << std::endl;
  275.         for (int i = 0; i < howMuch; ++i)
  276.             std::cout << i+1 << ". " << tab[i] << std::endl;
  277.  
  278.     return;
  279. }
  280.  
  281. void countingSort(int mainTab[], int secondTab[], int maxNum, int &howMuch) {
  282.    
  283.     int statTab[maxNum+1];
  284.  
  285.     for (int i = 0; i <= maxNum; ++i)
  286.         statTab[i] = 0;
  287.  
  288.     for (int i = 0; i < howMuch; ++i)
  289.         statTab[(mainTab[i])]++;
  290.  
  291.     for (int i = 1; i <= maxNum; ++i)
  292.         statTab[i] += statTab[i-1];
  293.    
  294.     for (int i = howMuch-1; i >= 0; --i) {
  295.         secondTab[(statTab[(mainTab[i])])-1];
  296.         statTab[(mainTab[i])]--;
  297.     }
  298.  
  299.     return;
  300. }
  301.  
  302. int maxNumber(int tab[], int &howMuch) {
  303.  
  304.     int max = tab[0];
  305.     for (int i = 1; i < howMuch; --i) {
  306.         if (max < tab[i]) max = tab[i];
  307.     }
  308.  
  309.     return max;
  310. }
  311.  
  312. void insertionSort(int tab[], int &howMuch) {
  313.  
  314.     int insert = 0, j = 0;
  315.  
  316.     for (int i = 0; i < howMuch; ++i) {
  317.        
  318.         insert = tab[i];
  319.         j = i - 1;
  320.  
  321.         while (j >= 0 && tab[j] > insert) {
  322.             tab[j+1] = tab[j];
  323.             --j;
  324.         }
  325.  
  326.         tab[j+1] = insert;
  327.     }
  328.  
  329.     return;
  330. }
  331.  
  332. void mergeSort(int first, int last, int tab[], int tab2[]) {
  333.  
  334.     int oldFirst = 0;
  335.     int it1 = 0, it2 = 0;
  336.  
  337.     oldFirst = (first + last + 1) / 2;
  338.     if (oldFirst - first > 1) mergeSort(first, oldFirst - 1, tab, tab2);
  339.     if (last - oldFirst > 0) mergeSort(oldFirst, last, tab, tab2);
  340.  
  341.     it1 = first; it2 = last;
  342.     for (int i = first; i <= last; ++i)
  343.         tab2[i] = ((it1 == oldFirst) || ((it2 <= last) && (tab[it1] > tab[it2]))) ? tab[it2++] : tab[it1++];
  344.     for (int i = first; i <= last; ++i)
  345.         tab[i] = tab2[i];
  346.  
  347.     return;
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement