public class Exercise { public static void main(String[] args) { int[] array = {2,3,4,1,5}; int[] myarray = {2,9,5,4,8,1,6}; System.out.println("my normal array is: " + Arrays.toString(array)); //bubbleSort array: bubbleSort(array); System.out.println(); System.out.println("After Sorting From Small number to Large number. "); System.out.println("\t" + Arrays.toString(array)); sort(array); System.out.println(); System.out.println("After Sorting From Large number to Small number: "); System.out.println("\t" + Arrays.toString(array)); //selection array System.out.println(); System.out.println("my second array is : " + Arrays.toString(myarray)); selectionArray(myarray); System.out.println(); System.out.println("After Selection Array from minimum number to maximum number: "); System.out.println("\t" + Arrays.toString(myarray)); } public static void bubbleSort(int[] array) { for(int i=0; i<array.length; i++) { for(int j=0; j<array.length-1-i; j++) { if(array[j] > array[j+1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } public static void sort(int[] array) { for(int i=0; i<array.length; i++) { for(int j=0; j<array.length-1-i; j++) { if(array[j] < array[j+1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } public static void selectionArray(int[] myarray) { for(int i =0; i<myarray.length; i++) { //find minimum in the list int currentMin = myarray[i]; int currentMinIndex = i; for(int j= i+1; j< myarray.length; j++) { if(currentMin > myarray[j]) { currentMin = myarray[j]; currentMinIndex = j; } } //swap list[i] with list[currentMinIndex] if(currentMinIndex != i) { myarray[currentMinIndex] = myarray[i]; myarray[i] = currentMin; } } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter