码迷,mamicode.com
首页 > 编程语言 > 详细

归并排序

时间:2019-10-10 11:31:07      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:pac   main   nbsp   getc   归并   assert   namespace   bsp   return   


#include<iostream>
#include<assert.h>
using namespace std;


int temp[20];

void domerge(int*a, int left, int right){

 

for (int i = left; i <= right; i++)
temp[i] = a[i];


int low = left;
int mid = (left + right) / 2;
int high = (left + right) / 2 + 1;

for (int i = left; i <= right; i++){

if (low>mid){
a[i] = temp[high++];
}
else if (high>right){
a[i] = temp[low++];
}
else if (temp[low]<temp[high]){
a[i] = temp[low++];
}
else {
a[i] = temp[high++];
}


}


}
void merge(int*a, int left, int right){
if (left >= right) return;
int mid = (left + right) / 2;

merge(a, left, mid);
merge(a, mid + 1, right);
domerge(a, left, right);

}


int main(){
int temp[12] = { 81, 4, 6, 2, 33, 6, 1, 6, 3, 4, 3, 0 };
merge(temp, 0, 11);

for (int i = 0; i < 12; i++)
cout << temp[i] << " ";
getchar();
return 0;
}

归并排序

标签:pac   main   nbsp   getc   归并   assert   namespace   bsp   return   

原文地址:https://www.cnblogs.com/liangyc/p/11646064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!