Algorithm comparison in java: test program does not work -


i'm trying compare execution of java implementation of quicksort , hybrid version (using insertionsort partitions smaller integer k). wrote test class analyze behaviour of algorithms values ok k (1 <= k <= 25). each value of k class compares different sizes of input array 2 algorithms.

can't run program values of size of array, instance values greater 4000. execution reach different values , freeze, after while finish have no output of computation. (i'm using eclipse).
problem? wish perform comparation of 2 algoritms array size 10 10000 (at least).
code listed below:

public class main {  private static final int max_k = 25; private static final int max_size = 4500; private static final int add_size = 100; private static int size = 10;  private static quicksort qsort; private static hybridsort hsort;  private static void initarray(int[] a) {     random rand = new random();     (int = 0; < a.length; i++) {         // a[i] = (int)(math.random()*100000);         a[i] = rand.nextint();      } }  private static int[] = new int[10]; private static int[] b = new int[10];  public static void main(string[] args) {      try {          filewriter fstream = new filewriter("out.txt");         bufferedwriter out = new bufferedwriter(fstream);         out.write("init file");          qsort = new quicksort();         hsort = new hybridsort();          /************************************************/         /* comparison */         /************************************************/          (int = 1; <= max_k; i++) {             hsort.setk(i);              int p = 0;             (int j = size; j <= max_size; j = j + add_size) {                  = new int[j];                 b = new int[j];                 initarray(a);                 initarray(b);                  long stime = system.nanotime();                 qsort.quicksort(a, 0, a.length - 1);                 long qduration = system.nanotime() - stime;                  stime = system.nanotime();                 hsort.hybridsort(b, 0, b.length - 1);                 long hduration = system.nanotime() - stime;                  out.append(/* "\na: " +printarray(a)+ */"k: " + + " a["                         + j + "]\tq = " + qduration + " h = " + hduration                         + "\n");                  string h = long.tostring(hduration);                 string q = long.tostring(qduration);                  if (h.length() < q.length()) {                     p++;                     out.append("\t#outperm k: "                             +                             + "\t\t"                             + hduration                             + "\t\t < \t\t "                             + qduration                             + "\t\t\t\t| a[]\t\t"                             + a.length                             + ((q.length() - h.length()) == 2 ? "\t magn. 2"                                     : "") + "\n");                 }             }             if (p > 0)                 out.append("#p= " + p + " k= " + + "\n\n");         }         out.append("close file");         out.close();     } catch (ioexception e) {      } }  } 

the algorithm classes:

public class quicksort {   public void quicksort(int[] a, int left, int right){     if (left < right) {         int m = partition(a, left, right);         quicksort(a, left, m-1);         quicksort(a, m, right);     } }   private int partition(int[] a, int left, int right){     int pivot = a[right];     int = left;     int j = right;      while (true) {         while ( (a[j] > pivot)) {             j--;         }         while ((a[i] < pivot)) {             i++;         }         if (i < j){             int swap = a[j];             a[j] = a[i];             a[i] = swap;         }else{             return i;         }     } }  } 


public class hybridsort {  int k; int m; insertionsort isort;  public hybridsort() {     k = 3;     isort = new insertionsort(); }  public void hybridsort(int[] a, int left, int right) {     if (left < right) {         if ((right - left) < k) {                                     isort.sort(a,left,right);         } else {                             m = partition(a, left, right);             hybridsort(a, left, m - 1);             hybridsort(a, m, right);         }     } }  private int partition(int[] a, int left, int right) {     int pivot = a[right];     int = left;     int j = right;      while (true) {         while ((a[j] > pivot) && (j >= 0)) {             j--;         }         while ((a[i] < pivot) && (i < a.length)) {             i++;         }         if (i < j) {             int swap = a[j];             a[j] = a[i];             a[i] = swap;         } else {             return i;         }     } }   public void setk(int k) {     this.k = k; } } 

your implementation of partition not correct. consider small test below (i made partition static convenience).

both while loops won't executed, because a[i] == a[j] == pivot. moreover, i<j, 2 elements swapped, resulting in same array. therefore, outer while loop becomes infinite.

the same problem occurs array first , last element same.

public class test {     public static void main(string[] args) {         int[] = {1, 1};         partition(a, 0, 1);     }      private static int partition(int[] a, int left, int right){         int pivot = a[right];         int = left;         int j = right;          while (true) {             while ( (a[j] > pivot)) {                 j--;             }             while ((a[i] < pivot)) {                 i++;             }             if (i < j){                 int swap = a[j];                 a[j] = a[i];                 a[i] = swap;             }else{                 return i;             }         }     } } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -