Divison and Recursion-MergeSort
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Divison and Recursion-MergeSort相关的知识,希望对你有一定的参考价值。
#include<iostream>
using namespace std;
void DoMerge(int array[], int buff[], int begin, int middle, int end){
int leftHalfBegin = begin;
int leftHalfEnd = middle;
int rightHalfBegin = middle+1;
int rightHalfEnd = end;
int index = begin;
while (leftHalfBegin <= middle && rightHalfBegin <= end){
if (array[leftHalfBegin] < array[rightHalfBegin]){
buff[index] = array[leftHalfBegin];
index++;
leftHalfBegin++;
}else{
buff[index] = array[rightHalfBegin];
index++;
rightHalfBegin++;
}
}
while (leftHalfBegin <= middle){
buff[index++] = array[leftHalfBegin++];
}
while(rightHalfBegin <= end)
{
buff[index++] = array[rightHalfBegin++];
}
for(int i = begin; i<= end;i++){
array[i] = buff[i];
}
}
void MergeSort(int array[], int buff[], int begin, int end)
{
int middle = (begin+end)/2;
if (begin < end){
MergeSort(array, buff, begin, middle);
MergeSort(array, buff, middle + 1, end);
DoMerge(array, buff, begin, middle, end);
}
}
int main(){
int array[6]={3,5,9,8,7,5};
int buff[6];
MergeSort(array, buff, 0, 5);
for(int i =0;i<6;i++){cout<<array[i]<<endl;}
}
以上是关于Divison and Recursion-MergeSort的主要内容,如果未能解决你的问题,请参考以下文章
(OrElse and Or) and (AndAlso and And) - 啥时候使用?
Alert and Action sheets and Timer and Animation