Calling insertion sort class from driver doesn't work
The bubble sort sorting in the driver class works fine but I make a new list and try to do insertion sort and it just doesn't sort the list. I was thinking it was something with the setSortBehavior method in the first Listing class but everything I've tried hasn't worked.
package strategydesignpattern;
import java.util.ArrayList;
public class Listing {
protected String title;
protected ArrayList<String> items;
protected SortBehavior theSortBehavior;
/**
* Creates a list with the indicated name
* @param title The title of the list
*/
public Listing(String title)
{
this.title = title;
this.items = new ArrayList<String>();
this.theSortBehavior = new BubbleSort();
}
/**
* Adds an item to the list
* @param item the item you want to add to the list
*/
public void add(String item)
{
items.add(item);
}
/**
* removes an item from the list
* @param item the item you want to remove from the list
*/
public void remove(String item)
{
items.remove(item);
}
/**
* a getter for the name of the list
* @return returns the title of the list
*/
public String getTitle()
{
return title;
}
/**
* sets the sort behavior to whatever is passed in
* @param sortBehavior gets the sort behavior, either insertion or bubble
*/
public void setSortBehavior(SortBehavior sortBehavior)
{
theSortBehavior = sortBehavior;
}
/**
*
* @return returns the sorted list of items
*/
public ArrayList<String> getSortedList()
{
return theSortBehavior.sort(items);
}
/**
*
* @return returns the unsorted list of items
*/
public ArrayList<String> getUnSortedList()
{
return items;
}
}
//////
package strategydesignpattern;
import java.util.ArrayList;
public interface SortBehavior {
public ArrayList<String> sort(ArrayList<String> data);
}
///
package strategydesignpattern;
import java.util.ArrayList;
public class BubbleSort implements SortBehavior{
public ArrayList<String> sort(ArrayList<String> data)
{
String temp;
boolean sorted = false;
while (!sorted) {
sorted = true;
for (int i = 0; i < data.size()-1; i++) {
if (data.get(i).compareTo(data.get(i + 1)) > 0) {
temp = data.get(i);
data.set(i, data.get(i + 1));
data.set(i + 1, temp);
sorted = false;
}
}
}
return data;
}
}
//
package strategydesignpattern;
import java.util.ArrayList;
public class InsertionSort implements SortBehavior{
public ArrayList<String> sort(ArrayList<String> data)
{
for (int j = 1; j < data.size(); j++) {
String current = data.get(j);
int i = j-1;
while ((i > -1) && ((data.get(i).compareTo(current)) == 1)) {
data.set(i+1, data.get(i));
i--;
}
data.set(i+1, current);
}
return data;
}
}
////
package strategydesignpattern;
import java.util.ArrayList;
/**
* Creates lists of information, and displays the sorted versions of the lists
*/
public class ListDriver {
/**
* Creates a new ListDriver
*/
public ListDriver(){}
/**
* Main entryway to the program
* Creates the lists, and displays the sorted lists
*/
public void run() {
Listing shoppingList = new Listing("Grocery List");
shoppingList.add("Apples");
shoppingList.add("Peaches");
shoppingList.add("Cheese");
shoppingList.add("Crackers");
shoppingList.add("Chocolate");
shoppingList.add("Cherries");
shoppingList.add("Bananas");
shoppingList.add("Oranges");
ArrayList<String> sortedGroceryItems = shoppingList.getSortedList();
System.out.println(shoppingList.getTitle());
displayList(sortedGroceryItems);
Listing wishList = new Listing("Wish List");
wishList.setSortBehavior(new InsertionSort());
wishList.add("Bike");
wishList.add("Paddle Board");
wishList.add("Boat");
wishList.add("Truck");
wishList.add("Puzzle");
wishList.add("Monolopy");
wishList.add("Skipping Rope");
wishList.add("Cherry Tree");
ArrayList<String> sortedWishListItems = wishList.getSortedList();
System.out.println("\n" + wishList.getTitle());
displayList(sortedWishListItems);
}
/**
* Loops through and displays each item in the list items
* @param items list to display
*/
private void displayList(ArrayList<String> items){
for(String item : items){
System.out.println("- " + item);
}
}
public static void main(String[] args){
ListDriver driver = new ListDriver();
driver.run();
}
}
Solution 1:
It appears that you have assumed the String.compareTo
will return 1
to indicate a lexicographical ordering where the string the method is being invoked on follows the argument string. The Documenation states that the return value for String.compareTo
is a positive integer to indicate this lexicographical ordering, not specifically 1
.