Advertisement
Guest User

Untitled

a guest
Mar 8th, 2021
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <array>
  4. std::vector<int> StalinSort(std::array<int, 500> randomarray);
  5. int main() {
  6. std::vector<int> finalArray;
  7. std::array<int, 500> randomarray;
  8. std::mt19937 RNGine;
  9. std::uniform_int_distribution<int> EngineDistributor(0, 49);
  10. for (auto& element : randomarray) {
  11. element = EngineDistributor(RNGine);
  12. }
  13. std::cout << "Before Stalin sort:" << std::endl;
  14. for (auto element : randomarray) {
  15. std::cout << element << ' ';
  16. }
  17.  
  18. std::cout << std::endl << std::endl << "After Stalin sort:" << std::endl;
  19.  
  20. finalArray = StalinSort(randomarray);
  21. for (auto element : finalArray) {
  22. std::cout << element << ' ';
  23. }
  24. std::cout << std::endl;
  25. return 0;
  26. }
  27. #include <vector>
  28. std::vector<int> StalinSort(std::array<int, 500> randomarray) {
  29. unsigned int j = 1;
  30. for (size_t i = 0; i < randomarray.size() - j;) {
  31. if (randomarray[i] <= randomarray[i + j]) {
  32. ++i;
  33. j = 1;
  34. }
  35. else {
  36. randomarray[i + j] = 0;
  37. ++j;
  38. }
  39. }
  40.  
  41. std::vector<int> NewArray;
  42. j = 0;
  43. for (size_t i = 0; i < randomarray.size(); ++i) {
  44. if (randomarray[i] != 0) {
  45. NewArray.push_back(randomarray[i]);
  46. j++;
  47. }
  48. }
  49. return NewArray;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement