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

归并排序的非递归算法

时间:2015-04-05 20:11:23      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

  #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);
}

 

归并排序的非递归算法

标签:

原文地址:http://www.cnblogs.com/mymint/p/4394624.html

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