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

C#
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];
      //We fill the array with random numbers.
      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 + " ");
      }
      //sort
      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();
   }
}
Posted
Updated 3-Jun-20 23:07pm
v2

1 solution

 
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