• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bubble Sort using 2 while loops  RSS feed

 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have successfully done a bubble sort, but apparently i am suppose to use only 2 while loops instead of two for loops and 1 while loop. Is this possible?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it is possible; as long as you avoid labelled continue, everything which can be done in a for loop can be done in a while loop.
Why do you have a while in line 3? Shouldn't that read if?
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That could be even done without any loops if you wanted to.
For example bubble sort written in recursive way.
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i am required to do a sequential and binary search after this bubble sort. When i run the code below, the sequential search works fine, but the binary search fails. Any thouights?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have verified that your sorting is correct?

Remove all that swapping code from there. That belongs in a method of its own, maybe swapTwoElements(something[], int, int). Where does last = count − 1 fit into all of that? You are setting count to −1 and probably never entering the following loop.
If you find the most dubious classification of methods known to modern science, those methods would be 1468 (or in some versions 1½468 where 1½ means you are altering the state of a parameter=“output parameter”). Since it ends with 68 that means both those methods could beneficially be marked static.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Westburry wrote:When i run the code below, the sequential search works fine, but the binary search fails. Any thouights?


In general, when binary search can't find an item in a sorted collection, I would check to confirm that how it is sorted, and how the search expect it to be sorted, are the same.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Westburry wrote:Well i am required to do a sequential and binary search after this bubble sort. When i run the code below, the sequential search works fine, but the binary search fails.
Don't mix these things up. These things are not much related to each other.

1 method. Bubble sort is Bubble sort and nothing else. Like independent standalone solution to sort numbers in ascending order in your case.
2 method. Sequential search just a simple linear search, which actually means looping thought an array starting from position 0 to n-1 [numbers no need to be sorted, so it is not related with Bubble sort algorithm at all].
3 method. In order to perform binary search, array has to be sorted [but again, it is not related particular with Bubble sort].

So, first things first, did you get Bubble sort working correctly? And it gives you a wanted results?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An hour ago, I wrote:. . . You are setting count to −1 and probably never entering the following loop. . . .
I should have said last there. And you are definitely not entering the following loop, so you are probably not sorting anything.
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would anybody be able to provide an example code using different data?
 
Taylor Hall
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that would be tremendously helpful, i too am a visual learner
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so here is my new code. Sequential search is still succesful but Binary search isnt... what should i change?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have got the headings wrong for those two swap methods. They should take the indices to swap. I haven't checked the rest of the code.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You have got the headings wrong for those two swap methods. They should take the indices to swap. I haven't checked the rest of the code.


Agreed. The swapElements() method seems to use ItemCount (to determine which element to swap), but nowhere is it set -- not even by the caller (the bubbleSort() method). Does this mean that it is the same two elements that is being checked and swapped... you can't really sort anything if you are only swapping the same two elements.

Anyway, I recommend printing out the elements *after* it has been sorted. I will speculate that it isn't sorted at all.

Henry
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what would that look like?
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code...still no luck, binary search still fails. please, somebody, anybody?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Westburry wrote:This is my code...still no luck, binary search still fails. please, somebody, anybody?


As Campbell already mentioned, your bubbleSort() method doesn't do anything. The outer while loop starts off as false, and the method simply returns without doing anything.


And BTW, you have other stuff wrong with the bubbleSort() method, which we have also mentioned in this topic -- the "doesn't do anything" issue is just the primary one at the moment.

Henry
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, suppose my array is: 12, 5, 3, 16
How would i use a bubble sort to display them in descending order?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!