Tvor0zhok

Сортировки (методы пузырька, вставки и посредством выбора)

Apr 28th, 2021 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. struct student
  10. {
  11. string f, i, o;
  12. int year;
  13. int m[5];
  14.  
  15. bool operator > (student s)
  16. {
  17. if (f != s.f) return (f > s.f);
  18. else if (i != s.i) return (i > s.i);
  19. else return (o > s.o);
  20. }
  21. };
  22.  
  23. class Base
  24. {
  25. int group;
  26.  
  27. vector <student> st;
  28.  
  29. public:
  30.  
  31. Base (int g) : group(g) {}
  32.  
  33. void push (student s) { st.push_back(s); }
  34.  
  35. void bubble_sort() //сортировка пузырьком
  36. {
  37. for (int i = 0; i < st.size(); ++i)
  38. for (int j = 1; j < st.size() - i; ++j)
  39. if (st[j - 1] > st[j]) swap(st[j - 1], st[j]);
  40. }
  41.  
  42. void insertion_sort() //сортировка вставками
  43. {
  44. for (int i = 0; i < st.size(); ++i)
  45. {
  46. int j = i;
  47.  
  48. while (j > 0 && st[j - 1] > st[j]) { swap(st[j], st[j - 1]); --j; }
  49. }
  50. }
  51.  
  52. void selection_sort() //сортировка посредством выбора
  53. {
  54. for (int i = 0; i < st.size(); ++i)
  55. {
  56. int it = i;
  57.  
  58. for (int j = i + 1; j < st.size(); ++j)
  59. if (st[it] > st[j]) it = j;
  60.  
  61. swap(st[i], st[it]);
  62. }
  63. }
  64.  
  65. void print(ostream &is)
  66. {
  67. is << left;
  68.  
  69. is << "Group: " << group << '\n';
  70.  
  71. is << setw(15) << "Familiya" << setw(10) << "Imya" << setw(15) << "Otchestvo" << setw(5) << "Year" << "Marks\n";
  72.  
  73. for (int i = 0; i < st.size(); ++i)
  74. is << setw(15) << st[i].f << setw(10) << st[i].i << setw(15) << st[i].o << setw(5) << st[i].year << st[i].m[0] << " " << st[i].m[1] << " " << st[i].m[2] << " " << st[i].m[3] << " " << st[i].m[4] << '\n';
  75. }
  76. };
  77.  
  78. int main()
  79. {
  80. ifstream fin ("input.txt");
  81.  
  82. ofstream fbubble ("bubble.txt");
  83. ofstream finsertion ("insertion.txt");
  84. ofstream fselection ("selection.txt");
  85.  
  86. int g; fin >> g; //номер группы
  87.  
  88. Base bubbleBase(g);
  89.  
  90. while (fin.peek() != EOF)
  91. {
  92. student s;
  93.  
  94. fin >> s.f >> s.i >> s.o >> s.year >> s.m[0] >> s.m[1] >> s.m[2] >> s.m[3] >> s.m[4];
  95.  
  96. bubbleBase.push(s);
  97. }
  98.  
  99. Base insertionBase = bubbleBase;
  100. Base selectionBase = bubbleBase;
  101.  
  102. bubbleBase.bubble_sort();
  103. bubbleBase.print(fbubble);
  104.  
  105. insertionBase.insertion_sort();
  106. insertionBase.print(finsertion);
  107.  
  108. selectionBase.selection_sort();
  109. selectionBase.print(fselection);
  110.  
  111. bubbleBase.print(cout);
  112.  
  113. fin.close();
  114.  
  115. fbubble.close();
  116. finsertion.close();
  117. fselection.close();
  118. return 0;
  119. }
  120.  
  121. //input.txt
  122. 211
  123. Ivanov Ivan Ivanovich 2001 2 2 3 4 5
  124. Ivanova Marya Ivanovna 2000 3 3 4 3 4
  125. Ivanov Ivan Ivanovich 2003 4 3 3 4 3
  126. Ivanov Petya Petrovich 2003 5 4 3 4 4
  127. Abramov Mark Pavlovich 2000 3 2 5 2 3
  128. Akimova Victoria Konstantinovna 2002 4 3 3 2 3
  129. Alekseeva Anastasia Nikolaevna 2000 4 3 2 3 3
  130. Andreev Kirill Leonidovich 2003 4 3 4 4 2
  131. Andreev Andrey Egorovich 2000 4 3 3 3 4
  132. Kuzmina Ksenia Tikhonovna 2002 2 4 2 3 5
  133.  
  134. //II 18 сортировка кучей, быстрая сортировка, добавить sort
Add Comment
Please, Sign In to add comment