java - Adding integer to ArrayList adds -1 not value -
when add integer arraylist adding value of -1
, not value such 349823
. calling counter object named "c". syntax nameoflist.add(c.instructioncounter);
does 1 know wrong? requested whole class, lot. trying add counter values arraylist , pass array method count standard deviation. bare me names.
import java.util.arraylist; import java.util.random; import javax.swing.plaf.synth.synthseparatorui; public class comparator extends instrumentedsorter { static int count = 1000; static float selectionic = 0;static float selectione = 0;static float insertionic = 0; static float insertione = 0;static float bubbleic = 0;static float bubblee = 0;static float quickic = 0; static float quicke = 0;static float rquickic = 0;static float rquicke = 0;static float mergeic = 0;static float mergee = 0; static arraylist<integer> saic = new arraylist<integer>(); static arraylist<float> sae = new arraylist<float>(); static arraylist<integer> maic = new arraylist<integer>(); static arraylist<float> mae = new arraylist<float>(); static arraylist<integer> baic = new arraylist<integer>(); static arraylist<float> bae = new arraylist<float>(); static arraylist<integer> iaic = new arraylist<integer>(); static arraylist<float> iae = new arraylist<float>(); static arraylist<integer> qaic = new arraylist<integer>(); static arraylist<float> qae = new arraylist<float>(); static arraylist<integer> raqic = new arraylist<integer>(); static arraylist<float> raqe = new arraylist<float>(); public static void main(string[] args) { comparator c = new comparator(); (int size = 1000; size <= 10000; size += 1000) { (int round = 1; round <= 10; round++) { int[] randomarray = c.createrandomarray(size); (int iter = 1; iter <= 20; iter++) { // clone randomarray selection sort int[] = randomarray.clone(); c.resetinstructioncounter(); c.timer.start(); c.selectionsort(a); c.timer.end(); saic.add(c.getinstructioncounter()); selectionic += c.instructioncounter; sae.add((float) c.timer.gettotaltime()); selectione += c.timer.gettotaltime(); // clone randomarray bubble sort int[] b = randomarray.clone(); c.resetinstructioncounter(); c.timer.start(); c.bubblesort(b); c.timer.end(); bubbleic += c.instructioncounter; baic.add(c.instructioncounter); bae.add((float) c.timer.gettotaltime()); bubblee += c.timer.gettotaltime(); // clone randomarray insertion sort int[] d = randomarray.clone(); c.resetinstructioncounter(); c.timer.start(); c.insertionsort(d); c.timer.end(); iaic.add(c.instructioncounter); insertionic += c.instructioncounter; iae.add((float) c.timer.gettotaltime()); insertione += c.timer.gettotaltime(); // clone randomarray quick sort int[] e = randomarray.clone(); c.resetinstructioncounter(); c.timer.start(); c.quicksort(e); c.timer.end(); qaic.add(c.instructioncounter); quickic += c.instructioncounter; qae.add((float) c.timer.gettotaltime()); quicke += c.timer.gettotaltime(); // clone randomarray randomized quick sort int[] f = randomarray.clone(); c.resetinstructioncounter(); c.timer.start(); c.randomizedquicksort(f); c.timer.end(); raqic.add(c.instructioncounter); rquickic += c.instructioncounter; raqe.add((float) c.timer.gettotaltime()); rquicke += c.timer.gettotaltime(); // clone randomarray merge sort int[] g = randomarray.clone(); c.resetinstructioncounter(); c.timer.start(); c.mergesort(g); c.timer.end(); maic.add(c.instructioncounter); mergeic += c.instructioncounter; mae.add((float) c.timer.gettotaltime()); mergee += c.timer.gettotaltime(); } } average(selectionic);average(bubbleic);average(rquickic);average(insertionic);average(quickic);average(mergeic); average(selectione);average(bubblee);average(rquicke);average(insertione);average(quicke);average(mergee); print(); mae.clear();maic.clear();raqe.clear();raqic.clear();bae.clear();baic.clear();iae.clear();iaic.clear();qae.clear(); qaic.clear();sae.clear();saic.clear(); } } public static void print(){ system.out.printf("┌────────────────────┬─────────────────────────────────────────────────────────────────────────────┐\n"); system.out.printf("│ │ array size = " +count+" │\n"); system.out.printf("├────────────────────┼──────────────────────────────────────┬──────────────────────────────────────┤\n"); system.out.printf("│ sorting algorithm │ instruction counter │ run time │\n"); system.out.printf("├────────────────────┼─────────────────┬────────────────────┼─────────────────┬────────────────────┤\n"); system.out.printf("│ selection sort │%17.2f|%20.2f│%17.2f│%20.2f│\n", selectionic, sdi(saic), selectione, sdl(sae)); system.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n"); system.out.printf("│ bubble sort │%17.2f│%20.2f│%17.2f│%20.2f│\n", bubbleic, sdi(baic), bubblee, sdl(bae)); system.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n"); system.out.printf("│ merge sort │%17.2f│%20.2f│%17.2f│%20.2f│\n", mergeic, sdi(maic), mergee, sdl(mae)); system.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n"); system.out.printf("│ insertion sort │%17.2f│%20.2f│%17.2f│%20.2f│\n", insertionic, sdi(iaic), insertione, sdl(iae)); system.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n"); system.out.printf("│ quick sort │%17.2f│%20.2f│%17.2f│%20.2f│\n", quickic, sdi(qaic), quicke, sdl(qae)); system.out.printf("├────────────────────┼─────────────────┼────────────────────┼─────────────────┼────────────────────┤\n"); system.out.printf("│ random quick sort │%17.2f│%20.2f│%17.2f│%20.2f│\n", rquickic, sdi(raqic), rquicke, sdl(raqe)); system.out.printf("└────────────────────┴─────────────────┴────────────────────┴─────────────────┴────────────────────┘\n\n\n"); count += 1000; } public static float average (float variable){ variable = variable/2000; return variable; } public static float sdl (arraylist<float> name){ float total = 0; for(int = 0; < name.size(); i++) { total += name.indexof(i); } total = total / name.size(); float deviation = 0; for(int = 0; < name.size(); i++){ deviation += (math.pow((name.indexof(i)-total),2)); } deviation /= name.size(); deviation = (float) math.pow(deviation, 2); return deviation; } public static float sdi(arraylist<integer> name){ int total = 0; for(int = 0; < name.size(); i++) { system.out.println(name.indexof(i)); } for(int = 0; < name.size(); i++) { total += name.indexof(i); } total = total / name.size(); int deviation = 0; for(int = 0; < name.size(); i++){ deviation += (math.pow((name.indexof(i)-total),2)); } deviation /= name.size(); deviation = deviation*deviation; return (float) deviation; } public void bubblesort(int[] array) { int temp; this.incrementinstructioncounter(); this.incrementinstructioncounter(); (int = 0; < array.length; i++) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); this.incrementinstructioncounter(); (int j = + 1; j < array.length; j++) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); if (array[i] > array[j]) { this.incrementinstructioncounter(); temp = array[i]; this.incrementinstructioncounter(); array[i] = array[j]; this.incrementinstructioncounter(); array[j] = temp; this.incrementinstructioncounter(); } } } } public void selectionsort(int[] array) { int first, temp; this.incrementinstructioncounter(); this.incrementinstructioncounter(); this.incrementinstructioncounter(); (int = array.length - 1; > 0; i--) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); first = i; this.incrementinstructioncounter(); this.incrementinstructioncounter(); (int j = 1; < i; j++) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); if (array[j] < array[first]) { this.incrementinstructioncounter(); first = j; this.incrementinstructioncounter(); } } this.incrementinstructioncounter(); temp = array[first]; this.incrementinstructioncounter(); array[first] = array[i]; this.incrementinstructioncounter(); array[i] = temp; } } public void insertionsort(int[] array) { int temp; this.incrementinstructioncounter(); this.incrementinstructioncounter(); (int = 1; < array.length; i++) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); temp = array[i]; this.incrementinstructioncounter(); int j; this.incrementinstructioncounter(); this.incrementinstructioncounter(); (j = - 1; j >= 0 && temp < array[j]; j--) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); array[j + 1] = array[j]; this.incrementinstructioncounter(); } array[j + 1] = temp; this.incrementinstructioncounter(); } } public void mergersort(int[] array) { mergesort(array); } public int[] mergesort(int[] array) { if (array.length > 1) { this.incrementinstructioncounter(); int elementsina1 = array.length / 2; this.incrementinstructioncounter(); this.incrementinstructioncounter(); int elementsina2 = elementsina1; this.incrementinstructioncounter(); if ((array.length % 2) == 1){ this.incrementinstructioncounter(); this.incrementinstructioncounter(); elementsina2 += 1; this.incrementinstructioncounter(); } int arr1[] = new int[elementsina1]; this.incrementinstructioncounter(); int arr2[] = new int[elementsina2]; this.incrementinstructioncounter(); this.incrementinstructioncounter(); (int = 0; < elementsina1; i++) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); arr1[i] = array[i]; this.incrementinstructioncounter(); } this.incrementinstructioncounter(); (int = elementsina1; < elementsina1 + elementsina2; i++){ this.incrementinstructioncounter(); this.incrementinstructioncounter(); arr2[i - elementsina1] = array[i]; this.incrementinstructioncounter(); this.incrementinstructioncounter(); } arr1 = mergesort(arr1); arr2 = mergesort(arr2); int = 0, j = 0, k = 0; this.incrementinstructioncounter(); this.incrementinstructioncounter(); this.incrementinstructioncounter(); while (arr1.length != j && arr2.length != k) { this.incrementinstructioncounter(); this.incrementinstructioncounter(); if (arr1[j] < arr2[k]) { this.incrementinstructioncounter(); array[i] = arr1[j]; this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); j++; this.incrementinstructioncounter(); } else { array[i] = arr2[k]; this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); k++; this.incrementinstructioncounter(); } } while (arr1.length != j) { this.incrementinstructioncounter(); array[i] = arr1[j]; this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); j++; this.incrementinstructioncounter(); } while (arr2.length != k) { this.incrementinstructioncounter(); array[i] = arr2[k]; this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); k++; this.incrementinstructioncounter(); } } return array; } public void quicksort(int[] array) { int low = 0; this.incrementinstructioncounter(); int high = array.length - 1; this.incrementinstructioncounter(); int middle = low + (high - low) / 2; this.incrementinstructioncounter(); int pivot = array[middle]; this.incrementinstructioncounter(); int = low; this.incrementinstructioncounter(); int j = high; this.incrementinstructioncounter(); while (i <= j) { this.incrementinstructioncounter(); while (array[i] < pivot) { this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); } while (array[j] > pivot) { this.incrementinstructioncounter(); j--; this.incrementinstructioncounter(); } if (i <= j) { this.incrementinstructioncounter(); int temp = array[i]; this.incrementinstructioncounter(); array[i] = array[j]; this.incrementinstructioncounter(); array[j] = temp; this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); j--; this.incrementinstructioncounter(); } } } public void randomizedquicksort(int[] array) { int low = 0; this.incrementinstructioncounter(); int high = array.length - 1; this.incrementinstructioncounter(); random r = new random(); this.incrementinstructioncounter(); int pivot = array[r.nextint(array.length)]; this.incrementinstructioncounter(); int = low; this.incrementinstructioncounter(); int j = high; this.incrementinstructioncounter(); while (i <= j) { this.incrementinstructioncounter(); while (array[i] < pivot) { this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); } while (array[j] > pivot) { this.incrementinstructioncounter(); j--; this.incrementinstructioncounter(); } if (i <= j) { this.incrementinstructioncounter(); int temp = array[i]; this.incrementinstructioncounter(); array[i] = array[j]; this.incrementinstructioncounter(); array[j] = temp; this.incrementinstructioncounter(); i++; this.incrementinstructioncounter(); j--; this.incrementinstructioncounter(); } } } }
you're using indexof
when mean get
. arraylist.indexof(5)
position of number 5
in list, or -1
if it's not there. arraylist.get(5)
number @ index 5.