Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

Bubble sort

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

What would you like to know about it? It is one of the most basic sorting algorithms. Not the best, but not too bad for short lists.
can you give example? I have array [9, 2, 5, 4]
The basic logic behind bubble sort is that you start with the first element and "sweep" it through the array, swapping it with its neighbor if it is out of order. in your example it would be as follows:

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

[9, 2, 5, 4] <- here we choose 9 and compare it to 2. 9 > 2 so we swap them and continue [2, 9, 5, 4] <- now 9 > its neighbor, 5 so we swap [2, 5, 9, 4] <-one more time [2, 5, 4, 9] <- now that we've reached the end we start over at the element that directly follows the one we chose this previous time. in this case, since we started on the 1st element (then a 9), we start on the 2nd element in the array, 5. since 5 > 4, we swap. [2, 4, 5, 9] <- 5 is not > 9, so we don't swap here. you can see we are now sorted, but the algorithm may continue on, checking the 3rd element (5) with the 4th (9). since 5 is not > 9, we do not swap. we have then reached our final element. so we know for sure it is in sorted order.
thank you
Please fan me if I helped. :)
A good way to get an idea of how these functions work is to just build one based on the description. Here is one that I made as an example: # Bubble Sort def check(a): for n in range(1,len(a)): if a[n] < a[n-1]: return True return False def bubble(a): b = a[:] while check(b) == True: for n in range(1,len(b)): c = b[:] if b[n] < b[n-1]: b[n] = c[n-1] b[n-1] = c[n] return b

Not the answer you are looking for?

Search for more explanations.

Ask your own question