fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void merge2(int a1[], int low, int mid, int high){
  5. int gap = (high-low+2)/2;
  6.  
  7. while(gap>=1){
  8. int i=0,j=i+gap;
  9.  
  10. while(j<=high){
  11.  
  12. if(a1[i]>a1[j]) swap(a1[i],a1[j]);
  13. i++;j++;
  14. }
  15.  
  16. int temp = gap;
  17. gap = (gap+2-1)/2;
  18. if(temp==gap==1) break;
  19. }
  20. }
  21.  
  22. void mergeSort(int a[], int low, int high){
  23. if(low>=high) return;
  24. int mid = low+(high-low)/2;
  25. mergeSort(a,low,mid);
  26. mergeSort(a,mid+1,high);
  27. merge2(a,low,mid,high);
  28.  
  29. }
  30.  
  31. int main(){
  32.  
  33.  
  34. int n; cin>>n;
  35. int arr[n];
  36. for (int i = 0; i < n; i++)
  37. {
  38. cin>>arr[i];
  39. }
  40. mergeSort(arr,0,n-1);
  41. for (int i = 0; i < n; i++)
  42. {
  43. cout<<arr[i]<<'\n';
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5512KB
stdin
5
5 4 3 2 1
stdout
1
2
3
4
5