Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[SOLVED]Bubblesort - Incorrect Sorting

 
Armin Smith
Greenhorn
Posts: 4
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20756
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Armin Smith
Greenhorn
Posts: 4
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, but it still doesn´t work.

Greeting
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?>
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you altered your original post? That makes the replies look like nonsense
 
Paul Clapham
Sheriff
Pie
Posts: 20756
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Armin Smith
Greenhorn
Posts: 4
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

now it works



Greetings
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic