Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 . . .



C#
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());
			}
		}
		
	}
}
Posted
Updated 18-Jan-16 2:01am
v5
Comments
Leo Chapiro 18-Jan-16 7:59am    
Try to debug it!
sdsoft.comze.com 18-Jan-16 8:01am    
i did but bug remain same
ZurdoDev 18-Jan-16 8:05am    
No, debug means to step through the code, watch each line and see what happens. Then you'll also see the line of code that causes the error and you'll see why. Then it's very easy to fix on your own.
sdsoft.comze.com 18-Jan-16 8:06am    
i think q++;
i am using monodevleop breakpoints does not work.
ZurdoDev 18-Jan-16 8:16am    
You have it tagged as Visual Studio.

The specific errors happens because there is a bug in the following line
Quote:
for (int i = 0; i <= array.Length; i++)

It should be
C#
for (int i = 0; i < array.Length; i++)


In any case, your implementation of the merge sort algorithm doesn't look correct.
 
Share this answer
 
Comments
sdsoft.comze.com 18-Jan-16 8:11am    
Error remain same.sir have you best implementation for me.
The error message tells you why
Quote:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at sort.sort.MergeSort(Int32[] array)

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

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900