Advertisement
baadgeorge

Untitled

Nov 6th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<fstream>
  4. #include<string>
  5. #include<sstream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. template <typename T>
  12. int partition(vector<T>& V, int low, int high){
  13. T pivot = V[floor((high + low)/2)];
  14. int i = low;
  15. int j = high;
  16. while (true) {
  17.  
  18. while (V[i] < pivot) {
  19. i++;
  20. }
  21.  
  22. while (V[j] > pivot) {
  23. j--;
  24. }
  25.  
  26. if (i >= j) return j;
  27.  
  28. swap(V[i++], V[j--]);
  29. }
  30.  
  31.  
  32. }
  33.  
  34. template <typename T>
  35. void quicksort(vector<T> &V, int low, int high){
  36.  
  37. if (low < high)
  38. {
  39. T pivot = partition(V, low, high);
  40.  
  41. quicksort(V, low, pivot);
  42. quicksort(V, pivot+1, high);
  43. }
  44. return;
  45. }
  46.  
  47. bool is_number(const string& str) {
  48. return !str.empty() && str.find_first_not_of("-.,0123456789") == string::npos;
  49. }
  50.  
  51. template <typename T>
  52. string parsStr(vector<T>& V, string str) {
  53.  
  54. string delimiter = " ";
  55. string token;
  56. size_t start = 0;
  57. size_t end;
  58.  
  59. stringstream pars_stream(str);
  60.  
  61. if (str.find_first_not_of(" ") == string::npos) return str;
  62.  
  63. while (getline(pars_stream, token, ' ')) {
  64. if (is_number(token)) V.push_back(stoi(token));
  65. else return token;
  66. }
  67. return "";
  68. }
  69.  
  70.  
  71.  
  72.  
  73. int main() {
  74.  
  75. string str;
  76. string buf;
  77. ifstream FILE;
  78. //vector<int> V = { 12,3,4,5,6,78,0 };
  79. vector<int> V ;
  80. FILE.open("input.txt");
  81. while (!FILE.eof()) {
  82. getline(FILE, buf);
  83. str += buf;
  84. }
  85.  
  86. if (str.size() == 0) {
  87. cout << " Empty file!\n";
  88. return -1;
  89. }
  90.  
  91. buf = parsStr(V, str);
  92.  
  93. if (buf != "") {
  94. cout << " File contains characters other than numbers: " << buf << endl;
  95. return -1;
  96. }
  97.  
  98. FILE.close();
  99.  
  100. for (int i = 0; i < V.size(); i++) {
  101. cout << V[i] << " ";
  102. }
  103. cout << endl;
  104.  
  105. quicksort(V, 0, V.size()-1);
  106.  
  107. for (int i = 0; i < V.size(); i++) {
  108. cout << V[i] << " ";
  109. }
  110. cout << endl;
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement