This is how the task sounds: Develop and program a non-recursive version of the quick sort algorithm. [Indication. Use the store to store ordered pairs (i, j). The pair stored in the store means that the elements Xi, ..., Xj must be ordered. For example, initially the store was supposed to contain a pair (1, 8). After the value X5 = 5 is selected as the average, the pair (1, 8) should be removed from the store, and the pairs (1, 4) and (6, 8) should be placed in the store. The algorithm ends when the store is empty.]
How to make non-recursive from this algorithm?
What I have tried:
class QuickSorting {
public static void sorting(double[] arr, long first, long last) {
double p = arr[(last - first)/2 + first];
double temp;
long i = first, j = last;
while(i <= j) {
while(arr[i] < p && i <= last) ++i;
while(arr[j] > p && j >= first) --j;
if(i <= j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
++i; --j;
}
}
if(j > first) sorting(arr, first, j);
if(i < last) sorting(arr, i, last);
}
}
class Test {
static void Main(string[] args) {
double[] arr = new double[100];
Random rd = new Random();
for(int i = 0; i < arr.Length; ++i) {
arr[i] = rd.Next(1, 101);
}
System.Console.WriteLine("The array before sorting:");
foreach(double x in arr) {
System.Console.Write(x + " ");
}
QuickSorting.sorting(arr, 0, arr.Length - 1);
System.Console.WriteLine("nnThe array after sorting:");
foreach(double x in arr) {
System.Console.Write(x + " ");
}
System.Console.WriteLine("nnPress the <Enter> key");
System.Console.ReadLine();
}
}