/** * Illustrates two sorting algorithms on an integer array */ import java.io.*; import java.util.Scanner; public class SortArray { public static void main(String[] args) throws IOException { int[] arr = {149, 72, 15, 19, 52, 66, 83, 100, 45, 18, 26, 150}; //selectionSort1method(arr); bubbleSort(arr); for (int x: arr) System.out.print(x + " "); System.out.println(); } public static void bubbleSort(int[] arr) { boolean swapped = true; int pass=0; while (swapped) { swapped=false; pass++; // iterate through comparing neighbors for (int i=0; iarr[i+1]) { swapped=true; swap(arr, i, i+1); } } } // Easier version to memorize puts selectionSort into 1 method public static void selectionSort1method(int[] arr) { // One by one move boundary of unsorted subarray for (int i = 0; i < arr.length-1; i++) { // Find the minimum element in array from loc i+1 until end int minIndex = i; for (int j = i+1; j < arr.length; j++) if (arr[j] < arr[minIndex]) minIndex = j; // Swap the found minimum element with the first // element (loc i) int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } /** Selection Sort algorithm repeatedly finds the min and puts it * in its place. Uses subsidiary methods of findMindIndex and swap. */ public static void selectionSort(int[] nums) { for (int i=0; i=nums.length) return -1; //int min=nums[startIndex]; int minIndex = startIndex; for (int i=startIndex+1; i