Advertisement
Guest User

Sortowanie-menu

a guest
Apr 6th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <fstream>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11.  
  12. clock_t start, stop;
  13. long long int compares, switches;
  14. const int genMin=0;
  15. const int genMax=10;
  16. int arraySize=100000;
  17. const bool print = 0;
  18. const int tests=10;
  19. bool keyb=0;
  20. bool error=0;
  21. //int numbers[10];
  22.  
  23. fstream file;
  24.  
  25. int isNumber(string thing){
  26.     for(int i=0; i<thing.length(); i++){
  27.         if(thing[i]!=45 && (thing[i]<48 || thing[i]>57))
  28.             return 0;
  29.     }
  30.     return 1;
  31. }
  32.  
  33. int checkFile(){
  34.     string nr;
  35.     if(!file.good()){
  36.         cout<<"Brak pliku data.txt"<<endl;
  37.         return 0;
  38.     }
  39.  
  40.     for(int i=0; true; i++){
  41.         file >> nr;
  42.         //fscanf(file, "%s\n", &nr);
  43.         //printf("%d %s ", i+1, nr);
  44.  
  45.         //cout<<nr<<" : "<<isNumber(nr)<<endl;
  46.         if(file.eof()){
  47.             //cout<<"h"<<endl;
  48.             return i;
  49.         }
  50.  
  51.         if(!isNumber(nr)){
  52.             //cout<<nr<<endl;
  53.             return 0;
  54.         }
  55.     }
  56. }
  57.  
  58. void loadFile(int tab[]){
  59.     int fileSize=checkFile();
  60.     //cout<<fileSize<<endl;
  61.     if(fileSize){
  62.         //fclose(file);
  63.         fstream f;
  64.         f.open("data.txt");
  65.         for(int i=0; i<arraySize; i++){
  66.             f >> tab[i];
  67.             //fscanf(f, "%d\n", &numbers[i]);
  68.         }
  69.         //fclose(f);
  70.     }else{
  71.         error++;
  72.         cout<<"dane nieprawidlowe"<<endl;
  73.     }
  74. }
  75.  
  76. void keyboard(int tab[]){
  77.     for (int i = 0; i < arraySize; i++) {
  78.         string z;
  79.         cin>>z;
  80.         if (isNumber(z)==1){
  81.             stringstream geek(z);
  82.             int x ;
  83.             geek >> x;
  84.             tab[i]=x;
  85.         }else{
  86.             error++;
  87.             printf("Niepoprawna wartosc wprowadzona");
  88.             break;
  89.         }
  90.     }
  91. }
  92.  
  93. void swapp(int* a, int* b){
  94.     switches++;
  95.     int t = *a;
  96.     *a = *b;
  97.     *b = t;
  98. }
  99.  
  100. void generateArray(int *tab, int s, int e){
  101.     for (int i = 0; i < arraySize; i++) {
  102.         tab[i] = (rand()%(e-s)+s);
  103.     }
  104. }
  105.  
  106. double timer() {
  107.     return ((double)(stop - start) / CLOCKS_PER_SEC);
  108. }
  109.  
  110. void printArray(int tab[]) {
  111.     if(print){
  112.         for (int i = 0; i < arraySize; i++) {
  113.             cout << tab[i] << " ";
  114.         }
  115.         cout << endl;
  116.     }
  117. }
  118.  
  119. void printArrayAbs(int tab[]) {
  120.  
  121.     for (int i = 0; i < arraySize; i++) {
  122.         cout << tab[i] << " ";
  123.     }
  124.     cout << endl;
  125. }
  126.  
  127. void insertionSort(int arr[]) {
  128.     int tab[arraySize];
  129.     memcpy(tab, arr, sizeof(tab));
  130.     compares = 0;
  131.     switches = 0;
  132.     start = clock();
  133.  
  134.     int i, key, j;
  135.     for (i = arraySize-2; i >= 0; i--) {
  136.         key = tab[i];
  137.         j = i + 1;
  138.  
  139.         compares++;
  140.         while (j < arraySize && tab[j] > key) {
  141.             compares++;
  142.             switches++;
  143.             tab[j - 1] = tab[j];
  144.             j = j + 1;
  145.         }
  146.         tab[j - 1] = key;
  147.     }
  148.  
  149.     stop = clock();
  150.     if(keyb==1){
  151.         printArrayAbs(tab);
  152.     }
  153. }
  154.  
  155. int partitionn(int arr[], int low, int high){
  156.     int pivot = arr[high];
  157.     int i = (low - 1);
  158.  
  159.     for (int j = low; j <= high - 1; j++)
  160.     {
  161.  
  162.         if (arr[j] > pivot)
  163.         {
  164.             compares++;
  165.             i++;
  166.             swapp(&arr[i], &arr[j]);
  167.         }
  168.     }
  169.     swapp(&arr[i + 1], &arr[high]);
  170.     return (i + 1);
  171. }
  172.  
  173. void quickSort(int arr[], int low, int high){
  174.     if (low < high)
  175.     {
  176.         int pi = partitionn(arr, low, high);
  177.         if(keyb)
  178.             cout<<arr[pi]<<" ";
  179.  
  180.         quickSort(arr, low, pi - 1);
  181.         quickSort(arr, pi + 1, high);
  182.     }
  183. }
  184.  
  185. void setQuickSort(int arr[]) {
  186.  
  187.     int tab[arraySize];
  188.     memcpy(tab, arr, sizeof(tab));
  189.     compares = 0;
  190.     switches = 0;
  191.     if(keyb)
  192.         cout<<"Pivot: ";
  193.     start = clock();
  194.  
  195.     quickSort(tab, 0, arraySize-1);
  196.     printf("\n");
  197.  
  198.     stop = clock();
  199.     if(keyb==1)
  200.         printArrayAbs(tab);
  201. }
  202.  
  203. void mergee(int arr[], int l, int m, int r){
  204.     //switches++;
  205.     int i, j, k;
  206.     int n1 = m - l + 1;
  207.     int n2 = r - m;
  208.  
  209.     int L[n1], R[n2];
  210.  
  211.     for (i = 0; i < n1; i++)
  212.         L[i] = arr[l + i];
  213.     for (j = 0; j < n2; j++)
  214.         R[j] = arr[m + 1 + j];
  215.  
  216.     i = 0;
  217.     j = 0;
  218.     k = l;
  219.     while (i < n1 && j < n2)
  220.     {
  221.         compares++;
  222.         if (L[i] >= R[j])
  223.         {
  224.             arr[k] = L[i];
  225.             i++;
  226.         }
  227.         else
  228.         {
  229.             arr[k] = R[j];
  230.             j++;
  231.         }
  232.         k++;
  233.     }
  234.  
  235.     while (i < n1)
  236.     {
  237.         arr[k] = L[i];
  238.         i++;
  239.         k++;
  240.     }
  241.  
  242.     while (j < n2)
  243.     {
  244.         arr[k] = R[j];
  245.         j++;
  246.         k++;
  247.     }
  248. }
  249.  
  250. void mergeSort(int arr[], int l, int r){
  251.     if (l < r)
  252.     {
  253.         int m = l + (r - l) / 2;
  254.         mergeSort(arr, l, m);
  255.         mergeSort(arr, m + 1, r);
  256.  
  257.         mergee(arr, l, m, r);
  258.     }
  259. }
  260.  
  261. void setMergeSort(int arr[]){
  262.     int tab[arraySize];
  263.     memcpy(tab, arr, sizeof(tab));
  264.     compares = 0;
  265.     switches = 0;
  266.     start = clock();
  267.  
  268.     mergeSort(tab, 0, arraySize-1);
  269.  
  270.     stop = clock();
  271.     if(keyb)
  272.         printArrayAbs(tab);
  273. }
  274.  
  275. void heapify(int arr[], int n, int i){
  276.  
  277.     int smallest = i;
  278.     int l = 2 * i + 1;
  279.     int r = 2 * i + 2;
  280.  
  281.     if (l < n && arr[l] < arr[smallest]){
  282.         compares++;
  283.         smallest = l;
  284.     }
  285.  
  286.     if (r < n && arr[r] < arr[smallest]){
  287.         compares++;
  288.         smallest = r;
  289.     }
  290.  
  291.     if (smallest != i) {
  292.         compares++;
  293.         swapp(&arr[i], &arr[smallest]);
  294.         heapify(arr, n, smallest);
  295.     }
  296. }
  297.  
  298. void heapSort(int arr[]){
  299.     int tab[arraySize];
  300.     memcpy(tab, arr, sizeof(tab));
  301.     compares = 0;
  302.     switches = 0;
  303.     start = clock();
  304.  
  305.     for (int i = arraySize / 2 - 1; i >= 0; i--)
  306.         heapify(arr, arraySize, i);
  307.  
  308.     for (int i = arraySize - 1; i >= 0; i--) {
  309.         swapp(&arr[0], &arr[i]);
  310.         heapify(arr, i, 0);
  311.     }
  312.     stop = clock();
  313.     if(keyb==1)
  314.         printArrayAbs(arr);
  315.  
  316. }
  317.  
  318. void shellSort(int arr[]){
  319.     int tab[arraySize];
  320.     memcpy(tab, arr, sizeof(tab));
  321.     compares = 0;
  322.     switches = 0;
  323.     printArray(tab);
  324.     if(keyb)
  325.         cout<<"Gap: ";
  326.     start = clock();
  327.  
  328.     for (int gap = arraySize / 2; gap > 0; gap /= 2){
  329.         for (int i = gap; i < arraySize; i += 1){
  330.             int temp = tab[i];
  331.             int j;
  332.             compares++;
  333.             for (j = i; j >= gap && tab[j - gap] > temp; j -= gap){
  334.                 switches++;
  335.                 compares++;
  336.                 tab[j] = tab[j - gap];
  337.             }
  338.             tab[j] = temp;
  339.         }
  340.         if(keyb)
  341.             cout<<gap<<" ";
  342.     }
  343.     stop=clock();
  344.     if(keyb)
  345.         cout<<endl;
  346.  
  347.     if(keyb==1){
  348.         printArrayAbs(tab);
  349.     }
  350. }
  351.  
  352. void formArray(int tab[], int s, int e, int type){
  353.     if(type==0){
  354.         generateArray(tab, s, e);
  355.     }
  356.     if(type==1){
  357.         for(int i=0; i<arraySize; i++){
  358.             tab[i]=i;
  359.         }
  360.     }
  361.     if(type==2){
  362.         for(int i=0; i<arraySize; i++){
  363.             tab[i]=arraySize-i-1;
  364.         }
  365.     }
  366.     if(type==3){
  367.         if(arraySize%2){
  368.             for(int i=0; i<=arraySize/2; i++){
  369.                 tab[arraySize/2-i]=2*i;
  370.                 tab[arraySize/2+i+1]=2*i+1;
  371.             }
  372.         }else{
  373.             for(int i=0; i<=arraySize/2-1; i++){
  374.                 tab[arraySize/2-1-i]=2*i;
  375.                 tab[arraySize/2+i]=2*i+1;
  376.             }
  377.         }
  378.     }
  379.     if(type==4){
  380.         if(arraySize%2){
  381.             for(int i=0; i<=arraySize/2; i++){
  382.                 tab[arraySize/2-i-1]=arraySize-2*i-2;
  383.                 tab[arraySize/2+i]=arraySize-2*i-1;
  384.             }
  385.         }else{
  386.             for(int i=0; i<=arraySize/2-1; i++){
  387.                 tab[arraySize/2-1-i]=arraySize - 2*i-2;
  388.                 tab[arraySize/2+i]=arraySize - 2*i-1;
  389.             }
  390.         }
  391.     }
  392. }
  393.  
  394. void menu(int &choice, int &sortC){
  395.     cout<<"1. Generuj liczby"<<endl;
  396.     cout<<"2. Wczytaj liczby z pliku"<<endl;
  397.     cout<<"3. Wczytaj liczby z klawiatury"<<endl;
  398.     choice=(getch()-48)*10;
  399.     system("cls");
  400.  
  401.     if(choice==10){
  402.         cout<<"Typ generowanej tablicy"<<endl;
  403.         cout<<"1. Randomowa"<<endl;
  404.         cout<<"2. Rosnaca"<<endl;
  405.         cout<<"3. Malejaca"<<endl;
  406.         cout<<"4. V-ksztaltna"<<endl;
  407.         cout<<"5. A-ksztaltna"<<endl;
  408.         choice+=(getch()-49);
  409.         system("cls");
  410.     }
  411.  
  412.     cout<<"Wielkosc tablicy: ";
  413.     cin>>arraySize;
  414.     system("cls");
  415.  
  416.     cout<<"Sortowanie"<<endl;
  417.     cout<<"1. Insertion Sort"<<endl;
  418.     cout<<"2. Shell Sort"<<endl;
  419.     cout<<"3. Heap Sort"<<endl;
  420.     cout<<"4. Merge Sort"<<endl;
  421.     cout<<"5. Quick Sort"<<endl;
  422.     sortC=getch()-48;
  423.     system("cls");
  424. }
  425.  
  426. void head(){
  427.     int choice=1;
  428.     int sortC=1;
  429.     menu(choice, sortC);
  430.     //cout<<arraySize<<endl;
  431.     //cout<<choice;
  432.     int tab[arraySize];
  433.  
  434.     keyb=(choice/10==3);
  435.  
  436.     if(choice/10==1){
  437.         formArray(tab, genMin, genMax, choice%10);
  438.         printArray(tab);
  439.     }
  440.     if(choice/10==2){
  441.         file.open("data.txt");
  442.         loadFile(tab);
  443.         printArray(tab);
  444.     }
  445.     if(choice/10==3){
  446.         keyboard(tab);
  447.     }
  448.     if(!error){
  449.         switch(sortC)
  450.         {
  451.         case 1:
  452.             printf("Insertion Sort:\n");
  453.             insertionSort(tab);
  454.             break;
  455.         case 2:
  456.             printf("Shell Sort:\n");
  457.             shellSort(tab);
  458.             break;
  459.         case 3:
  460.             printf("Heap Sort:\n");
  461.             heapSort(tab);
  462.             break;
  463.         case 4:
  464.             printf("Merge Sort:\n");
  465.             setMergeSort(tab);
  466.             break;
  467.         case 5:
  468.             printf("Quick Sort:\n");
  469.             setQuickSort(tab);
  470.             break;
  471.         }
  472.         printf("Time:\t  %.3f \n", timer());
  473.         printf("Compares: %.i \n", compares);
  474.         printf("Switches: %.i \n", switches);
  475.         printf("\n");
  476.     }
  477.     getch();
  478. }
  479.  
  480. int main()
  481. {
  482.     srand(time(NULL));      //seed
  483.     head();
  484.  
  485.     return 0;
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement