归并排序代码

来源:互联网 发布:java p2p网贷系统源码 编辑:程序博客网 时间:2024/04/29 21:26
#include <iostream>#include <math.h>#include <fstream>#include <stdio.h>#include <stdlib.h>#include <time.h>using namespace std;int Merge_(int low, int mid, int high, int *A){    int i, j, h, k;    h = low;    i = low;    j = mid + 1;    int B[high - low + 1];    while(h <= mid && j <= high)    {        if(A[h] <= A[j])        {            B[i-low] = A[h];            h++;        }        else        {            B[i-low] = A[j];            j++;        }        i++;    }    if(h > mid)    {        for(k = j; k <= high; k++)        {            B[i-low] = A[k];            i++;        }    }    else    {        for(k = h; k <= mid; k++)        {            B[i-low] = A[k];            i++;        }    }    for(k = low; k <= high; k++)    {        A[k] = B[k-low];    }    return 0;}int MergeSortL(int low, int high, int* A){    int mid;    if(low < high)    {        mid = floor((low + high)/2);        MergeSortL(low, mid, A);        MergeSortL(mid + 1, high, A);        Merge_(low, mid, high, A);    }    return 0;}int main(){    clock_t start, finish;    double duration;    start = clock();    ifstream in("array8.txt");    ofstream out("result8.txt");    int count_;    in >> count_;    int A[count_];    for(int i = 0; i < count_; i++)    {        in >> A[i];    }    MergeSortL(0, count_ - 1, A);    finish = clock();    duration = (double)(finish - start)/CLOCKS_PER_SEC;    cout << "ºÄʱ£º" << duration << "*****";    out << "ºÄʱ£º" << duration << "*****";    for(int i = 0; i < count_ - 1; i++)    {        out << A[i] << ",";    }    out << A[count_ - 1];    cout << "The project is finished!" << endl;    return 0;}

0 0