English 中文(简体)
与我的合并Sort Algorithm C# 的问题 [非公开]
原标题:Problems with my MergeSort Algorithm C# [closed]
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++;
}

我无法想出办法

我不知道我做错了什么。我一直盯着这个代码看太久了,现在我只想把电脑从窗户扔出去,只要我试着修好它。

最佳回答

当您正在将索引用于数组时, 当您正在将该索引加到该索引中时, 您需要测试该索引是否在数组的界限内( 您的代码为 0 基索引 ) 。

您是否测试您的索引, 以确保它无法用完界限?

问题回答

固定的代码。 (也许)

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];
            }
        }
    }
}

这就是你的问题:

if (start >= stop)
            return;

        int middle = (start + stop) / 2;

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

如果 start>=stop 你只需要跳过合并段,就不应该使用 return 。 这里有一个修正 :

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

查看您在循环中获取例外时使用的条件。 请注意, 如果第一个条目中的条件是真实的, 它将永远是真实的。 修正应该是显而易见的 。 (提示: 循环体中的任何内容不应更改 。)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签