我的MergeSort算法问题C#

时间:2012-05-21 19:44:45

标签: c# algorithm mergesort

using System;

namespace MergeSort
{
    class Program
    {
        static int[] vektor = { 5, 7, 8, 9, 1, 2, 23, 4 };
        static int[] delVektor;
        static int counter = 0;

        static void Main(string[] args)
        {
            PrintVektor(vektor);
            Merge(0, vektor.Length - 1);
            Console.WriteLine("--------");
            PrintVektor(vektor);
            Console.ReadKey();
        }

        static void PrintVektor(int[] vektor)
        {
            foreach (var item in vektor)
            {
                Console.WriteLine(item.ToString() + " ");
            }
        }

        static void Merge(int start, int stop)
        {
            if (start >= stop)
                return;

            int middle = (start + stop) / 2;

            Merge(start, middle);
            Merge(middle + 1, stop);

            delVektor = new int[stop - start + 1];

            int indexStart = start;
            int indexStop = middle + 1;

            while (indexStart <= middle && indexStop <= stop)
            {
                if (vektor[indexStart] < vektor[indexStop])
                {
                    delVektor[counter] = vektor[indexStart];
                    indexStart++;
                    counter++;
                }

                else
                {
                    delVektor[counter] = vektor[indexStop];
                    indexStop++;
                    counter++;
                }
            }

            while (indexStart <= middle)
            {
                delVektor[counter] = vektor[indexStart];
                indexStart++;
                counter++;
            }

            while (middle <= stop)
            {
                delVektor[counter] = vektor[indexStop]; // <---- here i get index out of range
                indexStop++;
                counter++;
            }

            for (int i = 0; i <= delVektor.Length - 1; i++)
            {
                vektor[start + i] = delVektor[i];
            }
        }
    }
}

问题是我得到一个超出范围异常的索引(我已在代码中注释),

while (middle <= stop) {
  delVektor[counter] = vektor[indexStop]; // <---- here i get index out of range
  indexStop++; counter++;
}

我无法弄清楚

我不知道我做错了什么。我一直盯着这段代码已经很长时间了,我只想在试图修复它时立即将计算机扔出窗外。

4 个答案:

答案 0 :(得分:1)

这是你的问题:

if (start >= stop)
            return;

        int middle = (start + stop) / 2;

        Merge(start, middle);
        Merge(middle + 1, stop);

如果return您只是想跳过合并段,则不应该start>=stop。这是一个修复:

int middle = (start + stop) / 2;
if (start < stop){

        Merge(start, middle);
        Merge(middle + 1, stop);
}
//...put the rest of the code which you already have here

答案 1 :(得分:1)

修复了你的代码。 (或许)

using System;

namespace MergeSort{
    class Program{
        static int[] vektor = { 5, 7, 8, 9, 1, 2, 23, 4 };
        static int[] delVektor;
        static int counter = 0;

        static void Main(string[] args){
            PrintVektor(vektor);
            Merge(0, vektor.Length - 1);
            Console.WriteLine("--------");
            PrintVektor(vektor);
            Console.ReadKey();
        }

        static void PrintVektor(int[] vektor){
            foreach (var item in vektor)
                Console.Write(item.ToString() + " ");
            Console.WriteLine("");
        }

        static void Merge(int start, int stop){
            if (start >= stop)
                return;

            int middle = (start + stop) / 2;

            Merge(start, middle);
            Merge(middle + 1, stop);

            delVektor = new int[stop - start + 1];
            counter = 0;//add

            int indexStart = start;
            int indexStop = middle + 1;

            while (indexStart <= middle && indexStop <= stop){
                if (vektor[indexStart] < vektor[indexStop]){
                    delVektor[counter] = vektor[indexStart];
                    indexStart++;
                    counter++;
                } else {
                    delVektor[counter] = vektor[indexStop];
                    indexStop++;
                    counter++;
                }
            }

            while (indexStart <= middle){
                delVektor[counter] = vektor[indexStart];
                indexStart++;
                counter++;
            }

            while (indexStop <= stop){//edit
                delVektor[counter] = vektor[indexStop];
                indexStop++;
                counter++;
            }

            for (int i = 0; i <= delVektor.Length - 1; i++){
                vektor[start + i] = delVektor[i];
            }
        }
    }
}

答案 2 :(得分:0)

当您在数组中使用索引并增加该索引时,您需要测试该索引是否实际位于数组的范围内(代码的从0开始的索引)。

您是否测试了索引,确保它不会超出范围?

答案 3 :(得分:0)

在您获得异常的循环中查看您正在使用的条件。请注意,如果条件在第一次输入时为真,则它将永远为真。然后修复应该是显而易见的。 (提示:循环体中的任何内容都不应更改。)