java - Already sorted array -
java - Already sorted array -
i have array including user's inputs. programme bubble sort, selection sort , insertion sort. first bubble, sec selection , insertion sort comes.
i couldn't manage solve problem. when code run selection sort, array sorted bubble sort.
i tried create 2 temporary arrays @ first utilize "source array" @ selection , insertion sorting arrays re-arranged bubble sort again. ( don't understand why )
is there way sort array seperately or have create them methods ? i'm counting swaps , comparisons btw. !
system.out.println("• please come in number of elements in sorting bag:"); length = input.nextint(); system.out.println("• number of elements: " + length); int[] sorbag = new int[length]; int[] sorbag2 = new int[length]; int[] sorbag3 = new int[length]; system.out.println("• please come in elements of sorting bag:"); (int = 0; < sorbag.length ; i++) { sorbag[i] = input.nextint(); } sorbag2 = sorbag; sorbag3 = sorbag; system.out.print("• elements in sorting handbag are:"); (int j = 0; j < sorbag.length; j++) { system.out.print(" " + sorbag[j]); } system.out.println(""); system.out.println(""); //bubble sort (int = 1; < sorbag.length; i++) { (int j = 0; j < sorbag.length - i; j++) { bcomparison++; if (sorbag[j] > sorbag[j + 1]) { bswaps++; temp1 = sorbag[j + 1]; sorbag[j + 1] = sorbag[j]; sorbag[j] = temp1; } } } system.out.print("• bubble sort:"); (int k = 0; k < sorbag.length; k++) { system.out.print(" " + sorbag[k] + " "); } system.out.print("comparisons: " + bcomparison + " swaps: " + bswaps); system.out.println(" "); //selection sort (int = 0; < sorbag2.length; i++) { min = i; (int j = + 1; j < sorbag2.length; j++) { scomparison++; if (sorbag2[j] < sorbag2[min]) { min = j; } if (min != i) { temp2 = sorbag2[i]; sorbag2[i] = sorbag2[min]; sorbag2[min] = temp2; sswaps++; } } } system.out.print("• selection sort:"); (int k = 0; k < sorbag2.length; k++) { system.out.print(" " + sorbag2[k] + " "); } system.out.print("comparisons: " + scomparison + " swaps: " + sswaps); system.out.println(" "); //insertion sort (int = 1; < sorbag3.length; i++) { int j = 0; while (j > && sorbag3[j] < sorbag3[j - 1]) { temp3 = sorbag3[j]; sorbag3[j] = sorbag3[j - 1]; sorbag3[j - 1] = temp3; iswaps++; j--; } icomparison++; } system.out.print("• insertion sort:"); (int k = 0; k < sorbag3.length; k++) { system.out.print(" " + sorbag3[k] + " "); } system.out.print("comparisons: " + icomparison + " swaps: " + iswaps); system.out.println(" "); } }
sorbag2 = sorbag
, sorbag3 = sorbag
copies reference of sorbag
other 2 arrays, instead of copying data. instead of:
system.out.println("• please come in elements of sorting bag:"); (int = 0; < sorbag.length ; i++) { sorbag[i] = input.nextint(); } sorbag2 = sorbag; sorbag3 = sorbag;
try this:
system.out.println("• please come in elements of sorting bag:"); (int = 0; < sorbag.length ; i++) { int nextint = intput.nextint(); sorbag[i] = nextint; sorbag2[i] = nextint; sorbag3[i] = nextint; }
java arrays sorting
Comments
Post a Comment