#include<bits/stdc++.h>
using namespace std;
int a[] = {2,1,3,4,5,9,10,11,4,19};
int tmp[20];
void m_sort(int a[], int L, int R)
{
if(L>=R){
return ;
}
int mid = (L+R)/2;
cout <<"左侧: ";
for(int i=L; i<=mid; i++){
cout << a[i] << " ";
}
cout <<"右侧: ";
for(int i=mid+1; i<=R; i++){
cout << a[i] << " ";
}
cout << endl;
m_sort(a, L , mid);
m_sort(a, mid+1, R);
int i = L , j = mid+1, k = L;
while(i <= mid && j <= R){
if(a[i] < a[j]){
tmp[k++] = a[i++] ;
}else{
tmp[k++] = a[j++];
}
}
while(i<=L){
tmp[k++] = a[i++];
}
while(j<=R){
tmp[k++] = a[j++];
}
for(int i=L; i<=R;i++){
a[i]= tmp[i];
}
return ;
}
int main(){
for(int i=0; i<10; i++){
cout <<a[i] << " ";
}
cout << endl;
m_sort(a, 0, 9);
for(int i=0; i<10; i++){
cout <<a[i] << " ";
}
return 0;
}
—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com