// Andrew Martin // Bubble sort of a fixed array with options for ascending or descending import javax.swing.JOptionPane; public class SortDoublesWithOptions { private static double[] listItem = {5.3, 4.7, 6.1, 81.6, 9.41, 1.4, 3.74, 1.11, 7.44, 32.18, 18.44, 1.18, 1.8, 30.7, 4.85}; public static void main (String[] args) { String userInput; char sortType = ' '; JOptionPane.showMessageDialog(null, "This is the array before it is sorted:"); displayArray(); while(sortType != 'a' && sortType != 'd') { userInput = JOptionPane.showInputDialog(null, "Please enter a sort method.\nA for ascending\nD for descending"); sortType = Character.toLowerCase(userInput.charAt(0)); } sortArray(sortType); JOptionPane.showMessageDialog(null, "This is the array after it is sorted:"); displayArray(); System.exit(0); } public static void sortArray(char sortType) { double tempItem; int cycleCount = listItem.length - 1; if(sortType == 'a') for(int index = 0; index < listItem.length - 1; ++index) { for(int count = 0; count < cycleCount; ++count) { if(listItem[count] > listItem[count + 1]) { tempItem = listItem[count]; listItem[count] = listItem[count + 1]; listItem[count + 1] = tempItem; } } --cycleCount; } else for(int index = 0; index < listItem.length - 1; ++index) { for(int count = 0; count < cycleCount; ++count) { if(listItem[count] < listItem[count + 1]) { tempItem = listItem[count]; listItem[count] = listItem[count + 1]; listItem[count + 1] = tempItem; } } --cycleCount; } } public static void displayArray() { StringBuffer arrayString = new StringBuffer(100); for(int count = 0; count < listItem.length; ++count) arrayString.append(listItem[count] + ", "); JOptionPane.showMessageDialog(null, arrayString); } }