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++;
}
我无法想出办法
我不知道我做错了什么。我一直盯着这个代码看太久了,现在我只想把电脑从窗户扔出去,只要我试着修好它。