• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with understanding what bubble sort does?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I am trying to see if I understand bubble sort correctly, also there many gaps in my knowledge that I hope people can fill in.

I found this bubble sort code online and am now trying to analyze how it all works.



when we do this swap is, is temp = arr[i] at that moment an integer or the index or element itself, so confused about that. Don't understand what it represents right then and there.


Say we have the unsorted array: 9,5,2,7,1

here would that code look like:



I know it's suppose to be a swap, but I am just not seeing how that happens there.
we say our temp is now the value of 9.
It's strange to see a value on one side and then the variable name on the other.
Would it be the same to say temp = 5; at that point?
I am quite confused about arrays.

How exactly does this output a sorted array at the end of it all?

please help
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swap looks more like

At the end, v1 contains 5 and v2 contain 9.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Swap looks more like

At the end, v1 contains 5 and v2 contain 9.


Thanks!
So it's simply assignments happening here. What's on the left get's assigned to what's on the right, like that?
So we say that temp will hold on to the value that the array has at that specified index.
and then say that, that array index is to be assigned with the array index next to it? so even though they are both array[index] you can assign one to be WHERE it's at and the other to be the value?
so it's more like
array[0] = 5;
instead of array[0] = array[1]; ?

it confuses me to see an array assigned to another array, like what's even happening there. Is one array becoming another array? massive confusion about it.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the array at index 0 is assigned to the VALUE part, not the array itself, at index 1. Idk it really confuses me to think of one array being assigned to another array.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What the variable j is doing is limiting the number of elements that are checked for swapping.  The idea is that when you have made one pass, at least the last element (or first, depending on your point of view) is sorted, so why check it.  After the second pass, the last two are sorted, and so on.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:So the array at index 0 is assigned to the VALUE part, not the array itself, at index 1. Idk it really confuses me to think of one array being assigned to another array.

It's one array element being assigned to another, not the whole array.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to visualize it. Say you have two cups, one containing juice, one containing water.  If you want to swap the contents of these two cups, you can't just pour the juice from its cup into the cup that has water, right? You'd need a third cup to temporarily hold one of the liquids. Then it's just like a game of musical cups.

Same thing goes in Java when you need to swap the values held by two variables. You can think of each element of an array as a separate variable.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Swap looks more like . . .
Of course, you would write a utility class with a public static swap method in to do all the swapping. It will look just like what CB showed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!