Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
merge-sort
//g++ 7.4.0 #include <bits/stdc++.h> using namespace std; void merge(vector<int>&v,int lo,int mid,int hi,int n) { int i=lo;int j=mid+1; int k=lo; vector<int>tmp(n); while(i<=mid && j<=hi) { if(v[i]<=v[j]) { tmp[k]=v[i]; i++; k++; } else if(v[i]>v[j]) { tmp[k]=v[j]; j++;k++; } } if(i>mid) { while(j<=hi) { tmp[k]=v[j]; j++; k++; } } else { while(i<=mid) { tmp[k]=v[i]; k++; i++; } } for(int q=lo;q<=hi;q++) { v[q]=tmp[q]; } } void mergesort(vector<int>&v,int lo,int hi,int n) { if(lo>=hi) return ; int mid=(lo+hi)/2; mergesort(v,lo,mid,n); mergesort(v,mid+1,hi,n); merge(v,lo,mid,hi,n); return; } int main() { int n;cin>>n; vector<int>v(n); for(int i=0;i<n;i++) cin>>v[i]; mergesort(v,0,n-1,n); for(int i=0;i<n;i++) cout<<v[i]<<" "; cout<<endl; }
run
|
edit
|
history
|
help
0
Count of factors
stl_sizeof.cc
PhoneDirectory
СП КИ ЭТАП 2
On Off
Próba
shell sort
PreDir
MINVEST
Job Scheduling