/* * Bubble sort - String and integer UI */ package bubblesortStringInteger; public class BubbleSortStringIntegerUI { public static void main(String[]args) { BubbleSortStringInteger bs = new BubbleSortStringInteger(); int[] intArr = {6,7,5,6,6,4,8,7,5,7,7,2,4,2,3,6,7}; int countInt = intArr.length; String[] strArr = {"Delta", "Alpha", "Echo", "Bravo", "Charlie", "Foxtrot"}; int countStr = strArr.length; System.out.println("Unsorted Integer"); System.out.println("==============="); for (int i = 0; i < countInt; i++){ System.out.println(intArr[i]); } System.out.println("Unsorted String"); System.out.println("==============="); for (int i = 0; i < countStr; i++){ System.out.println(strArr[i]); } intArr = bs.sortInteger(intArr); strArr = bs.sortString(strArr); System.out.println("Sorted Integers"); System.out.println("==============="); for(int i = 0; i < intArr.length;i++){ System.out.println(intArr[i]); } System.out.println("Sorted String"); System.out.println("==============="); for(int i = 0; i < strArr.length;i++){ System.out.println(strArr[i]); } } // end main } // end class =========================================================================== /* * The bubble sort - String and integer */ package bubblesortStringInteger; public class BubbleSortStringInteger { public int[] sortInteger(int[] intArr) { int count = intArr.length; int temp = 0; for(int i = 0; i < count - 1 ; i++){ for(int j = i + 1 ; j < count; j++){ if(intArr[i] > (intArr[j])) { // swap around temp = intArr[i]; intArr[i] = intArr[j]; intArr[j] = temp; } } } return intArr; } // end sortInteger public String[] sortString(String[] strArr) { // "compareTo" subtracts the unicode value of one character from the // unicode value of the other character. If the answer is zero, // the letters are the same. If not, they must be swapped around. int count = strArr.length; String temp = null; for(int i = 0; i < count - 1 ; i++){ for(int j = i + 1 ; j < count; j++){ if(strArr[i].compareTo(strArr[j]) > 0) { // swap around temp = strArr[i]; strArr[i] = strArr[j]; strArr[j] = temp; } } } return strArr; } // end sortString } // end class =========================================================================================== OUTPUT Unsorted Integer =============== 6 7 5 6 6 4 8 7 5 7 7 2 4 2 3 6 7 Unsorted String =============== Delta Alpha Echo Bravo Charlie Foxtrot Sorted Integers =============== 2 2 3 4 4 5 5 6 6 6 6 7 7 7 7 7 8 Sorted String =============== Alpha Bravo Charlie Delta Echo Foxtrot