归并排序的非递归算法

  #include<iostream>

  #include<stdio.h>

#include<stdlib.h>
using namespace std;
//智二
//交换数组中两个元素的位置
void swap(int left, int right, int sort[]){

    int temp;
    temp = sort[left];
    sort[left] = sort[right];
    sort[right] = temp;

}

//两个已经排好序的小数组整合成较大的数组
void bodySort(int midnum, int leftstart, int rightEnd, int sort[]){

    for (int i = midnum; i >= leftstart; i--,midnum--)
    {
        for (int j = midnum; j < rightEnd; j++)
        {
            if(sort[j] > sort[j+1]) swap(j,j+1,sort);
            else break;
        }

    }

}

//归并排序进行的拆分工作
void marginSort(int sortnum, int sortArray[]){

    int* tempSort = (int*)malloc(sortnum*sizeof(int));
    int tm = 0;
    for (int i = 0; i < sortnum; i++)  //wwmm
    {
        if(sortArray[i+1] < sortArray[i]){
            tempSort[tm++] = i;
        }
    }
    tempSort[tm++] = sortnum-1;
    
    /***************************************************************/
    cout << "tempSort的初始情况:" << endl;
    cout << "tempSort.size == " << tm << endl;
    for (int i = 0; i < tm; i++)
    {
        cout << "tempSort["<<i<<"]==" << tempSort[i] << " ";
    }
    cout << "---------------------------------------------------" << endl;
    /***************************************************************/
    int count = 1;
    while (tm != 1)
    {
        cout << ""<< count++ <<"次归并:" << endl;
        int tmfl = tm % 2 == 0 ? tm : tm - 1;
        for (int i = 0; i < tmfl; i += 2)
        {
            int startNum = 0;
            if(i != 0)
                startNum = tempSort[i-1]+1;
             bodySort(tempSort[i],startNum,tempSort[i+1],sortArray);
        }
        int ff = 0;
        
        for (int i = 1; i < tmfl; i += 2)
        {
            tempSort[ff++] = tempSort[i];
        }
        if((tm) % 2 != 0){
            
            tempSort[ff++] = tempSort[tm-1];
            
        }    
        tm = ff;
        /***************************************************************/
        cout << "tempSort的情况:" << endl;
        cout << "tempSort.size == " << tm << endl;
        for (int i = 0; i < tm; i++)
        {
            cout << "tempSort["<<i++<<"]==" << tempSort[i] << " ";
        }
        cout << endl;
        /***************************************************************/
    }

    free(tempSort);
}


int main ()//函数的功能:归并排序的非递归算法
{

    int* sortArray;
    int sortnum = 0;
    
    cout << "请输入要排序的元素的长度:" << endl;
    cin >> sortnum;
    sortArray = (int*)malloc(sortnum*sizeof(int));
    if(sortArray == NULL){
        cout << "不能成功分配存储空间!";
        exit(1);
    }
    cout << "请输入元素:" << endl;
    for (int i = 0; i < sortnum; i++)
    {
        cin >> sortArray[i];
    }

    marginSort(sortnum,sortArray);
    
    cout << "排序后的数组元素为:" << endl;
    for (int i = 0; i < sortnum; i++)
    {
        cout << sortArray[i] << " ";
    }
    free(sortArray);
    while (true);
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。