Advertisement
Guest User

Sortowania

a guest
Dec 13th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.67 KB | None | 0 0
  1. //ALGO2 IS1 211A LAB05
  2. //Jakub Ogryzek
  3. //oj44443@zut.edu.pl
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <ctime>
  7. #include <time.h>
  8. #include <iomanip>
  9. #include <string>
  10. #include <cstdlib>
  11. #include <memory>
  12. #include <string>
  13. #include <random>
  14. #include <vector>
  15. #include <algorithm>
  16. #include <stdio.h>
  17.  
  18. using namespace std;
  19.  
  20. template <typename T>
  21. class Heap {
  22. public:
  23.     T *data = new T[capacity];
  24.     int capacity;
  25.     int size;
  26.     Heap()
  27.     {
  28.         size = 0;
  29.         capacity = 1;
  30.         data = new T[capacity];
  31.     }
  32.     Heap(int tab_size, T* tab)
  33.     {
  34.         size = tab_size;
  35.         capacity = tab_size;
  36.         data = new T[capacity];
  37.         for (int i = 0; i < tab_size; i++)
  38.             data[i] = tab[i];
  39.     }
  40.     ~Heap()
  41.     {
  42.         delete[] data;
  43.         data = nullptr;
  44.         size = 0;
  45.         capacity = 0;
  46.     }
  47.  
  48.     int Push(int size)
  49.     {
  50.         if (size == 0)
  51.             return 0;
  52.         int parent_index = parent(size);
  53.         if (parent_index < 0)
  54.             return 0;
  55.         if (data[size] <= data[parent_index])
  56.             return 0;
  57.         swap(data[size], data[parent_index]);
  58.         return Push(parent_index);
  59.     }
  60.  
  61.     void AddNewElement(const T&input)
  62.     {
  63.         size = size + 1;
  64.         if (size <= capacity) {
  65.             data[size - 1] = input;
  66.         }
  67.         if (size > capacity)
  68.         {
  69.             capacity = capacity * 2;
  70.             T *temp = new T[capacity];
  71.             for (int i = 0; i < size - 1; i++)
  72.             {
  73.                 temp[i] = move(data[i]);
  74.             }
  75.             temp[size - 1] = input;
  76.             delete[] data;
  77.             data = temp;
  78.         }
  79.         Push(size - 1);
  80.     }
  81.     T DeleteRoot(int index)
  82.     {
  83.         if (size == 0)
  84.         {
  85.             throw out_of_range("ERROR");
  86.         }
  87.         if (size == 1)
  88.         {
  89.             T temp = data[0];
  90.             return temp;
  91.         }
  92.         swap(data[0], data[size - 1]);
  93.         size = size - 1;
  94.         DeleteRootRek(0);
  95.         T temp = data[size];
  96.         return temp;
  97.     }
  98.  
  99.     void DeleteRootRek(int index)
  100.     {
  101.         int highest_value = index;
  102.         int left = left_son(index);
  103.         int right = right_son(index);
  104.         if (left < size)
  105.             if (data[highest_value] < data[left])
  106.                 highest_value = left;
  107.         if (right < size)
  108.             if (data[highest_value] < data[right])
  109.                 highest_value = right;
  110.         if (highest_value != index)
  111.         {
  112.             swap(data[index], data[highest_value]);
  113.             DeleteRootRek(highest_value);
  114.         }
  115.     }
  116.  
  117.     int parent(int index)
  118.     {
  119.         return (index - 1) / 2;
  120.     }
  121.     int left_son(int index)
  122.     {
  123.         return 2 * index + 1;
  124.     }
  125.     int right_son(int index)
  126.     {
  127.         return 2 * index + 2;
  128.     }
  129.  
  130.     void ToString()
  131.     {
  132.         cout << "Kopiec:" << endl;
  133.         cout << "Liczba elementow: " << size << endl;
  134.         int dzielenie = 1;
  135.         for (int i = 0; i < size; i++)
  136.         {
  137.             cout << data[i] << ", ";
  138.         }
  139.         cout << endl;
  140.     }
  141.  
  142.     void ClearHeap()
  143.     {
  144.         cout << "Kopiec zostal wyczyszczony" << endl;
  145.         for (int i = 0; i < size; i++)
  146.         {
  147.             data[i].~T();
  148.         }
  149.         size = 0;
  150.         capacity = 1;
  151.     }
  152.  
  153.     void ChangeTabIntoHeap(int tab_size)
  154.     {
  155.         int par = parent(tab_size - 1);
  156.         if (par >= 0)
  157.         {
  158.             DeleteRootRek(par);
  159.             ChangeTabIntoHeap(tab_size - 1);
  160.         }
  161.     }
  162.     void HeapSort(int tab_size, int flag)
  163.     {
  164.         T* result_tab = new T[tab_size];
  165.         for (int i = tab_size - 1; i >= 0; i--)
  166.         {
  167.             result_tab[i] = DeleteRoot(0);
  168.         }
  169.         if (flag == 1)
  170.         {
  171.             if (tab_size >= 10)
  172.             {
  173.                 for (int i = 0; i < 3; i++)
  174.                 {
  175.                     cout << result_tab[i] << " ";
  176.                 }
  177.                 cout << "(...) ";
  178.                 for (int i = tab_size - 3; i < tab_size; i++)
  179.                 {
  180.                     cout << result_tab[i] << " ";
  181.                 }
  182.             }
  183.             else
  184.             {
  185.                 for (int i = 0; i < tab_size; i++)
  186.                 {
  187.                     cout << result_tab[i] << " ";
  188.                 }
  189.             }
  190.         }
  191.         delete[] result_tab;
  192.     }
  193. };
  194.  
  195. struct Struktura
  196. {
  197.     int pole1;
  198.     Struktura()
  199.     {
  200.         pole1 = rand() % 30000 + 1;
  201.     }
  202.     Struktura(int a)
  203.     {
  204.         pole1 = a;
  205.     }
  206.     bool operator ==(const Struktura&n) const
  207.     {
  208.         return pole1 == n.pole1;
  209.     }
  210.     bool operator >(const Struktura&n) const
  211.     {
  212.         return pole1 > n.pole1;
  213.     }
  214.     bool operator <(const Struktura&n) const
  215.     {
  216.         return pole1 < n.pole1;
  217.     }
  218.     bool operator <=(const Struktura&n) const
  219.     {
  220.         return pole1 <= n.pole1;
  221.     }
  222.     bool operator >=(const Struktura&n) const
  223.     {
  224.         return pole1 >= n.pole1;
  225.     }
  226.     bool operator /(const Struktura&n) const
  227.     {
  228.         return pole1 / n.pole1;
  229.     }
  230.     //bool operator =(const Struktura&n)
  231.     //{
  232.         //return pole1 = n.pole1;
  233.     //}
  234. };
  235.  
  236. ostream& operator <<(ostream&stream, const Struktura&m)
  237. {
  238.     return (stream << m.pole1);
  239. }
  240.  
  241. template <typename T>
  242. T FindRange(T *tab, int tab_size)
  243. {
  244.     T max;
  245.     max = tab[0];
  246.     for (int i = 1; i < tab_size; i++)
  247.     {
  248.         if (tab[i] > max)
  249.             max = tab[i];
  250.     }
  251.     return max;
  252. }
  253.  
  254. template <typename T>
  255. void CountingSort(T *tab, int tab_size)
  256. {
  257.     int range = FindRange(tab, tab_size);
  258.     T* result_tab = new T[tab_size];
  259.     T* count = new T[range + 1];
  260.     //Wyzerowanie tablicy zliczającej
  261.     for (int i = 0; i <= range; i++)
  262.     {
  263.         count[i] = 0;
  264.     }
  265.     //Zliczanie
  266.     for (int i = 0; i < tab_size; i++)
  267.     {
  268.         count[tab[i]] += 1;
  269.     }
  270.     cout << endl;
  271.     //Sprawdzenie
  272.     /*cout << "Zliczanie:" << endl;
  273.     for (int i = 0; i <= range; i++)
  274.     {
  275.         cout << i << "\t" << count[i] << endl;
  276.     }*/
  277.     //Wyznaczenie tablicy wynikowej
  278.     int counter;
  279.     int sum = 0;
  280.     for (int i = 0; i <= range; i++)
  281.     {
  282.         counter = count[i];
  283.         for (int j = 1; j <= counter; j++)
  284.         {
  285.             result_tab[sum] = i;
  286.             sum += 1;
  287.         }
  288.     }
  289.     //Sprawdzenie2
  290.     /*cout << "Posortowane:" << endl;
  291.     for (int i = 0; i < tab_size; i++)
  292.     {
  293.         cout << i << "\t" << result_tab[i] << endl;
  294.     }*/
  295.     //Nadpisanie tablicy wejściowej
  296.     for (int i = 0; i < tab_size; i++)
  297.     {
  298.         tab[i] = result_tab[i];
  299.     }
  300.     delete[] result_tab;
  301.     delete[] count;
  302. }
  303.  
  304. void Bucket_Sort_Int(int *tab, int rozmiar, int m)
  305. {
  306.     //Tworzenie wiader
  307.     vector<vector<int>> bucket(m);
  308.     int max = tab[0];
  309.     for (int i = 0; i < rozmiar; i++)
  310.     {
  311.         if (tab[i] > max)
  312.             max = tab[i];
  313.     }
  314.     //max = max + 1;
  315.     //Wsadzanie elementow do wiader
  316.     for (int i = 0; i < rozmiar; i++)
  317.     {
  318.         bucket[floor((tab[i]) / (double(max)*rozmiar))].push_back(tab[i]);
  319.     }
  320.     //Sortowanie
  321.     for (int i = 0; i < m; i++)
  322.     {
  323.         sort(bucket[i].begin(), bucket[i].end());
  324.     }
  325.     //Wklejanie wiader do wejściowej tablicy
  326.     int index = 0;
  327.     for (int i = 0; i < rozmiar; i++)
  328.     {
  329.         for (int j = 0; j < bucket[i].size(); j++)
  330.         {
  331.             tab[index] = bucket[i][j];
  332.             index = index + 1;
  333.         }
  334.     }
  335.     //Sprawdzenie2
  336.     /*cout << "Posortowane:" << endl;
  337.     for (int i = 0; i < rozmiar; i++)
  338.     {
  339.         cout << i << "\t" << tab[i] << endl;
  340.     }*/
  341. }
  342.  
  343. template <typename T>
  344. void Bucket_Sort_Templates(T *tab, int rozmiar, int m)
  345. {
  346.     //Tworzenie wiader
  347.     vector<vector<T>> bucket(m);
  348.     int max = tab[0].pole1;
  349.     for (int i = 0; i < rozmiar; i++)
  350.     {
  351.         if (tab[i] > max)
  352.             max = tab[i].pole1;
  353.     }
  354.     //max = max + 1;
  355.     //Wsadzanie elementow do wiader
  356.     for (int i = 0; i < rozmiar; i++)
  357.     {
  358.         //bucket[floor((tab[i].pole1) / (double(max)*rozmiar))].push_back(tab[i]);
  359.         bucket[floor((tab[i].pole1 / (double(max)*rozmiar)))].push_back(tab[i]);
  360.     }
  361.     //Sortowanie
  362.     for (int i = 0; i < m; i++)
  363.     {
  364.         sort(bucket[i].begin(), bucket[i].end());
  365.     }
  366.     //Wklejanie wiader do wejściowej tablicy
  367.     int index = 0;
  368.     for (int i = 0; i < rozmiar; i++)
  369.     {
  370.         for (int j = 0; j < bucket[i].size(); j++)
  371.         {
  372.             tab[index] = bucket[i][j];
  373.             index = index + 1;
  374.         }
  375.     }
  376.     //Sprawdzenie2
  377.     /*cout << "Posortowane:" << endl;
  378.     for (int i = 0; i < rozmiar; i++)
  379.     {
  380.         cout << i << "\t" << tab[i] << endl;
  381.     }*/
  382. }
  383.  
  384. void ShortenedTabInt(int *tab, int tab_size)
  385. {
  386.     if (tab_size >= 10)
  387.     {
  388.         for (int i = 0; i < 3; i++)
  389.         {
  390.             cout << tab[i] << " ";
  391.         }
  392.         cout << "(...) ";
  393.         for (int i = tab_size-3; i < tab_size; i++)
  394.         {
  395.             cout << tab[i] << " ";
  396.         }
  397.     }
  398.     else
  399.     {
  400.         for (int i = 0; i < tab_size; i++)
  401.         {
  402.             cout << tab[i] << " ";
  403.         }
  404.     }
  405. }
  406.  
  407. int main()
  408. {
  409.     //int tab1[30] = { 40,25,16,34,27,12,11,5,50,7,1,9,8,6,13,15,37,32,13,11,4,13,2,47,26,39,42,12,11,30 };
  410.     //cout << "Heap sort:" << endl;
  411.     //Heap<int> *heap = new Heap<int>(30, tab1);
  412.     //heap->ChangeTabIntoHeap(30);
  413.     //heap->HeapSort(30, 1);
  414.  
  415.     /*int tab1[10] = { 40,25,16,34,27,12,11,5,50,7 };
  416.     cout << "Tablica przed posortowaniem:" << endl;
  417.     for (int i = 0; i < 10; i++)
  418.         cout << tab1[i] << " ";
  419.     cout << "\n";
  420.     ShortenedTabInt(tab1, 10);
  421.     int tab2[10];
  422.     memcpy(tab2, tab1, 10 * sizeof(int));
  423.     int tab3[10];
  424.     memcpy(tab3, tab1, 10 * sizeof(int));
  425.     cout << "\nCounting sort:";
  426.     CountingSort(tab1, 10);
  427.     for (int i = 0; i < 10; i++)
  428.         cout << tab1[i] << " ";
  429.     cout << "\n";
  430.     ShortenedTabInt(tab1, 10);
  431.     cout << "\nBucket sort:" << endl;
  432.     Bucket_Sort_Int(tab2, 10, 50);
  433.     for (int i = 0; i < 10; i++)
  434.         cout << tab2[i] << " ";
  435.     cout << "\n";
  436.     ShortenedTabInt(tab2, 10);
  437.     cout << "\nHeap sort:" << endl;
  438.     Heap<int> *heap = new Heap<int>(10, tab3);
  439.     heap->ChangeTabIntoHeap(10);
  440.     heap->HeapSort(10, 1);*/
  441.  
  442.     cout << "LICZBY CALKOWITE:" << endl;
  443.     srand(0);
  444.     const int MAX_ORDER = 7;
  445.     const int m = pow(10, 7);
  446.     for (int o = 1; o <= MAX_ORDER; o++)
  447.     {
  448.         const int n = pow(10, o);
  449.         cout << n << " Elementow w tablicy:";
  450.         int* array1 = new int[n];
  451.         for (int i = 0; i < n; i++)
  452.         {
  453.             int rand_val = (rand() % 10000000) + 1;
  454.             array1[i] = rand_val;
  455.         }
  456.         cout << "\nPoczatkowa tablica:\n";
  457.         ShortenedTabInt(array1, n);
  458.         int *array2 = new int[n];
  459.         int *array3 = new int[n];
  460.         memcpy(array2, array1, n * sizeof(int));
  461.         memcpy(array3, array1, n * sizeof(int));
  462.         //CountingSort
  463.         clock_t t1 = clock();
  464.         CountingSort(array1, n);
  465.         ShortenedTabInt(array1, n);
  466.         clock_t t2 = clock();
  467.         cout << "Czas wykonania CountingSort to: " << (double)(t2 - t1) / CLOCKS_PER_SEC << " sekundy" << endl;
  468.         //HeapSort
  469.         clock_t t3 = clock();
  470.         Heap<int> *heap = new Heap<int>(n, array2);
  471.         heap->ChangeTabIntoHeap(10);
  472.         heap->HeapSort(n,1);
  473.         delete heap;
  474.         clock_t t4 = clock();
  475.         cout << "Czas wykonania HeapSort to: " << (double)(t4 - t3) / CLOCKS_PER_SEC << " sekundy" << endl;
  476.         //BucketSort
  477.         clock_t t5 = clock();
  478.         Bucket_Sort_Int(array3, n, 10000000);
  479.         ShortenedTabInt(array3, n);
  480.         clock_t t6 = clock();
  481.         cout << "Czas wykonania BucketSort to: " << (double)(t6 - t5) / CLOCKS_PER_SEC << " sekundy" << endl;
  482.         delete[] array1, array2, array3;
  483.     }
  484.     cout << "STRUKTURY:" << endl;
  485.     for (int o = 1; o <= MAX_ORDER; o++)
  486.     {
  487.         const int n = pow(10, o);
  488.         cout << n << " Elementow w tablicy:" << endl;
  489.         Struktura *array1 = new Struktura[n];
  490.         for (int i = 0; i < n; i++)
  491.         {
  492.             Struktura nowy_obiekt;
  493.             array1[i] = nowy_obiekt;
  494.         }
  495.         Struktura *array2 = new Struktura[n];
  496.         memcpy(array2, array1, n * sizeof(Struktura));
  497.         //HeapSort
  498.         clock_t t1 = clock();
  499.         Heap<Struktura> *heap = new Heap<Struktura>(n, array2);
  500.         heap->ChangeTabIntoHeap(n);
  501.         heap->HeapSort(n, 1);
  502.         delete heap;
  503.         clock_t t2 = clock();
  504.         cout << "Czas wykonania HeapSort to: " << (double)(t2 - t1) / CLOCKS_PER_SEC << " sekundy" << endl;
  505.         //BucketSort
  506.         clock_t t3 = clock();
  507.         Bucket_Sort_Templates(array2, n, 10000000);
  508.         clock_t t4 = clock();
  509.         cout << "Czas wykonania BucketSort to: " << (double)(t4 - t3) / CLOCKS_PER_SEC << " sekundy" << endl;
  510.     }
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement