0

HI ALL,
well i am writing a program for quick sort but with some changes. The following code sorts the integers but i need it to show the sorting process in each steps.i.e. for example:

Initial List: 5 -> 9 -> 2 -> 9 -> 7 -> null
Level 0: Before Left: 2 -> null
Level 0: Before Pivot is: 5
Level 0: Before Right: 9 -> 9 -> 7 -> null
Level 1: Before Left: 7 -> null
Level 1: Before Pivot is: 9
Level 1: Before Right: 9 -> null Level 1: After Left: 7 -> null Level 1: After Pivot is: 9
Level 1: After Right: 9 -> null
Level 0: After Left: 2 -> null
Level 0: After Pivot is: 5
Level 0: After Right: 7 -> 9 -> 9 -> null
Sorted List: 2 -> 5 -> 7 -> 9 -> 9 -> null


and then,a function / method that generates an array of random data. As a parameter this method takes the size of the array to create.
Generate random values between zero and twice the length of the array. For example, an array of size ten would be filled with values between 0 and 20. Use this method to create an array of size five and sort the array.

public class QuickSort{
  public static void main(String a[]){
  int i;
  int array[] = {12,9,4,99,120,1,3,10,13};

  System.out.println(" Quick Sort\n\n");
  System.out.println("Values Before the sort:\n");
  for(i = 0; i < array.length; i++)
  System.out.print( array[i]+"  ");
  System.out.println();
  quick_srt(array,0,array.length-1);
  System.out.print("Values after the sort:\n");
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();
  System.out.println("PAUSE");
  }

  public static void quick_srt(int array[],int low, int n){
  int lo = low;
  int hi = n;
  if (lo >= n) {
  return;
  }
  int mid = array[(lo + hi) / 2];
  while (lo < hi) {
  while (lo<hi && array[lo] < mid) {
  lo++;
  }
  while (lo<hi && array[hi] > mid) {
  hi--;
  }
  if (lo < hi) {
  int T = array[lo];
  array[lo] = array[hi];
  array[hi] = T;
  }
  }
  if (hi < lo) {
  int T = hi;
  hi = lo;
  lo = T;
  }
  quick_srt(array, low, lo);
  quick_srt(array, lo == low ? lo+1 : lo, n);
  }
}

Edited by Ezzaral: Added code tags. Please use them to format any code that you post. Please learn to indent as well.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by hfx642
1

Well... You obviously know how to use

System.out.println();

Add them where you want a message to be displayed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.