Results 1 to 20 of 21
Thread: Few question about BubbleSort
- 10-17-2013, 05:51 PM #1
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
Few question about BubbleSort
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 #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();
}
}
- 10-17-2013, 06:10 PM #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) { } }
- 10-17-2013, 06:17 PM #4
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
- 10-17-2013, 06:19 PM #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 ... } } } } }
- 10-17-2013, 07:01 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Few question about BubbleSort
I htink ras_oscar was thinking more about doing that for the code you'd written.
Please do not ask for code as refusal often offends.
** This space for rent **
- 10-17-2013, 07:26 PM #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] + " "); } } }
- 10-17-2013, 08:20 PM #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?
- 10-17-2013, 08:24 PM #9
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
- 10-17-2013, 08:54 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Few question about BubbleSort
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 ...Build a wall around Donald Trump; I'll pay for it.
- 10-17-2013, 08:55 PM #11
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
Re: Few question about BubbleSort
What i did wrong?
- 10-17-2013, 09:08 PM #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 09:13 PM.
- 10-17-2013, 09:22 PM #13
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
- 10-17-2013, 09:32 PM #14
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-17-2013, 09:55 PM #15
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
- 10-17-2013, 10:08 PM #16
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-17-2013, 10:23 PM #17
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
Re: Few question about BubbleSort
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] + " "); } } }
- 10-17-2013, 10:40 PM #18
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-17-2013, 11:07 PM #19
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
- 10-17-2013, 11:08 PM #20
Member
- Join Date
- Oct 2013
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Forum question on why discussion threads are locked though question is unanswered
By pseeburger in forum Suggestions & FeedbackReplies: 2Last Post: 05-25-2012, 05:00 PM -
Bubblesort Issue within Method
By burrish in forum New To JavaReplies: 9Last Post: 09-27-2011, 03:39 AM -
Could not find or load main class BubbleSort.class
By blaqkout in forum New To JavaReplies: 5Last Post: 09-12-2011, 08:54 PM -
Question with bubbleSort()
By Metastar in forum New To JavaReplies: 1Last Post: 09-13-2010, 06:55 PM -
Die Bubblesort
By N3VRMND in forum New To JavaReplies: 2Last Post: 11-06-2009, 04:44 PM
Bookmarks