A BubbleSort class with a method called sort that accepts an array of Strings and sorts the array in
alphabetical order (regardless of case) and a method called print that displays the strings in the array.
The algorithm should implement one efficiency improvement in that once a list is sorted, the algorithm
should end rather than continuing to attempt to sort. So if an array of 100 sorted items is given to the
sort routine, the algorithm should only make 1 pass before ending.
Example
Sorting a list such as:
arr = {“Banana”, “Apple”, “artichoke”};
sorter.sort();
sorter.print();
would display:
Apple
artichoke
Banana
This is what i have so far though im not sure if im doing it the right way and i still need it to ignore the case when sorting
import java.util.*;
public class BubbleSort {
public static void main(String[] args) {
//Declare and Initialize Array
String list[]={"artichoke" , "Apple" , "CHERRY" , "banana"};
BubbleSort(list);
//Begin For loop
for(int i=0; i<list.length; i++)
{
System.out.println(list[i]);
}
}// End of Main
private static void BubbleSort(String[] array) {
String temp;
// Begin For loop
for(int i=0; i<array.length; i++) {
//Open For loop
for(int j=0; j<array.length-1-i; j++) {
//Open If Statement Compare and sort strings
if(array[j].compareTo(array[j+1])>0) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}//End of If Statement
}//End of For Loop
}//End of For Loop
}//End of Private Class BubbleSort
}// End of Public class BubbleSort

New Topic/Question
Reply



MultiQuote







|