我在数组中运算时,经常提示:
Make sure that the maximum index on a list is less than the size
我的数组也没有越界,不知道为什么???

解决方案 »

  1.   

    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 };
    //for(int i = 0; i < arr.Length;i++ )
    MergeSortFunction(arr, 0, arr.Length);

    }
    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)
    {
    if(arr[valueA]>=arr[valueB])
    {
    temp[i++]=arr[valueB++];
    }
    else
    {
    temp[i++]=arr[valueA++];
    }
    }
    while(valueA<=mid)
    {
    temp[i++]=arr[valueA++];
    }
    while(valueB<=last)
    {
    temp[i++]=arr[valueB++];
    }
    for(int j = first; j <= last; j++)

    arr[j] = temp[j];
    }
    foreach(int n in arr)
    Console.Write(n + "  ");
    Console.ReadLine();
    }
    }
    }
    我的这个归并排序,错在哪里??