1. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

Hello, i am doing the exercise on text book.
Can anyone deal with my question?

The following code is for bubble sort.

public class BubbleSort {

public void bubbleSort(Integer [] list) {

boolean sorted = false;

for (int top = list.length - 1; top > 0 && !sorted; top--) {

sorted = true;

for (int i = 0; i < top; i++) {

if (_____fill in code here_______ ) {

sorted = false;

...

... fill in code here

...
}

}

}

}

}

Question :
Complete the codes provided and write an executable Java class BubbleSortTester which contains a static main method to create BubbleSort objects and pass the following array to perform the sorting. The testing program will print the array before and after the sorting. Show the program source code and the screen dumps of testing.

11 　2　 8 　3　 6　 15　 12 　0 　7　 4　 1　 13　 5　 9 　14　 10

Sample output:
The elements are
11 2 8 3 6 15 12 0 7 4 1 13 5 9 14 10
The sorted order is
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

2. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

The following code is what I've typed.
I am totally lost within the code.

public class BubbleSort {

public void bubbleSort(Integer [] list){
boolean sorted = false;

for (int top = list.length - 1; top > 0 && !sorted; top--) {
sorted = true;

for (int i = 0; i < top; i++) {
if ( list[i] > list[i+1] ) {
sorted = false;
int temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;

}

}
}
}
}

public class BubbleSorTester {

public static void main(String [] args) {
int [] num = {11, 2, 8, 3, 6, 15, 12, 0, 7, 4, 1, 13, 5, 9, 14, 10 };
asort(num);
System.out.println("The element are \n");
for (int i = 0; i < list.length; i++)
System.out.print (list[i] + " " );
System.out.println();
}
}

3. Member
Join Date
Jun 2013
Posts
60
Rep Power
0

## Re: Few question about BubbleSort

Its a lot easier to see your logic if you indent your braces like this:

Java Code:
```if (foo + bar == krump)
{

while (krump - bar== feelings)
{

}
}```

4. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

Originally Posted by ras_oscar
Its a lot easier to see your logic if you indent your braces like this:

Java Code:
```if (foo + bar == krump)
{

while (krump - bar== feelings)
{

}
}```
Do you mean the code like 1st post?
I did it before I posted, but it seems not work 2nd post.
By the way, can u fill in the code to let me familiarize it ??

5. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

Java Code:
```public class BubbleSort {

public void bubbleSort(Integer [] list) {

boolean sorted = false;

for (int top = list.length - 1; top > 0 && !sorted; top--) {

sorted = true;

for (int i = 0; i < top; i++) {

if (_____fill in code here_______ ) {

sorted = false;

...

... fill in code here

...
}

}

}

}

}```

6. Moderator
Join Date
Apr 2009
Posts
13,234
Rep Power
23

## Re: Few question about BubbleSort

I htink ras_oscar was thinking more about doing that for the code you'd written.

7. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

I have already corrected the code.
It can be compiled successfully. But, the code haven't sort.

Java Code:
```public class BubbleSort {

public void bubbleSort(Integer [] list){
boolean sorted = false;

for (int top = list.length - 1; top > 0 && !sorted; top--) {
sorted = true;

for (int i = 0; i < top; i++) {
if ( list[i-1] > list[i] ) {
sorted = false;
int temp = list[i];
list[i] = list[i-1];
list[i-1] = temp;
}
}
}
}
}

public class BubbleSorTester {

public static void main(String [] args) {
int list [] = new int [] {11, 2, 8, 3, 6, 15, 12, 0, 7, 4, 1, 13, 5, 9, 14, 10 };

System.out.println("The element are ");
for (int i = 0; i < list.length; i++){
System.out.print (list[i] + " " );
}

BubbleSort access = new BubbleSort();

System.out.println(" ");

System.out.println("The sorted order is ");
for(int i=0; i < list.length; i++){
System.out.print(list[i] + " ");

}
}
}```

8. Member
Join Date
Jun 2013
Posts
60
Rep Power
0

## Re: Few question about BubbleSort

If it doesn't sort, what does it do?

9. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

Originally Posted by ras_oscar
If it doesn't sort, what does it do?
Output:
The elements are
11 2 8 3 6 15 12 0 7 4 1 13 5 9 14 10
The sorted order is
11 2 8 3 6 15 12 0 7 4 1 13 5 9 14 10

10. ## Re: Few question about BubbleSort

Originally Posted by ras_oscar
Its a lot easier to see your logic if you indent your braces like this: [ ... ]
That is all in the eye of the beholder; I, for one, hate the style you're recommending; I use (proper) K&R style only; the style you mentioned is for folks from a Pascal world.

kind regards,

Jos

ps. seriously though, as long as a style is used consistently, it is fine with me. Formatters can change the bracket positions for me after all ...

11. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

What i did wrong?

12. Member
Join Date
Jun 2013
Posts
60
Rep Power
0

## Re: Few question about BubbleSort

Where do you pass the integer array to your sort routine?

to josah: I've never even seen a pascal program. I did c++, but didn't realize there were different styles of indentation for each language, so I've learned something. Thanks. I find the format I illustrated is easier for me to see the statement nesting. I guess its what you've become accustomed to that matters. I'll give the "official" Java style a go nect time I gert lost in my code and see if it helps.
Last edited by ras_oscar; 10-17-2013 at 08:13 PM.

13. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

Originally Posted by ras_oscar
Where do you pass the integer array to your sort routine?

to josah: I've never even seen a pascal program. I did c++, but didn't realize there were different styles of indentation for each language, so I've learned something. Thanks. I find the format I illustrated is easier for me to see the statement nesting. I guess its what you've become accustomed to that matters. I'll give the "official" Java style a go nect time I gert lost in my code and see if it helps.
I dont know.
I just base on the code and run the program.
was I missed to call the method??

14. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,844
Rep Power
10

## Re: Few question about BubbleSort

Calling a method to invoke the sort routine is relatively simple compared to writing the sort algorithm. So did you actually write this code yourself? Do you understand how it works?

Regards,
Jim

15. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

Originally Posted by jim829
Calling a method to invoke the sort routine is relatively simple compared to writing the sort algorithm. So did you actually write this code yourself? Do you understand how it works?

Regards,
Jim
yes I write it myself.
in this case, how to call the method?

16. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,844
Rep Power
10

## Re: Few question about BubbleSort

Your method is called bubbleSort. You have an instance of BubbleSort called access.

So access.bubbleSort(list);

is how you call it. I recommend you change your method signature to use int[] as opposde to Integer[].

Regards,
Jim

17. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

Originally Posted by jim829
Your method is called bubbleSort. You have an instance of BubbleSort called access.

So access.bubbleSort(list);

is how you call it. I recommend you change your method signature to use int[] as opposde to Integer[].

Regards,
Jim
Java Code:
```public class BubbleSorTester {

public static void main(String [] args) {
int list [] = new int [] {11, 2, 8, 3, 6, 15, 12, 0, 7, 4, 1, 13, 5, 9, 14, 10 };

System.out.println("The elements are ");
for (int i = 0; i < list.length; i++){
System.out.print(list[i] + " " );
}

bubbleSort access = new bubbleSort ();
access.bubbleSort(list);

System.out.println(" ");

System.out.println("The sorted order is ");

for(int i=0; i < list.length; i++){
System.out.print(list[i] + " ");

}
}
}```
compile error happened

18. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,844
Rep Power
10

## Re: Few question about BubbleSort

Why did you change the following?

bubbleSort access = new bubbleSort ();

Put it the way it was. BubbleSort is the class name. bubbleSort is the method within the class. Also, it is not a good idea to name your methods the same as the class in which they reside.

Regards,
Jim

19. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

FINALLY I DID IT!!!!!
THANK JIM

20. Member
Join Date
Oct 2013
Posts
17
Rep Power
0

## Re: Few question about BubbleSort

Also, thank all of you who have answered my question, I love you guys!!!

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•