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

数据结构 Merge合并排序

时间:2016-06-11 22:42:34      阅读:303      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!