/** * Illustrates bubble sort on an array of Strings */ import java.io.*; import java.util.Scanner; public class SortStringArray { public static void main(String[] args) throws IOException { int[] arr = {149, 72, 15, 19, 52, 66, 83, 100, 45, 18, 26, 150}; String[] strArr = {"ac", "abc", "ab", "a", "do", "re", "me", "fa", "so", "la", "tee"}; //selectionSort1method(arr); bubbleSort(strArr); for (String s: strArr) System.out.print(s + " "); System.out.println(); } public static void bubbleSort(String[] arr) { boolean swapped = true; int pass=0; while (swapped) { swapped=false; pass++; // iterate through comparing neighbors for (int i=0; i 0) { // (arr[i]>arr[i+1]) { swapped=true; //swap(arr, i, i+1); String temp = arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } } System.out.println("number of passes when bubble sort completed: "+ pass); } // 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; } } }