Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. std::vector<int> przezWybor(std::vector<int> liczby) {
  7.     if (liczby.size() == 0) return liczby;
  8.     std::vector<int> sorted;
  9.  
  10.     while (liczby.size() != 0) {
  11.         int min = liczby[0];
  12.         int pos = 0;
  13.         for (int j = 0; j < liczby.size(); j++) {
  14.             if (min > liczby[j]) {
  15.                 min = liczby[j];
  16.                 pos = j;
  17.             }
  18.         }
  19.         liczby.erase(liczby.begin() + pos);
  20.         sorted.push_back(min);
  21.     }
  22.     return sorted;
  23. }
  24.  
  25. std::vector<int> przezWstawianie(std::vector<int> liczby) {
  26.     if (liczby.size() == 0) return liczby;
  27.     std::vector<int> sorted;
  28.  
  29.     while (liczby.size() != 0) {
  30.         int elem = liczby[liczby.size()-1];
  31.         liczby.pop_back();
  32.         int pos = 0;
  33.         while (pos<sorted.size() && sorted[pos]<elem) {
  34.             pos++;
  35.         }
  36.         sorted.insert(sorted.begin() + pos, elem);
  37.     }
  38.     return sorted;
  39. }
  40.  
  41. std::vector<int> bubbleSort(std::vector<int> liczby) {
  42.     if (liczby.size() == 0) return liczby;
  43.  
  44.     bool isSorted = true;
  45.     do {
  46.         isSorted = true;
  47.         for (int i = 0; i < liczby.size() - 1; i++) {
  48.             if (liczby[i] > liczby[i + 1]) {
  49.                 isSorted = false;
  50.                 std::iter_swap(liczby.begin() + i, liczby.begin() + i + 1);
  51.             }
  52.         }
  53.     } while (!isSorted);
  54.     return liczby;
  55. }
  56.  
  57. int main()
  58. {
  59.     srand(time(NULL));
  60.     std::vector<int> liczby;
  61.     for (int i = 0; i < 10; i++) {
  62.         liczby.push_back(std::rand() % 100);
  63.     }
  64.     auto sorted = przezWybor(liczby);
  65.     auto sorted2 = przezWstawianie(liczby);
  66.     auto sorted3 = bubbleSort(liczby);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement