java - Why does order of testing change testing results? -


i have coded 2 sorting methods (bubble sort , merge sort) know have different efficiencies wanted graph these out different array sizes. check times expected, did test run generated 10000 random values , put them in array, did bubble sort on it, generated them again , did merge sort. timed both. when looked @ time took bubble sort, far, far less on merge sort. switched 1 did first , merge sort fastest. why happen , how can stop it? here's of code.

import java.util.arraylist; public class sorter { public static <t extends comparable> boolean isinorder(arraylist<t> ar) {     (int = 0; < ar.size() - 1; i++) {         if (ar.get(i).compareto(ar.get(i + 1)) > 0) {             return false;         }     }     return true; }  public static <t extends comparable> boolean isinorder(t[] ar) {     (int = 0; < ar.length - 1; i++) {         if (ar[i].compareto(ar[i + 1]) > 0) {             return false;         }     }     return true; }  private static <t> arraylist<t> splitarraylist(arraylist<t> ar, int start, int end) {     arraylist<t> toreturn = new arraylist<>();     (int = start; < end; i++) {         toreturn.add(ar.get(i));     }     return toreturn; }  private static <t extends comparable> arraylist<t> merge(arraylist<t> a, arraylist<t> b) {     arraylist<t> toreturn = new arraylist<>();     int bindex = 0;     (t value : a) {         while (bindex < b.size() && (b.get(bindex).compareto(value) < 0)) {             toreturn.add(b.get(bindex));             bindex++;         }         toreturn.add(value);     }     if (bindex <= b.size()) {         (int = bindex; < b.size(); i++) {             toreturn.add(b.get(i));         }     }     return toreturn; }  public static <t extends comparable> arraylist<t> mergesort(arraylist<t> ar) {     if (ar.size() == 1) return ar;     else {         int splitpoint = ar.size() / 2;         arraylist<t> split1 = mergesort(splitarraylist(ar, 0, ar.size() / 2));         arraylist<t> split2 = mergesort(splitarraylist(ar, ar.size() / 2, ar.size()));         ar = merge(split1, split2);     }     return ar; }   public static <t extends comparable> arraylist<t> bubblesort(arraylist<t> ar) {     boolean issorted = false;     while (!issorted) {         issorted = true;         (int = 0; < ar.size() - 1; i++) {             if (ar.get(i).compareto(ar.get(i + 1)) > 1) {                 t holdvalue = ar.get(i);                 ar.set(i, ar.get(i + 1));                 ar.set(i + 1, holdvalue);                 issorted = false;             }         }     }     return ar; }  public static void main(string[] args) {     arraylist<double> test = generaterandomdata(100000);     double t1 = system.currenttimemillis();     mergesort(test);     double t2 = system.currenttimemillis();     test = generaterandomdata(100000);     double t3 = system.currenttimemillis();     bubblesort(test);     double t4 = system.currenttimemillis();     system.out.println("merge sort " + (t2-t1) + " bubble sort "  + (t4-t3)); } public static arraylist<double> generaterandomdata(int size){     arraylist<double> toreturn = new arraylist<>();     (int = 0; < size; i++) {         toreturn.add(math.random());     }     return toreturn; } } 

the reason looks odd because bubblesort not sorting arrays. code, given array [6.0, 5.0, 4.0, 3.0, 2.0, 1.0], after running method wind with: [6.0, 5.0, 4.0, 3.0, 2.0, 1.0]. hmmm. looks lot started with.

the error simple. in line

if (ar.get(i).compareto(ar.get(i + 1)) > 1) { 

the compareto method returns 1 of -1, 0, 1, depending on whether or not argument less than, equal to, or greater than, respectively. change to

if (ar.get(i).compareto(ar.get(i + 1)) > 0) { 

and work correctly (and go very, slowly).

as mergesort works correctly, should see different picture between bubblesort , mergesort.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -