How to loop through Selection Sort in Java? -
i want show each iteration of selection sort print out how works, how loop , print this? have printing out output after been sorting already. here's code:
public class testselectionsort { public static void main(string[] args) { int list[] = { 2, 56, 34, 25, 73, 46, 89, 10, 5, 16 }; selectionsort(list, list.length); system.out.println("after sorting, list elements are:"); (int = 0; < list.length; i++) { system.out.print(list[i] + " "); } } public static void selectionsort(int[] list, int listlength) { int index; int smallestindex; int minindex; int temp; (index = 0; index < listlength - 1; index++) { //step smallestindex = index; (minindex = index + 1; minindex < listlength; minindex++) if (list[minindex] < list[smallestindex]) smallestindex = minindex; //step b temp = list[smallestindex]; list[smallestindex] = list[index]; list[index] = temp; } } }
just copy snippet used printing final result:
for(int = 0; < list.length; i++) { system.out.print(list[i] + " "); } before end of loop in selectionsort().
Comments
Post a Comment