Question: Why this code gives the error message Index was outside the bounds of array. (Help to fix it) using System; using System.Collections.Generic; using System.Linq; using
Why this code gives the error message "Index was outside the bounds of array". (Help to fix it)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication32
{ class CArray
{
private int[] arr;
private int upper;
private int numElements;
public CArray(int size)
{
arr = new int[size];
upper = size - 1;
numElements = 0;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{
for (int i = 0; i <= upper; i++)
Console.Write(arr[i] + " ");
}
public void Clear()
{
for (int i = 0; i <= upper; i++)
{
arr[i] = 0;
}
numElements = 0;
} public void SelectionSort()
{
int min, temp;
for (int outer = 0; outer <= upper; outer++)
{
min = outer;
for (int inner = outer + 1; inner <= upper; inner++)
if (arr[inner] < arr[min])
min = inner;
temp = arr[outer];
arr[outer] = arr[min];
arr[min] = temp;
}
} public void MergeSort()
{
int[] tempArray = new int[numElements];
RecMergeSort(tempArray, 0, numElements - 1);
}
public void RecMergeSort(int[] tempArray, int lbound, int ubound)
{
if (lbound == ubound)
{
return;
}
else
{
int mid = (int)(lbound + ubound) / 2;
RecMergeSort(tempArray, lbound, mid);
RecMergeSort(tempArray, mid + 1, ubound);
Merge(tempArray, lbound, mid + 1, ubound);
}
}
public void Merge(int[] tempArray, int lowp, int highp, int ubound)
{
int j = 0;
int lbound = lowp;
int mid = highp - 1;
int n = (ubound - lbound) + 1;
while ((lowp <= mid) && (highp <= ubound))
{
if (arr[lowp] < arr[highp])
{
tempArray[j] = arr[lowp];
j++;
lowp++;
}
else
{
tempArray[j] = arr[highp];
j++;
highp++;
}
}
while (lowp <= mid)
{
tempArray[j] = arr[lowp];
j++;
lowp++;
}
while (highp <= ubound)
{
tempArray[j] = arr[highp];
j++;
highp++;
}
for (j = 0; j <= n - 1; j++)
{
arr[lbound + j] = tempArray[j];
}
}
public void QSort()
{
RecQSort(0, numElements - 1);
}
public void RecQSort(int first, int last)
{
if ((last - first) <= 0)
{
return;
}
else
{
int pivot = arr[last];
int part = this.Partition(first, last);
RecQSort(first, part - 1);
RecQSort(part + 1, last);
}
}
public int Partition(int first, int last)
{
int pivotVal = arr[first];
int theFirst = first;
bool okSide;
first++;
do
{
okSide = true;
while (okSide)
{
if (arr[first] > pivotVal)
okSide = false;
else
{
first++;
okSide = (first <= last);
}
}
okSide = (first <= last);
while (okSide)
{
if (arr[last] <= pivotVal)
okSide = false;
else
{
last--;
okSide = (first <= last);
}
}
if (first < last)
{
Swap(first, last);
first++;
last--;
}
} while (first <= last);
Swap(theFirst, last);
return last;
}
public void Swap(int item1, int item2)
{
int temp = arr[item1];
arr[item1] = arr[item2];
arr[item2] = temp;
}
static void Main(string[] args)
{
CArray nums = new CArray(3000);
Random rnd = new Random(30000);
Console.WriteLine(" before selection sort");
for (int i = 0; i < 3000; i++)
{
nums.Insert((int)(rnd.NextDouble() * 30000));
}
nums.DisplayElements();
Console.WriteLine(" selection sort");
nums.SelectionSort();
nums.DisplayElements();
Console.WriteLine(" before merge sort");
for (int i = 0; i < 3000; i++)
{
nums.Insert((int)(rnd.NextDouble() * 30000));
}
nums.DisplayElements();
Console.WriteLine(" merge sort");
nums.MergeSort();
nums.DisplayElements();
nums.Clear();
Console.WriteLine(" before quick sort");
for (int i = 0; i < 3000; i++)
{
nums.Insert((int)(rnd.NextDouble() * 30000));
}
nums.DisplayElements();
Console.WriteLine(" quick sort");
nums.QSort();
nums.DisplayElements(); Console.ReadLine(); }
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
