java - Merge Sort count number of swaps and compares -


i have existing code need add swap , compare counter for. far believe have counts correctly cannot output no display loop of each swap.

public void mergesort(int[] a, int howmany) {       if (a.length >= 2) {         // split array 2 halves         int[] left  = arrays.copyofrange(a, 0, a.length/2);         int[] right = arrays.copyofrange(a, a.length/2, a.length);           // sort 2 halves         mergesort(left,howmany);         mergesort(right, howmany);          // merge sorted halves sorted whole         merge(a, left, right);      } }     // merges left/right elements sorted result. // precondition: left/right sorted public static void merge(int[] result, int[] left,                                         int[] right) {     int i1 = 0;   // index left array     int i2 = 0;   // index right array    int compcount = 0;    int swapcount = 0;       (int = 0; < result.length; i++) {         compcount++;         if (i2 >= right.length ||            (i1 < left.length && left[i1] <= right[i2])) {             result[i] = left[i1];    // take left             i1++;            swapcount++;         } else {             result[i] = right[i2];   // take right             i2++;            swapcount++;         }     }    //figure loop issue out system.out.println("merge sort " + compcount + " " + swapcount); } 

make global variable or field within class holds merge , merge sort methods. allow methods increment variable. if declare inside method stay local variable , each recursive call produce different local variables of same name belonging different recursive method calls. therefore code should like:

public class classwithmergemethodinside {     int swapcount;     int compcount;      public void mergesort(int[] a, int howmany) {         //since merge sort begins here may initiliaze variables here         swapcount = 0;         compcount = 0;         if (a.length >= 2) {             // split array 2 halves             int[] left  = arrays.copyofrange(a, 0, a.length/2);             int[] right = arrays.copyofrange(a, a.length/2, a.length);               // sort 2 halves             mergesort(left,howmany);             mergesort(right, howmany);              // merge sorted halves sorted whole             merge(a, left, right);          }     }         // merges left/right elements sorted result.     // precondition: left/right sorted     public static void merge(int[] result, int[] left,                                             int[] right) {         int i1 = 0;   // index left array         int i2 = 0;   // index right array         //will not declare counter variables here         (int = 0; < result.length; i++) {             compcount++;             if (i2 >= right.length ||                (i1 < left.length && left[i1] <= right[i2])) {                 result[i] = left[i1];    // take left                 i1++;                swapcount++;             } else {                 result[i] = right[i2];   // take right                 i2++;                swapcount++;             }         }        //figure loop issue out system.out.println("merge sort " + compcount + " " + swapcount);     } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -