MergeSort在c#数组错误中 [英] MergeSort in c# array error

查看:70
本文介绍了MergeSort在c#数组错误中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码给出了以下错误:



Following code gives this error:

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at sort.sort.MergeSort(Int32[] array) in c:\Users\Muhammad Rizwan\Documents\Projects\sort\sort\Main.cs:line 44
   at sort.sort.Main() in c:\Users\Muhammad Rizwan\Documents\Projects\sort\sort\Main.cs:line 66
Press any key to continue . . .










using System;
using System.Collections.Generic;
using System.Linq;

namespace sort
{
	class sort{
		public static int[] MergeSort(int[] array)
		{	
			if (array.Length == 2) {
				int t = array[0];
				if(array[0] > array[1]){
					array[0] = array[1];
					array[1] = t;
				}
			}
			else if(array.Length > 2){

				int m = array.Length/2;	

				int[] A, B;
				A = new int[m];
				B = new int[(array.Length-m)];

				for(int i =0; i <= m-1;i++){
					A[i] = array[i];
				}
				for(int i = m ; i <= array.Length-1;i++){
					B[i-m] = array[i];
				}				
				
				int r =0;
				int q =0;
				

				for(int k = 0; k <= array.Length; k++){
					if(A[r] <= B[q]){  //line 44
						array[k] = A[r];
						r++;					
					}
					else{
						array[k] = B[q];
						q++;	
					}
				}

			}			
			return array;
		}	

		static void Main(){
			int[] array = {5, 2, 6, 4, 7, 10, 1, 9, 3, 8};

			array = MergeSort (array);
		        for (int i = 0; i <= array.Length; i++) {
				Console.WriteLine(array[i].ToString());
			}
		}
		
	}
}

推荐答案

具体错误发生,因为以下行中存在错误

The specific errors happens because there is a bug in the following line
Quote:

for(int i = 0; i< ; = array.Length; i ++)

for (int i = 0; i <= array.Length; i++)



它应该是


It should be

for (int i = 0; i < array.Length; i++)





在任何情况下,您的合并排序算法的实现看起来都不正确。



In any case, your implementation of the merge sort algorithm doesn't look correct.


错误消息告诉你为什么

The error message tells you why
引用:

未处理的异常:System.IndexOutOfRangeException:索引超出了数组的范围。 at sort.sort.MergeSort(Int32 [] array)

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at sort.sort.MergeSort(Int32[] array)



where


and where

引用:

在c:\ Users \ Muhammad Rizwan \Documents\Projects \sort \sort \ Main.cs:第44行

in c:\Users\Muhammad Rizwan\Documents\Projects\sort\sort\Main.cs:line 44



此代码 if(A [r]< = B [q])在到达其中一个数组<$ c后立即失败$ c> A 或 B

这只是代码中的一个问题,解决方案1显示另一个问题一个。



我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]


This code if(A[r] <= B[q]) fails as soon as you reach the end of one of the arrays A or B.
This is just one of the problems in your code, Solution 1 show another one.

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于MergeSort在c#数组错误中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆