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

归并排序

时间:2019-12-18 21:57:11      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:pac   i++   mes   归并   clu   div   temp   amp   out   

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>


using namespace std;


void merge(int A[], int left,int mid,int right){
   int p1 = left;
   int p2 = mid + 1;
   int temp[right-left+1];
   int i = 0;
   while(p1 <= mid && p2 <= right){
        temp[i++] = A[p1] > A[p2] ? A[p2++] : A[p1++];
   }
   while(p1 <= mid){
        temp[i++] = A[p1++];
   }
   while(p2 <= right){
        temp[i++] = A[p2++];
   }

   for(int i = left; i <= right; i++){
        A[i] = temp[i-left];
   }
}
void sort(int A[], int left,int right) {
    if(left == right){
       return;
    }
    int mid = left + ((right-left) >> 2);
    sort(A,left,mid);
    sort(A,mid+1,right);
    merge(A,left,mid,right);
}

int main(){
    int a[] = {5,4,7,9,3,8,2,1};
    sort(a,0,8);
    for(int i = 0; i < 8;i++){
        cout<<a[i]<<" ";
    }
    return 0;
}

   

归并排序

标签:pac   i++   mes   归并   clu   div   temp   amp   out   

原文地址:https://www.cnblogs.com/buaaZhhx/p/12063608.html

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