Advertisement
Stybyk

Sorty

Oct 28th, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <windows.h>
  7.  
  8. using namespace std;
  9.  
  10. void generateList(int * list, int size,int random);
  11. void printList(int * list, int size);
  12. void bubbleSort(int * list, int size);
  13. void insertionSort(int * list, int size);
  14. void selectionSort(int * list, int size);
  15.  
  16. int main(){
  17. char input;
  18. const int lenght = 1000;
  19. const int size_of_array = 200;
  20. int pole[lenght];
  21.  
  22. srand((int)time(NULL));// Generator nahodnych cisel
  23.  
  24. //Hlavicka programu
  25. cout << "-------------------------------" << endl;
  26. cout << "VSB-TUO - FEI - IVT" << endl;
  27. cout << "APPS - Threads Nr.1" << endl;
  28. cout << "Author: M. Zwieryzna / ZWI0009" << endl;
  29. cout << "Lenght of array: " << lenght << endl;
  30. cout << "Random numbers: " << size_of_array << endl;
  31. cout << "-------------------------------" << endl;
  32. Sleep(3000);
  33.  
  34. // Generovani pole
  35. cout << "Random array generated." << endl;
  36. generateList(pole, lenght,size_of_array); // Funkce pro generaci a naplneni pole
  37.  
  38. // Tisk pole
  39. cout << "-------------------------------" << endl;
  40. cout << "Print array?" << endl;
  41. cout << "Y - Yes" << endl;
  42. cout << "N - No" << endl;
  43. cout << "-------------------------------" << endl;
  44. cout << "System is waiting for response..." << endl;
  45.  
  46. cin >> input;
  47. switch(input)
  48.    {
  49.    case 'Y':
  50.       {
  51.       cout << "\nGenerated list:" << endl;
  52.       printList(pole, lenght); // Funkce pro vypis pole
  53.       }
  54.    }
  55.  
  56. // Vyber typu trizeni
  57. cout << "-------------------------------" << endl;
  58. cout << "Select sorting type:" << endl;
  59. cout << "A - Bubble Sort" << endl;
  60. cout << "B - Insertion Sort" << endl;
  61. cout << "C - Selection Sort" << endl;
  62. cout << "-------------------------------" << endl;
  63. cout << "System is waiting for response..." << endl;
  64.  
  65. cin >> input;
  66. switch(input)
  67.    {
  68.    case 'A':
  69.       {
  70.       bubbleSort(pole, lenght);
  71.       }
  72.    case 'B':
  73.       {
  74.       insertionSort(pole, lenght);
  75.       }
  76.    case 'C':
  77.       {
  78.       selectionSort(pole, lenght);
  79.       }
  80.    }  
  81.  
  82. // Tisk pole
  83. cout << "-------------------------------" << endl;
  84. cout << "Print array?" << endl;
  85. cout << "Y - Yes" << endl;
  86. cout << "N - No" << endl;
  87. cout << "-------------------------------" << endl;
  88. cout << "System is waiting for response..." << endl;
  89.  
  90. cin >> input;
  91. switch(input)
  92.    {
  93.    case 'Y':
  94.       {
  95.       cout << "\nSorted list:" << endl;
  96.       printList(pole, lenght); // Funkce pro vypis pole
  97.       }
  98.    }
  99. }
  100.  
  101. void generateList(int * list, int size, int random)
  102. {
  103. for (int i=0; i<size;i++)
  104.    {
  105.    list[i]=rand()%200 +1;
  106.    }
  107. }
  108. void printList(int * list, int size)
  109. {
  110. for (int i=0; i<size;i++)
  111.    {
  112.       cout << "[" << i << "]: {" << list[i] << "} " << endl;
  113.    }
  114. }
  115.  
  116. void bubbleSort(int * list, int size)
  117. {
  118. int help;
  119. for(int i=0;i<size;i++)
  120.    {
  121.    for(int j=0;j<size;j++)
  122.       {
  123.       if(list[j+1] < list[j])
  124.          {
  125.          help = list[j+1];
  126.          list[j+1] = list[j];
  127.          list[j]=help;
  128.          }
  129.       }
  130.    }
  131. }
  132.  
  133. void insertionSort(int * list, int size){
  134. int help;
  135. for(int i=0;i<size;i++)
  136.    {
  137.    int j = i + 1;
  138.    help = list[j];
  139.    while(j > 0 && help < list[j-1])
  140.       {
  141.       list[j] = list[j-1];
  142.       j--;
  143.       }
  144.    list[j] = help;
  145.    }
  146. }
  147.  
  148. void selectionSort(int * list, int size)
  149. {
  150. int help;
  151. for(int i = 0; i < size; i++)
  152.    {
  153.    int maxIndex = i;
  154.    for(int j=i+1; j < size; j++)
  155.       {
  156.       if(list[j] < list[maxIndex])
  157.          {
  158.          maxIndex =j;
  159.          }
  160.       }
  161.    help = list[i];
  162.    list[i] = list[maxIndex];
  163.    list[maxIndex] = help;
  164.    }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement