Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-17-2013, 05:51 PM
hills2720s
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
• 10-17-2013, 05:52 PM
hills2720s
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();
}
}
• 10-17-2013, 06:10 PM
ras_oscar
Its a lot easier to see your logic if you indent your braces like this:

Code:

```if (foo + bar == krump) {     while (krump - bar== feelings)     {     } }```
• 10-17-2013, 06:17 PM
hills2720s
Quote:

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

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 ??
• 10-17-2013, 06:19 PM
hills2720s
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 　　　　　　　... 　　　　　} 　　　　} 　　　} 　　} 　}```
• 10-17-2013, 07:01 PM
Tolls
I htink ras_oscar was thinking more about doing that for the code you'd written.
• 10-17-2013, 07:26 PM
hills2720s
I have already corrected the code.
It can be compiled successfully. But, the code haven't sort.

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] + " ");                 　　} 　} }```
• 10-17-2013, 08:20 PM
ras_oscar
If it doesn't sort, what does it do?
• 10-17-2013, 08:24 PM
hills2720s
Quote:

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-17-2013, 08:54 PM
JosAH
Quote:

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 ...
• 10-17-2013, 08:55 PM
hills2720s
What i did wrong?
• 10-17-2013, 09:08 PM
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.
• 10-17-2013, 09:22 PM
hills2720s
Quote:

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??
• 10-17-2013, 09:32 PM
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
• 10-17-2013, 09:55 PM
hills2720s
Quote:

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?
• 10-17-2013, 10:08 PM
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
• 10-17-2013, 10:23 PM
hills2720s
Quote:

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

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:s:
• 10-17-2013, 10:40 PM
jim829
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
• 10-17-2013, 11:07 PM
hills2720s