A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
Bubble sort
anonymous
 5 years ago
Bubble sort

This Question is Closed

heisenberg
 5 years ago
Best ResponseYou've already chosen the best response.0What 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0can you give example? I have array [9, 2, 5, 4]

heisenberg
 5 years ago
Best ResponseYou've already chosen the best response.0The 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:

heisenberg
 5 years ago
Best ResponseYou've already chosen the best response.0[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.

heisenberg
 5 years ago
Best ResponseYou've already chosen the best response.0Please fan me if I helped. :)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0A 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[n1]: 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[n1]: b[n] = c[n1] b[n1] = c[n] return b
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.