MergeSort() 实现返回初始未排序数组
MergeSort() implementation returning the initial unsorted array
每次我 运行 这是一个未排序的数组 returns。我已经放置了一些代码来告诉我事情在哪里发生和没有发生,但它看起来和看起来一切正常,但它实际上不会对任何东西进行排序。
public static int[] MergeSort(int[] array)
{
if(array.Length <= 1)
{
return array;
}
(int[], int[]) p = SplitArray(array);
int[] left = MergeSort(p.Item1);
int[] right = MergeSort(p.Item2);
return Merge(left, right);
}
public static int[] Merge(int[] low, int[] high)
{
int[] middle = new int[(low.Length + high.Length + 1)];
int count = 0;
foreach (int item in low)
{
Console.WriteLine("low: " + item + ',');
}
foreach (int item in high)
{
Console.WriteLine("high: " + item + ',');
}
int low_index = 0;
int high_index = 0;
while (low_index < low.Length && high_index < high.Length)
{
if(low[low_index] < high[high_index])
{
middle.SetValue(low[low_index], count);
low_index++;
count++;
Console.WriteLine("Low inserted.");
}
else
{
middle.SetValue(high[high_index], count);
high_index++;
count++;
Console.WriteLine("High inserted.");
}
}
if (low_index == low.Length)
{
high.CopyTo(middle, high_index);
}
else
{
low.CopyTo(middle, low_index);
}
return middle;
}
public static (int[], int[]) SplitArray(int[] k)
{
int MAXINDEX = k.Length - 1;
int count = 0;
int[] a = new int[(MAXINDEX / 2)];
int[] b = new int[(MAXINDEX / 2)];
for (int i = 0; i < (MAXINDEX / 2); i++)
{
a[i] = k[i];
}
for (int i = ((MAXINDEX / 2) + 1); i < MAXINDEX; i++)
{
b[count] = k[i];
count++;
}
return (a, b);
}
我不知道我哪里出错了。每次回到这里时,我可能只是因为太累而忽略了一些东西。我基本上打印出一堆应该使用控制台发生的事情,这一切看起来都是正确的,我正在失去理智。
好的,我认为主要的错误是 high.copyTo(...) 和 low.copyTo(...) 的函数索引错误
这里是完整的源码(已测试):
public static int[] MergeSort(int[] array)
{
if (array.Length <= 1)
{
return array;
}
Tuple<int[], int[]> p = SplitArray(array);
int[] left = MergeSort(p.Item1);
int[] right = MergeSort(p.Item2);
return Merge(left, right);
}
public static int[] Merge(int[] low, int[] high)
{
int[] middle = new int[low.Length + high.Length];
int count = 0;
foreach (int item in low)
{
Console.WriteLine("low: " + item + ',');
}
foreach (int item in high)
{
Console.WriteLine("high: " + item + ',');
}
int low_index = 0;
int high_index = 0;
while (low_index < low.Length && high_index < high.Length)
{
if (low[low_index] < high[high_index])
{
middle[count] = low[low_index];
low_index++;
count++;
Console.WriteLine("Low inserted.");
}
else
{
middle[count] = high[high_index];
high_index++;
count++;
Console.WriteLine("High inserted.");
}
}
if (low_index == low.Length)
{
for (; count < middle.Length; count++)
{
middle[count] = high[high_index];
high_index++;
}
// alternate: Array.Copy(high, high_index, middle, count, middle.Length - count);
}
else
{
for (; count < middle.Length; count++)
{
middle[count] = low[low_index];
low_index++;
}
// alternate: Array.Copy(low, low_index, middle, count, middle.Length - count);
}
return middle;
}
public static Tuple<int[], int[]> SplitArray(int[] k)
{
int half = k.Length / 2;
int[] a = new int[half];
int[] b = new int[k.Length - half];
int k_index = 0;
for (int i = 0; i < a.Length; i++)
{
a[i] = k[k_index];
k_index++;
}
for (int i = 0; i < b.Length; i++)
{
b[i] = k[k_index];
k_index++;
}
return new Tuple<int[], int[]>(a, b);
}
给你:代码充满了错误,数组的长度不匹配,你每次都在覆盖你的中间数组。
namespace MainClass
{
class Program
{
static void Main(string[] args)
{
int[] array = { 3, 5, 15, 22, 12, 15, 23 };
int[] x = MergeSort(array);
foreach (int i in x)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
public static int[] MergeSort(int[] array)
{
if (array.Length <= 1)
{
return array;
}
(int[], int[]) p = SplitArray(array);
int[] left = MergeSort(p.Item1);
int[] right = MergeSort(p.Item2);
return Merge(left, right);
}
public static int[] Merge(int[] low, int[] high)
{
int[] middle = new int[(low.Length + high.Length)];
int count = 0;
foreach (int item in low)
{
Console.WriteLine("low: " + item + ',');
}
foreach (int item in high)
{
Console.WriteLine("high: " + item + ',');
}
int low_index = 0;
int high_index = 0;
while (low_index < low.Length && high_index < high.Length)
{
if (low[low_index] < high[high_index])
{
middle.SetValue(low[low_index], count);
low_index++;
count++;
Console.WriteLine("Low inserted.");
}
else
{
middle.SetValue(high[high_index], count);
high_index++;
count++;
Console.WriteLine("High inserted.");
}
}
while (high_index < high.Length)
{
middle[low_index + high_index] = high[high_index++];
}
while (low_index < low.Length)
{
middle[low_index + high_index] = low[low_index++];
}
return middle;
}
public static (int[], int[]) SplitArray(int[] k)
{
int[] a = new int[((k.Length + 1) / 2)];
int[] b = new int[(k.Length / 2)];
for (int i = 0; i < a.Length; i++)
{
a[i] = k[i];
}
for (int i = 0; i < b.Length; i++)
{
b[i] = k[i + a.Length];
}
return (a, b);
}
}
每次我 运行 这是一个未排序的数组 returns。我已经放置了一些代码来告诉我事情在哪里发生和没有发生,但它看起来和看起来一切正常,但它实际上不会对任何东西进行排序。
public static int[] MergeSort(int[] array)
{
if(array.Length <= 1)
{
return array;
}
(int[], int[]) p = SplitArray(array);
int[] left = MergeSort(p.Item1);
int[] right = MergeSort(p.Item2);
return Merge(left, right);
}
public static int[] Merge(int[] low, int[] high)
{
int[] middle = new int[(low.Length + high.Length + 1)];
int count = 0;
foreach (int item in low)
{
Console.WriteLine("low: " + item + ',');
}
foreach (int item in high)
{
Console.WriteLine("high: " + item + ',');
}
int low_index = 0;
int high_index = 0;
while (low_index < low.Length && high_index < high.Length)
{
if(low[low_index] < high[high_index])
{
middle.SetValue(low[low_index], count);
low_index++;
count++;
Console.WriteLine("Low inserted.");
}
else
{
middle.SetValue(high[high_index], count);
high_index++;
count++;
Console.WriteLine("High inserted.");
}
}
if (low_index == low.Length)
{
high.CopyTo(middle, high_index);
}
else
{
low.CopyTo(middle, low_index);
}
return middle;
}
public static (int[], int[]) SplitArray(int[] k)
{
int MAXINDEX = k.Length - 1;
int count = 0;
int[] a = new int[(MAXINDEX / 2)];
int[] b = new int[(MAXINDEX / 2)];
for (int i = 0; i < (MAXINDEX / 2); i++)
{
a[i] = k[i];
}
for (int i = ((MAXINDEX / 2) + 1); i < MAXINDEX; i++)
{
b[count] = k[i];
count++;
}
return (a, b);
}
我不知道我哪里出错了。每次回到这里时,我可能只是因为太累而忽略了一些东西。我基本上打印出一堆应该使用控制台发生的事情,这一切看起来都是正确的,我正在失去理智。
好的,我认为主要的错误是 high.copyTo(...) 和 low.copyTo(...) 的函数索引错误
这里是完整的源码(已测试):
public static int[] MergeSort(int[] array)
{
if (array.Length <= 1)
{
return array;
}
Tuple<int[], int[]> p = SplitArray(array);
int[] left = MergeSort(p.Item1);
int[] right = MergeSort(p.Item2);
return Merge(left, right);
}
public static int[] Merge(int[] low, int[] high)
{
int[] middle = new int[low.Length + high.Length];
int count = 0;
foreach (int item in low)
{
Console.WriteLine("low: " + item + ',');
}
foreach (int item in high)
{
Console.WriteLine("high: " + item + ',');
}
int low_index = 0;
int high_index = 0;
while (low_index < low.Length && high_index < high.Length)
{
if (low[low_index] < high[high_index])
{
middle[count] = low[low_index];
low_index++;
count++;
Console.WriteLine("Low inserted.");
}
else
{
middle[count] = high[high_index];
high_index++;
count++;
Console.WriteLine("High inserted.");
}
}
if (low_index == low.Length)
{
for (; count < middle.Length; count++)
{
middle[count] = high[high_index];
high_index++;
}
// alternate: Array.Copy(high, high_index, middle, count, middle.Length - count);
}
else
{
for (; count < middle.Length; count++)
{
middle[count] = low[low_index];
low_index++;
}
// alternate: Array.Copy(low, low_index, middle, count, middle.Length - count);
}
return middle;
}
public static Tuple<int[], int[]> SplitArray(int[] k)
{
int half = k.Length / 2;
int[] a = new int[half];
int[] b = new int[k.Length - half];
int k_index = 0;
for (int i = 0; i < a.Length; i++)
{
a[i] = k[k_index];
k_index++;
}
for (int i = 0; i < b.Length; i++)
{
b[i] = k[k_index];
k_index++;
}
return new Tuple<int[], int[]>(a, b);
}
给你:代码充满了错误,数组的长度不匹配,你每次都在覆盖你的中间数组。
namespace MainClass
{
class Program
{
static void Main(string[] args)
{
int[] array = { 3, 5, 15, 22, 12, 15, 23 };
int[] x = MergeSort(array);
foreach (int i in x)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
public static int[] MergeSort(int[] array)
{
if (array.Length <= 1)
{
return array;
}
(int[], int[]) p = SplitArray(array);
int[] left = MergeSort(p.Item1);
int[] right = MergeSort(p.Item2);
return Merge(left, right);
}
public static int[] Merge(int[] low, int[] high)
{
int[] middle = new int[(low.Length + high.Length)];
int count = 0;
foreach (int item in low)
{
Console.WriteLine("low: " + item + ',');
}
foreach (int item in high)
{
Console.WriteLine("high: " + item + ',');
}
int low_index = 0;
int high_index = 0;
while (low_index < low.Length && high_index < high.Length)
{
if (low[low_index] < high[high_index])
{
middle.SetValue(low[low_index], count);
low_index++;
count++;
Console.WriteLine("Low inserted.");
}
else
{
middle.SetValue(high[high_index], count);
high_index++;
count++;
Console.WriteLine("High inserted.");
}
}
while (high_index < high.Length)
{
middle[low_index + high_index] = high[high_index++];
}
while (low_index < low.Length)
{
middle[low_index + high_index] = low[low_index++];
}
return middle;
}
public static (int[], int[]) SplitArray(int[] k)
{
int[] a = new int[((k.Length + 1) / 2)];
int[] b = new int[(k.Length / 2)];
for (int i = 0; i < a.Length; i++)
{
a[i] = k[i];
}
for (int i = 0; i < b.Length; i++)
{
b[i] = k[i + a.Length];
}
return (a, b);
}
}