fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. mt19937_64 rd(time(0));
  5. int rand(int L, int R){
  6. return L + rd() % (R - L + 1);
  7. }
  8.  
  9. int n;
  10. vector<int> arr;
  11.  
  12. vector<int> slow_merge_sort(vector<int>& arr, int length){
  13. if(length <= 1) return arr;
  14. if(length == 2){
  15. if(arr[0] < arr[1]) swap(arr[0], arr[1]);
  16. return arr;
  17. }
  18.  
  19. int pivot = 3*length/4;
  20. int times[2]; memset(times, 0, sizeof times);
  21. vector<int> newVector[2];
  22. vector<int> resultVector;
  23.  
  24. for(int i = 0; i < pivot; i++) newVector[0].push_back(arr[i]);
  25. for(int i = pivot; i < length; i++) newVector[1].push_back(arr[i]);
  26.  
  27. newVector[0] = slow_merge_sort(newVector[0], newVector[0].size());
  28. newVector[1] = slow_merge_sort(newVector[1], newVector[1].size());
  29.  
  30. while(resultVector.size() < length){
  31. int cur_index = 0;
  32. int cur_value = (int) -1e9;
  33.  
  34. for(int i = 0; i < 2; i++){
  35. if(times[i] >= newVector[i].size()) continue;
  36.  
  37. if(cur_value <= newVector[i][times[i]]){
  38. cur_value = newVector[i][times[i]];
  39. cur_index = i;
  40. }
  41. }
  42.  
  43. resultVector.push_back(cur_value);
  44. times[cur_index]++;
  45. }
  46.  
  47. return resultVector;
  48. }
  49.  
  50. bool isSorted(){
  51. for(int i = 1; i < n; i++){
  52. if(arr[i - 1] < arr[i]) return 0;
  53. }
  54.  
  55. return 1;
  56. }
  57.  
  58. int main(){
  59. ios_base::sync_with_stdio(0);
  60. cin.tie(0); cout.tie(0);
  61.  
  62. cin >> n;
  63. for(int i = 0; i < n; i++){
  64. int value; cin >> value;
  65. arr.push_back(value);
  66. }
  67.  
  68. while(!isSorted()){
  69. arr = slow_merge_sort(arr, n);
  70. }
  71.  
  72. for(int i = 0; i < n; i++) cout << arr[i] << ' ';
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty