首页 > 编程语言 > 详细

数据结构 Merge合并排序

时间:2016-06-11 22:41:14      阅读:285      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {



            int[] a = { 1, 5, 7, 12 };
            int[] b = { 2, 6, 8, 9 };
            int len = a.Length + b.Length;
            int[] c = new int[len];
            int v1_index = 0;
            int v2_index = 0;
            int index = 0;

            while (index <= len)
            {
                if (a[v1_index] < b[v2_index])
                {
                    c[index++] = a[v1_index];
                    v1_index++;
                }
                else if (a[v1_index] == b[v2_index])
                {
                    c[index++] = a[v1_index];
                    c[index++] = b[v2_index];
                    v1_index++;
                    v2_index++;
                }
                else
                {
                    c[index++] = b[v2_index];
                    v2_index++;
                }

                if (v1_index == a.Length || v2_index == b.Length)
                {
                    break;
                }

            }
            if (v1_index == a.Length)
            {
                for (int i =v2_index; i < b.Length; i++)
                {
                    c[index++] = b[i];

                }
            }
            else
            {
                for (int i = v1_index; i < a.Length; i++)
                {
                    c[index++] = a[i];

                }

            }

        }
    }
}

 

数据结构 Merge合并排序

原文:http://www.cnblogs.com/kexb/p/5576018.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!