Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

noah Group Title

Bubble sort

  • 3 years ago
  • 3 years ago

  • This Question is Closed
  1. heisenberg Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • 3 years ago
  2. noah Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    can you give example? I have array [9, 2, 5, 4]

    • 3 years ago
  3. heisenberg Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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:

    • 3 years ago
  4. heisenberg Group Title
    Best Response
    You've already chosen the best response.
    Medals 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.

    • 3 years ago
  5. noah Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    thank you

    • 3 years ago
  6. heisenberg Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Please fan me if I helped. :)

    • 3 years ago
  7. Wpaprocki Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 3 years ago
    • Attachments:

See more questions >>>

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.