2

Let me first say that I know that there a better ways to sort where you might use something other than an array. This an assignment for class where the user can store strings in an array, delete, display, and sort them. I am completely lost to where to go from here. I am trying to use a bubble sort and everything works except whatever is the first entry in my array won't sort. I avoid null pointer exceptions through filtering out null values which is why my if statement is so long.

private void sortItems(String[] cargohold) {
    String temp;
    for (int i = 0; i < cargohold.length - 1; i++) {
        for (int j = 1; j < cargohold.length; j++) {
            if (cargohold[i] != null && cargohold[j] != null && (cargohold[i].compareTo(cargohold[j]) < 0)) {
                temp = cargohold[j];
                cargohold[j] = cargohold[i];
                cargohold[i] = temp;
            }   
        }
    }
}

I have tried a bunch of different ways of doing this and I can't find any good reason why this shouldn't work. I have looked through anything I could find on Stack Overflow as far as examples and no one is having the same issue I am.

To recap, I may have 5 strings, "Derp", "Herp", "Sam", "Alfred", "Bill" and this sort will give me: "Derp", "Alfred", "Bill", "Herp", "Sam". Thanks in advance for the guidance.

2
  • 1
    First of all this is not a bubble sort there's the first issue Commented Sep 6, 2017 at 17:44
  • @WIR3D you should explain why it is not. The inner loop of a bubble sort normally starts at i + 1, not at 1. As an aside: please wirte the array-brackets after the type, not the variable (String[] cargohold instead of String cargohold[]). Commented Sep 6, 2017 at 17:47

4 Answers 4

3

The line

if(cargohold[i] != null && cargohold[j] != null && (cargohold[i].compareTo(cargohold[j]) < 0))

should be

if(cargohold[j] != null && cargohold[j-1] != null && (cargohold[j].compareTo(cargohold[j-1]) < 0))

and the swapping should be done as:

temp = cargohold[j];
cargohold[j] = cargohold[j-1];
cargohold[j-1] = temp;

Remember that in bubblesort you compare adjacent elements, where as your code does not do that.

Flaw

There will be cases when i > j and i < j, but the swapping logic remains the same, and that is completely wrong.

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

5 Comments

You also need to keep track of whether or not an element has been changed allowing you to know when the sort is complete
@WIR3D, That is one of the discrete optimization , which you can do, but thats not the point here.
@SumeetSingh can you explain to me why this works as opposed to my code?
@user8570492 Because you have not compared the adjacent elements, which is what you do in bubbe sort.
@user8570492 If you carefully observe, there will be cases when i>j and i<j, but the swap logic remains the same, and that is the main flaw.
2

Bubblesort algorithm with some optimizations:

private static void sortItems(String cargohold[]) {
    String temp;
    boolean wasSwap = true;
    for (int index1 = 0; index1 < cargohold.length - 1 && wasSwap; ++index1) {
        wasSwap = false;
        for (int index2 = 0; index2 < cargohold.length - index1 - 1; ++index2) {
            if (cargohold[index2].compareToIgnoreCase(cargohold[index2+1]) > 0) {
                temp = cargohold[index2];
                cargohold[index2] = cargohold[index2+1];
                cargohold[index2+1] = temp;
                wasSwap = true;
            }
        }
    }
}

Average complexity being O(n^2) with the best case of O(n).

Comments

0

Your implementation is wrong.

Here is bubble sort (in a java-like shorthand):

for (index1 = 0; index1 < array.length - 1; ++index1)
    for (index2 = index1 + 1; index2 < array.length; ++index2)
        if (array[index1] < array[index1])
            swap(array[index1], array[index2]);

note the index2 = index1 + 1 of the inner loop.

Comments

0
String[] str = { "red", "blue", "green" };
int n = str.length;
String temp;
System.out.println("Original String Array = " + Arrays.toString(str));
for(int a=0;a<n;a++) {
    for(int b=a+1;b<n;b++) {
        if( str[a].compareTo(str[b]) > 0 ) {
            temp = str[a];
            str[a] = str[b];
            str[b] = temp;
        }
    }
}
System.out.println("Sorted String Array = " + Arrays.toString(str));

1 Comment

If you add explanation, it'd be better.

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.