aspose file tools*
The moose likes Beginning Java and the fly likes [SOLVED]Bubblesort - Incorrect Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[SOLVED]Bubblesort - Incorrect Sorting" Watch "[SOLVED]Bubblesort - Incorrect Sorting" New topic
Author

[SOLVED]Bubblesort - Incorrect Sorting

Korbinian Michl
Greenhorn

Joined: Sep 22, 2011
Posts: 4

Hi,

(First, sorry for my bad English.)

I need help. I programmed a simple bubblesort ... well, not very good. Unfortunately now I have the problem that I have an incorrect sorting. But what is wrong?



Can you help me please?

Thanks

grenzreiter
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Lines 22 to 26 say "If two array entries are equal, then interchange them."

To me that doesn't seem like it would ever do anything to the array, at least not anything you could see.
Korbinian Michl
Greenhorn

Joined: Sep 22, 2011
Posts: 4

Thank you, but it still doesn´t work.

Greeting
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

couple of things i see:

line 22: look VERY closely...you have something extra there that makes it not do what you think...

why are you not going all the way to the end in your sort? As written, you will be leaving the last element in place. You should see this when you print out your final array. you probably want your line 29 to be

why are you only swapping if they are equal? Shouldn't you swap if one is less than (or greater than, depending on which way you want to sort) the other?>


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Korbi Michl wrote:Thank you, but it still doesn´t work.

one thing to note...simply saying "It doesn't work" is really useless. We have no idea HOW it doesn't work. it may
a) not compile
b) not run
c) start to run then die
d) run and produce bad results

The more details you give us, the more likely and faster you will get help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Welcome to the Ranch
I recommend you search for utility class, and you will find this. Now create yourself a utility class like that, and give it a swapTwoElementsInArray() method. You pass the array and the indices of the two values, then you can put the swapping code in that method, and it is available for re-use.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Why have you altered your original post? That makes the replies look like nonsense
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Campbell Ritchie wrote:Why have you altered your original post? That makes the replies look like nonsense


Agreed. I won't be following this thread any more.
Arka Sharma
Ranch Hand

Joined: Jun 15, 2011
Posts: 103
Check this line out
for(int i1=0; i1 > (aArray.length)-2; i1++){


at the line 21.

And this one also

if(aArray[i1] < aArray[i1+1]);


Regards,
Arka
Korbinian Michl
Greenhorn

Joined: Sep 22, 2011
Posts: 4

Hi,

now it works



Greetings
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Arka Sharma, thank you for quoting a line which was changed later ( ). That has enabled me to restore the original post by reversing (at least some of) the changes which were made to it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [SOLVED]Bubblesort - Incorrect Sorting