Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void getData(vector<int>&);
  7. void bubbleSort(vector<int>&);
  8. void insertionSort(vector<int>&);
  9. void selectionSort(vector<int>&);
  10. void printVector(vector<int>&);
  11. void printInfo();
  12.  
  13. int main() {
  14.     int choose;
  15.     vector<int> myArray;
  16.  
  17.     printInfo();
  18.  
  19.     do {
  20.         cin >> choose;
  21.  
  22.         if(choose == 1) {
  23.             getData(myArray);
  24.         } else if(choose == 2) {
  25.             bubbleSort(myArray);
  26.         } else if(choose == 3) {
  27.             insertionSort(myArray);
  28.         } else if(choose == 4) {
  29.             selectionSort(myArray);
  30.         } else if(choose == 5) {
  31.             printVector(myArray);
  32.         }
  33.  
  34.         cout << endl;
  35.         printInfo();
  36.     } while(choose != 6);
  37.  
  38.     return 0;
  39. }
  40.  
  41. void getData(vector<int>& myArray) {
  42.     int element;
  43.     int n;
  44.  
  45.     cout << "Podaj rozmiar tablicy: ";
  46.     cin >> n;
  47.  
  48.     for(int i = 0; i < n; i++) {
  49.         cout << endl << "Podaj element " << i << ": ";
  50.         cin >> element;
  51.         myArray.push_back(element);
  52.     }
  53. }
  54.  
  55. void bubbleSort(vector<int> & a) {
  56.     bool swapp = true;
  57.  
  58.     while(swapp) {
  59.             swapp = false;
  60.             for(size_t i = 0; i < a.size()-1; i++) {
  61.                 if(a[i] > a[i + 1]) {
  62.                     a[i] += a[i + 1];
  63.                     a[i + 1] = a[i] - a[i + 1];
  64.                     a[i] -= a[i + 1];
  65.                     swapp = true;
  66.                 }
  67.             }
  68.     }
  69.  
  70.     cout << "Posortowalem babelkowo" << endl;
  71. }
  72.  
  73. void insertionSort(vector<int> & data) {
  74.     int i, j, tmp;
  75.  
  76.     for(i = 1; i < data.size(); i++) {
  77.         j = i;
  78.         tmp = data[i];
  79.  
  80.         while(j > 0 && tmp < data[j - 1]) {
  81.             data[j] = data[j - 1];
  82.             j--;
  83.         }
  84.  
  85.         data[j] = tmp;
  86.     }
  87.  
  88.     cout << "Posortowalem przez wstawianie" << endl;
  89.  }
  90.  
  91.  void selectionSort(vector<int> & data) {
  92.      int vecsize = data.size();
  93.  
  94.      for(int j = 0; j < vecsize - 1; ++j) {
  95.         int minimum = j;
  96.  
  97.         for(int i = j + 1; i < vecsize; ++i) {
  98.             if (data.at(minimum) > data.at(i)) {
  99.                 minimum = i;
  100.             }
  101.         }
  102.  
  103.         if (minimum != j)
  104.             swap(data.at(j), data.at(minimum));
  105.     }
  106.  
  107.     cout << "Posortowalem przez wybieranie" << endl;
  108. }
  109.  
  110. void printVector(vector<int>& path) {
  111.     for(vector<int>::const_iterator i = path.begin(); i != path.end(); ++i)
  112.         cout << *i << " ";
  113.  
  114.     cout << endl << endl;
  115. }
  116.  
  117. void printInfo() {
  118.     cout << "1 - Pobranie danych" << endl;
  119.     cout << "2 - Sortowanie babelkowe" << endl;
  120.     cout << "3 - Sortowanie przez wstawianie" << endl;
  121.     cout << "4 - Sortowanie przez wybieranie" << endl;
  122.     cout << "5 - Wydruk tablicy" << endl;
  123.     cout << "6 - Koniec programu" << endl;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement