0

I have created the following class to sort an array of strings.

public class StringSort {
private String[] hotelNames;
private int arrayLength;

public void sortHotel(String[] hotelArray) {
    if (hotelArray.length <= 1) {
        return;
    }
    this.hotelNames = hotelArray;
    arrayLength = hotelArray.length;
    quicksort(0, arrayLength - 1);
}

private void quicksort(int low, int high) {
    int i = low, j = high;
    String first = hotelNames[low];
    String last = hotelNames[high];     
    String pivot = hotelNames[low + (high - low) / 2];

    while( (first.compareTo(last)) < 0 ) { // first is less than last
        while( (hotelNames[i].compareTo(pivot)) < 0 ) { // ith element is < pivot
            i++;
        }
        while( (hotelNames[j].compareTo(pivot)) > 0) { // jth element is > pivot
            j--;
        }
        if ( ( hotelNames[i].compareTo( hotelNames[j] )) <= 0 ) {
            swap(i, j);
            i++;
            j--;                
        }

        //recursive calls
        if (low < j) {
            quicksort(low, j);
        }
        if (i < high) {
            quicksort(i, high);
        }
    }
}

private void swap(int i, int j) {
    String temp = hotelNames[i];
    hotelNames[i] = hotelNames[j];
    hotelNames[j] = temp;
}

}

However in my main class (a class to test StringSort), when I do:

StringSort str = new StringSort();
String[] hotel1 = {"zzzz", "wwww", "dddd", "bbbbb", "bbbba", "aaaf", "aaag", "zzz"};
str.sortHotel(hotel1);

And then I have another method that prints out the array. However when it prints out, it outputs the hotel1 array as it is, unchanged. There is no 'sorting' happening, I'm not sure where I've gone wrong.

3
  • 3
    What debugging have you done? Add a printout to swap() and see if it's doing the swaps you would expect. Commented Mar 31, 2013 at 20:05
  • No debugging sorry, still learning to do that. Commented Mar 31, 2013 at 20:08
  • I thought compareTo was suitable as it compares strings lexicographically. Commented Mar 31, 2013 at 20:13

1 Answer 1

6

There are several problems in your implementation of quicksort:

  1. First/last comparison. This code will made your quicksort not do anything as long as first element is less than the last element, regardless of any other order.

    while( (first.compareTo(last)) < 0 ) { // first is less than last
    
  2. Check before swap. This line is unnecessary:

    if ( ( hotelNames[i].compareTo( hotelNames[j] )) <= 0 ) {
    

What you really want to do is see if the i is still less than j and bail out of the loop then. If not, then swap. After you finished with the partitioning loop, then make the recursive call, as long as there are more than two elements in each subarray.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I knew that that line was dodgy, I was confusing high/low with my pointers i and j. I Changed it and it now works. I was basing this class of another quicksort I made for integers, that made me bit confused with i's and j's. Thanks man.
Awesome! Beware though that there may be other problems lurking in there: your algorithm may perform badly on data that has lots of dupes, and on already sorted data.
I will be thoroughly testing it, before implementing it into my program, thank you for pointing it out. I love this site!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.