Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    jProtagonist is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Lightbulb Bubble sort array unsorted

    Hello,

    I wrote this piece of code, and it is supposed to use bubblesort to sort the array. Yet somehow it re-aranges it, but doesn't exactly sort it. For example, when I enter [3, 5, 6, 9, 8, 4, 7, 5, 6, 2], it prints Done [2, 5, 9, 5, 7, 8, 4, 6, 3, 6]
    Thank you in advance, any help is appreciated.

    Java Code:
    import java.lang.*;
    import java.util.*;
    import java.util.Arrays;
    
    public class bubble {
    	public static int[] array(){
    		int [] anArray = new int [10];
    		for (int x=0; x<=9; x++){
    			Scanner input=new Scanner(System.in);
    			System.out.println("What is the "+(x+1)+" th number?");
    			anArray[x]=input.nextInt();
    		}
    			//System.out.println("done");
    		System.out.println(Arrays.toString(anArray));
    			return anArray;
    	}
    	public static int[] bubbleSort (int [ ] myArray) {
    		for (int x=0;x<myArray.length-1;x++){
    			for (int y=0;y<myArray.length-1;y++){
    				if (myArray[y]>myArray[y+1]){
    					int holding=myArray[x];
    					myArray[x]=myArray[y+1];
    					myArray[y+1]=holding;
    			}}
    		}
    		return myArray;
    	}
    
    	public static void main(String [] args){
    		int[] array=array();
    		int[] sorted=bubbleSort(array);
    		System.out.println("Done "+Arrays.toString(sorted));
    	}
    }
    Last edited by jProtagonist; 07-08-2014 at 04:16 AM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    Have you tried stepping through this with a debugger, or at least adding print statements, to figure out when the program's execution differs from what you expect?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jProtagonist is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Bubble sort array unsorted

    Never mind, I fixed it my self. My if statement wasn't swapping the right numbers. Thank you anyway! :)
    Here is my final code:

    Java Code:
    public static int[] bubbleSort (int [ ] myArray) {
    		for (int x=0;x<myArray.length;x++){
    			for (int y=0;y<myArray.length;y++){
    				if (myArray[x]<myArray[y]){
    					int holding=myArray[y];
    					myArray[y]=myArray[x];
    					myArray[x]=holding;
    			}}
    		}
    		return myArray;

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    "When you do things right, people won't be sure you've done anything at all."
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by jProtagonist View Post
    Never mind, I fixed it my self. My if statement wasn't swapping the right numbers. Thank you anyway! :)
    Here is my final code:

    Java Code:
    public static int[] bubbleSort (int [ ] myArray) {
    		for (int x=0;x<myArray.length;x++){
    			for (int y=0;y<myArray.length;y++){
    				if (myArray[x]<myArray[y]){
    					int holding=myArray[y];
    					myArray[y]=myArray[x];
    					myArray[x]=holding;
    			}}
    		}
    		return myArray;
    That doesn't sort or accidentally at best ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,990
    Rep Power
    5

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by KevinWorkman View Post
    "When you do things right, people won't be sure you've done anything at all."
    ... are you quoting Futurama??
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by gimbal2 View Post
    ... are you quoting Futurama??
    Maaaaybe. I love (not even being sarcastic here) when I ask an OP to go off and do something and they come back with "nevermind, fixed it myself!" - I was showing you how to fix it yourself!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,990
    Rep Power
    5

    Default Re: Bubble sort array unsorted

    But Jos's famously intelligent ass has spotted that it ain't fixed at all. (that just sounds rude without further context: Scanner does not read to the end of a very long TXT file)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    Time to go back and use a debugger or add some more print statements then!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by gimbal2 View Post
    But Jos's famously intelligent ass has spotted that it ain't fixed at all.
    Except that it is fixed! The algorithm does indeed sort. But it is very inefficient, even for a bubble sort. And since it does an in place
    sort, there is no need to return the array.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by jim829 View Post
    Except that it is fixed! The algorithm does indeed sort. But it is very inefficient, even for a bubble sort. And since it does an in place
    sort, there is no need to return the array.
    The algorithm in reply #3 definitely doesn't sort.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by JosAH View Post
    The algorithm in reply #3 definitely doesn't sort.
    Bwah? It sorts for me, unless I'm missing a corner case:

    Java Code:
    import java.util.Arrays;
    
    public class Test {
    	public static void main (String [] args){
    
    		int[] array = new int[]{5, 4, 3, 2, 1, 6};
    
    		bubbleSort(array);
    
    		System.out.println(Arrays.toString(array));
    
    	}
    
    
    
    	public static int[] bubbleSort (int [ ] myArray) {
    		for (int x=0;x<myArray.length;x++){
    			for (int y=0;y<myArray.length;y++){
    				if (myArray[x]<myArray[y]){
    					int holding=myArray[y];
    					myArray[y]=myArray[x];
    					myArray[x]=holding;
    				}}
    		}
    		return myArray;
    	}
    }
    Prints: [1, 2, 3, 4, 5, 6]

    That is not to say this this is a good or efficient algorithm, even for bubble sort!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by JosAH View Post
    The algorithm in reply #3 definitely doesn't sort.

    kind regards,

    Jos
    Well, I just did 100 million iterations sorting the two arrays. The two arrays were identical for each sort. The provided bubbleSort algorithm had the exact same results as the JDK built-in Arrays.sort method. However, I could have made a mistake somewhere. Can you post the set of numbers which did not sort for you.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    Actually I sorta give up. OP I don't really know what you're doing. Like I said, you should add some print statements to figure out what's going on. Here is an example:

    Java Code:
    import java.util.Arrays;
    
    public class Test {
    	public static void main (String [] args){
    
    		int[] array = new int[]{1, 2, 3};
    
    		bubbleSort(array);
    
    		System.out.println(Arrays.toString(array));
    
    	}
    
    
    	public static int[] bubbleSort (int [ ] myArray) {
    		for (int x=0;x<myArray.length;x++){
    			for (int y=0;y<myArray.length;y++){
    				
    				System.out.println(Arrays.toString(myArray));
    				
    				if (myArray[x]<myArray[y]){
    					
    					System.out.println("Swapping " + myArray[x] + " and " + myArray[y]);
    					
    					int holding=myArray[y];
    					myArray[y]=myArray[x];
    					myArray[x]=holding;
    				}}
    		}
    		return myArray;
    	}
    }
    Which generates the following output:

    [1, 2, 3]
    [1, 2, 3]
    Swapping 1 and 2
    [2, 1, 3]
    Swapping 2 and 3
    [3, 1, 2]
    Swapping 1 and 3
    [1, 3, 2]
    [1, 3, 2]
    [1, 3, 2]
    [1, 3, 2]
    Swapping 2 and 3
    [1, 2, 3]
    [1, 2, 3]

    ...what? It does indeed sort, but you go through some very incorrect middle states. I'm pretty surprised this works at all.

    Edit: Even with {1, 2} being the input array, it still does this:

    [1, 2]
    [1, 2]
    Swapping 1 and 2
    [2, 1]
    Swapping 1 and 2
    [1, 2]
    [1, 2]

    I don't know what this is, but it's not bubble sort.
    Last edited by KevinWorkman; 07-09-2014 at 08:47 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,990
    Rep Power
    5

    Default Re: Bubble sort array unsorted

    huh? The OP is long gone, its just forum regulars keeping this thread alive at this point :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    jProtagonist is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Bubble sort array unsorted

    Whoops, sorry, so, can someone teach (or at least point me to a link) to where I can find an efficient bubble sort?

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by jProtagonist View Post
    Whoops, sorry, so, can someone teach (or at least point me to a link) to where I can find an efficient bubble sort?
    There is no efficient bubble sort and your way of sorting is even less efficient (it does sort though, although I claimed it didn't); read one of my blog articles (including code!) on a generic heap sort; it used (n*log(n)) steps while your method uses O(n^2) steps.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by jProtagonist View Post
    Whoops, sorry, so, can someone teach (or at least point me to a link) to where I can find an efficient bubble sort?
    I assume it's your homework to implement bubble sort, in which case I would recommend consulting your textbook and your class notes.

    If you still want more information, google is your friend. Here is the first result for googling "bubble sort": Bubble sort - Wikipedia, the free encyclopedia
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    jProtagonist is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Bubble sort array unsorted

    Quote Originally Posted by KevinWorkman View Post
    I assume it's your homework to implement bubble sort, in which case I would recommend consulting your textbook and your class notes.

    If you still want more information, google is your friend. Here is the first result for googling "bubble sort": Bubble sort - Wikipedia, the free encyclopedia
    Thanks! I actually am teaching myself at home, while I still have time during the summer, so I don't really have to go over bubble sort, but I think it's better to know. Upon reading the wikipedia article, I now know that bubble sort isn't the best solution for sorting, so I will focus my energy on more efficient sorting algorithms.

    Thank you all! :)

  20. #20
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Bubble sort array unsorted

    You know, Java has its own sorting algorithms that you can just use. You might want to look into custom comparators instead of the nitty gritty of sorting algorithms. But it's up to you, some people are interested in the lower-level stuff.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 1 of 2 12 LastLast

Similar Threads

  1. Bubble Sort
    By riyazm in forum New To Java
    Replies: 9
    Last Post: 03-04-2014, 02:49 PM
  2. Bubble Sort
    By Cruger in forum New To Java
    Replies: 2
    Last Post: 10-10-2013, 04:23 PM
  3. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Tags for this Thread

Posting Permissions

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