Q: Form gives Stackoverflow Exception

Hi!

I'm a student programmer, and as a assignment we have to make a quicksort with a recursive function.

I have done my best, but now I constantly get stackoverflow Exceptions, because I cannot get the end statement of the functions right.

This is my class code:

class qSort

{ public static int[] ExtraArray = new int[10]; public static int[] TestArr = new int[1]; public static bool isGo = true; /// <summary> /// A Quicksort: Sorts the items in a given array /// </summary> /// <param name="Arr"> The given array</param> /// <returns>Should return a sorted array</returns> public static int[] SortDevice(int[] Arr) { int LowDone = 0; //Check if the incoming array is either a single digit or 2 digits with the same value if (Arr.Length == 1) { Arr.CopyTo(ExtraArray, 0); LowDone = Arr.Length; isGo = false; } int getal = 0; int n = Arr.Length; int avg; // Get the average value of the array for (int i = 0; i < n; i++) { getal = Arr[i] + getal; } // The average value if (n > 0) { avg = getal / n; } else { avg = 1; } // Creating the arrays and their counters int t1 = 0; int t2 = 0; int NoemL = 0; int NoemH = 0; for (int i = 0; i < n; i++) { if (Arr[i] <= avg) { NoemL++; } if (Arr[i] > avg) { NoemH++; } } int[] LowArray = new int[NoemL]; int[] HighArray = new int[NoemH]; // Seperate the items (higher than or equal to average/lower than average) and add them to arrays for (int i = 0; i < n; i++) { if(Arr[i] <= avg) { LowArray[t1] = Arr[i]; t1++; } else { HighArray[t2] = Arr[i]; t2++; } } int telLow = 0; int telHigh = 0; // Measure if all the items have been sorted, if not, go recursive // If yes, put all data into an array for (int i = 0; i < LowArray.Length-1 ; i++) { if (LowArray[i] == LowArray[i+1]) { telLow++; } if (telLow == LowArray.Length) { LowArray.CopyTo(ExtraArray, 0); } } for (int i = 0; i < HighArray.Length-1; i++) { if (HighArray[i] == HighArray[i+1]) { telHigh++; } if (telHigh == HighArray.Length) { HighArray.CopyTo(ExtraArray, telLow); } } int[] SortHoog = qSort.SortDevice(HighArray); while (isGo = true) { int[] SortLaag = qSort.SortDevice(LowArray); for (int i = LowDone; i < SortLaag.Length; i++) { ExtraArray[i] = SortLaag[i]; } for (int i = 0; i < SortHoog.Length; i++) { ExtraArray[i + SortLaag.Length + LowDone] = SortHoog[i]; } } return ExtraArray; } } 

The whole idea of the program is to sort a random given array, and then put it in a Windows form list ( That part has been handled, all the random numbers are given in the button function) I'm NOT asking you guys for an answer, just a little nudge in the right direction since I have to learn this. I have been stuck on this for the last 2 hours.

Many thanks!

by misterblp via /r/csharp

Leave a Reply