• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Help with understanding what bubble sort does?

 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10920
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79827
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic