日期:2014-05-18  浏览次数:20861 次

归并排序,错在哪里??
大家帮我看看这个归并排序,错在哪里?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MergeSort1
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 9, 5, 8, 4, 7, 3, 6, 2, 1 };
MergeSortFunction(arr, 0, 9);
foreach(int n in arr)
Console.Write(n + " ");
Console.ReadLine();
}
public static void MergeSortFunction(int[] array, int first,int last)
{
if(first < last)
{
int mid = (first + last) / 2;
MergeSortFunction(array,first,mid);
MergeSortFunction(array,mid+1,last);
MergeSort(array, first, mid, last);
}
}
public static void MergeSort(int[] arr, int first, int mid, int last)
{
int valueA = first;
int valueB = mid+1;
int[] temp=new int[last+1];
int i=0;
while(valueA<=mid&&valueB<=last-1)
{
if(arr[valueA]>=arr[valueB])
temp[i++]=arr[valueB++];
else
temp[i++]=arr[valueA++];
}
while(valueA<=mid&&valueB==last)
temp[i++]=arr[valueA++];
while(valueB<=last&&valueA==mid)
temp[i++]=arr[valueB++];
for(int j = 0; j <= last-1; j++)
arr[j] = temp[j];

}
}
}


------解决方案--------------------
http://www.cnblogs.com/mingmingruyuedlut/archive/2011/08/18/2144984.html
对照着你自己找问题吧