//---Following JavaApplication compairs sortalgorithem----

import java.util.Random;

public class Sorting{
 static int VergAnzahl = 0;
 static int TauschAnzahl = 0;

 public static void main(String[] args){
  if (args.length == 0) { }
 else{ 
  System.out.println("Anzahl QuickSort SelectionSort BubbleSort");
  for (int n=1000; n<11000; n=n+1000){
   int[] TestFeld = new int[n];
   int[] Feld_Select = new int[n]; 
   int[] Feld_Bubble = new int[n]; 
   int[] Feld_Quick = new int[n];    

   if (args[0].equals("times")){
     init(TestFeld); //-Testfeld gets values
     Feld_Select = TestFeld; //-Feld_Select gets values
     long start = TimeStart();
     selection_sort(Feld_Select);
     if (sorted(Feld_Select) == false) System.out.println(" !! Fehler beim Sortieren !!"); 
     long time_select = TimeStop(start);
     Feld_Bubble = TestFeld;
     start = TimeStart();
     bubble_sort(Feld_Bubble);
     if (sorted(Feld_Bubble) == false) System.out.println(" !! Fehler beim Sortieren !!"); 
     long time_bubble = TimeStop(start);
     Feld_Quick = TestFeld;
     start = TimeStart();
     quick_sort(Feld_Quick,0,Feld_Quick.length-1);
     if (sorted(Feld_Quick) == false) System.out.println(" !! Fehler beim Sortieren !!");      
     long time_quick = TimeStop(start);
     System.out.println(n + "     " + time_quick + "         " + time_select + "             " + time_bubble);
   }
   else if (args[0].equals("swaps")){
     init(TestFeld);
     Feld_Select = TestFeld;
     TauschAnzahl=0; VergAnzahl=0;
     selection_sort(Feld_Select);
     if (sorted(Feld_Select) == false) System.out.println(" !! Fehler beim Sortieren !!"); 
     int swaps_select = TauschAnzahl;
     Feld_Bubble = TestFeld;
     TauschAnzahl=0; VergAnzahl=0;
     bubble_sort(Feld_Bubble);
     if (sorted(Feld_Bubble) == false) System.out.println(" !! Fehler beim Sortieren !!"); 
     int swaps_bubble = TauschAnzahl;
     Feld_Quick = TestFeld;
     TauschAnzahl=0; VergAnzahl=0;
     quick_sort(Feld_Quick,0,Feld_Quick.length-1);
     if (sorted(Feld_Quick) == false) System.out.println(" !! Fehler beim Sortieren !!"); 
     int swaps_quick = TauschAnzahl;
     System.out.println(n + "     " + swaps_quick + "         " + swaps_select + "             " + swaps_bubble);
   }
   else if (args[0].equals("comps")){
     init(TestFeld);
     Feld_Select = TestFeld;
     TauschAnzahl=0; VergAnzahl=0;
     selection_sort(Feld_Select);
     if (sorted(Feld_Select) == false) System.out.println(" !! Fehler beim Sortieren !!");    
     int comps_select = VergAnzahl;
     Feld_Bubble = TestFeld;
     TauschAnzahl=0; VergAnzahl=0;
     bubble_sort(Feld_Bubble);
     if (sorted(Feld_Bubble) == false) System.out.println(" !! Fehler beim Sortieren !!"); 
     int comps_bubble = VergAnzahl;
     Feld_Quick = TestFeld;
     TauschAnzahl=0; VergAnzahl=0;
     quick_sort(Feld_Quick,0,Feld_Quick.length-1);
     if (sorted(Feld_Quick) == false) System.out.println(" !! Fehler beim Sortieren !!"); 
     int comps_quick = VergAnzahl;
     System.out.println(n + "      " + comps_quick + "         " + comps_select + "             " + comps_bubble);
   }
  } 
  }
 }

//------------------------------

 public static void init(int[] f){
  int n = f.length;
  java.util.Random r = new java.util.Random();
  r.setSeed(17);
  for (int i = 0; i < n; i++) f[i] = r.nextInt();
 }

 public static void bubble_sort(int[] f){
  int a, b, help = 0;
  for (a = 0; a < f.length-1; a++)
   for (b = a+1; b < f.length; b++)
     if (less(f[a],f[b]) == false) swap(f,a,b);
 }

 public static void selection_sort(int[] f){
  int help, i, posmini;
  i = 0; help = 0;
  while(i < f.length){
    posmini = getMini(f,i);
    swap(f,i,posmini);
    i++;
  }
 }

 public static void quick_sort(int[] a, int links, int rechts){ 
  int i = links;
  int j = rechts;
  int x = a[(links+rechts)/2];
  
  do{
     while(a[i] < x) i++;
     while(a[j] > x) j--;
     if(i <= j){
       swap(a,i,j);
       i++;
       j--;
     }
  }while(i <= j);

  if(less(links,j) == true) quick_sort(a, links, j);
  if(less(i,rechts) == true) quick_sort(a, i, rechts);
 }   

//---does sort-method work ?------
 private static boolean sorted(int[] f){
  boolean wert = true;
  for (int a = 0; a < f.length-1; a++){
    if (f[a] > f[a+1]) wert = false;
      else wert = true;
  }
  return wert;
 }

//---compair'n'count-----
 private static boolean less(int i, int j){
  boolean wert = true;
  if (i < j){
    wert = true;
  }
  else{
    wert = false;
  }
  VergAnzahl++;
  return wert;
 }

//---change'n'count---- 
 private static void swap(int[] f, int i, int j){
  int help = 0;
  help = f[i];
  f[i] = f[j];
  f[j] = help;
  TauschAnzahl++;
 }

//---2 methodes to get the time----
 private static long TimeStart(){
  return System.currentTimeMillis();
 }
 
 private static long TimeStop(long w){
  long time = (System.currentTimeMillis() - w);
  return time;
 }

//---method is nessecary for selection_sort----
 public static int getMini(int[] Feld, int start){
  int pos = start;
  for (int a = start; a < Feld.length; a++){
    if (less(Feld[pos],Feld[a]) == false){
      pos = a;
    }
  }
  return pos;
 }

}//----End of Application----       