0

I have been trying to implement Bubble Sort using simple integer array in java. However there seems to be some problem. Now i know that using ArrayList would be the best option and I would do that too. But why isnt it getting sorted with simple integer array.Here is the code

package sort;

public class BubbleSort {

    int array[]={1,5,3,32,54,6,87,5,1};
    int temp=0;
public void enter(){
    for(int i=0;i<array.length;i++){
        for(int j=0;j<(array.length-i);j++){
            if(array[j]>=array[j+1]){


                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
                }

        }
    }
}
public void show(){

    for(int i:array){
    System.out.println(i);
    }
}
public static void main(String str[]){

    new BubbleSort().Enter();
    new BubbleSort().Show();
}
}

Its producing the same array as entered. Nothing is getting changed. The difference between a simple array and an ArrayList or Vector ,is just that they offer dynamic time expansion of array size.Is there anything more to it? I mean does simple array creates a different instance every time it is manipulated, just like Strings? It does seem to do so here.

2
  • 3
    Please note that it is convention in Java for package names to be lower-case, and method names to be camelCase. See docstore.mik.ua/orelly/java-ent/jnut/ch07_01.htm. It is surprisingly confusing when Java developers read code that violates this. Commented Sep 3, 2012 at 17:35
  • 1
    No this is not homework. And I am using eclipse This particular package has all the sorting programs. I dont know how to rename it.I know its not the right convention. I'll keep that in mind next time.Thank you that was so helpful Commented Sep 3, 2012 at 17:41

4 Answers 4

5

The problem is that you're not assigning a name to the instantiation of your BubbleSort class.

new BubbleSort().Enter();
new BubbleSort().Show();

Your code creates a new BubbleSort class, and then sorts it. And then it creates another new (and completely separate) BubbleSort class, and displays that one instead - and it hasn't been sorted.

You want to give a name to your variable, so you can sort it and then display it, like this:

BubbleSort myBubbleSort = new BubbleSort();
myBubbleSort.Enter();
myBubbleSort.Show();

As a side note (and as pointed out in SiB's answer), you may also want to check out the Java Naming Conventions. Following these conventions makes your code more legible to other Java programmers, and includes things like using lowerCamelCase for method names, and UpperCamelCase for class names.

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

1 Comment

Thank you !! How did I miss that !
5

Because you are sorting one instance and showing another.

new BubbleSort().Enter();
new BubbleSort().Show();

Use

BubbleSort bubbleSort = new BubbleSort();
bubbleSort.Enter();
bubbleSort.Show();

Also you should rename Enter() to enter() and Show() to show() to say the least.

Comments

3

Because you are creating two different BubbleSort Objects, sorting the first one and displaying a different one.

It should have been....

public static void main(String str[]){

    BubbleSort sort = new BubbleSort();
    sort.Enter();
    sort.Show():

}

Comments

1

And the correct BubbleSort code is:

    int temp = 0;
    for (int i = 0; i < array.length; i++) {
        for (int j = 1; j < (array.length - i); j++) {
            if (array[j - 1] > array[j]) {
                temp = array[j - 1];
                array[j - 1] = array[j];
                array[j] = temp;
            }

        }
    }

I hope it helps someone else looking for it.

Comments

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.