Results are out! Find what you need...fast. Get quick advice or join the chat
x

Unlock these great extras with your FREE membership

  • One-on-one advice about results day and Clearing
  • Free access to our personal statement wizard
  • Customise TSR to suit how you want to use it

quick question about sorting algorithms!

Announcements Posted on
Find your uni forum to get talking to other applicants, existing students and your future course-mates 27-07-2015
  1. Offline

    ReputationRep:
    hey!
    i'm doing a past paper and not sure how to work out this question:
    what is the maximum number of swaps needed to rearrange a list of 8 numbers into ascending order using a bubble sort?
    i know that the maximum would be when they are in reverse order, but not sure how i would quickly work out the answer, as they obviously don't want you to work it out.
    is there a pattern or something i should know?
    thanks!
  2. Offline

    ReputationRep:
    It takes n-1 swaps to take the largest/smallest to the end of the list. The second largest is now at the start and you need n-2 swaps to put it in its place. etc..

    Maximum number of swaps = 1+2+3+4+.....(n-2)+ (n-1).

    ....I think.
  3. Offline

    ReputationRep:
    (Original post by BabyMaths)
    It takes n-1 swaps to take the largest/smallest to the end of the list. The second largest is now at the start and you need n-2 swaps to put it in its place. etc..

    Maximum number of swaps = 1+2+3+4+.....(n-2)+ (n-1).

    ....I think.
    oh okay, that makes sense..but still not sure how i would get the answer to the question, which is apparently 28. if there's 8 numbers, using this n-1 thing there would be 7 swaps... i'm guessing you x4 to get 28 but no idea why
    thanks though!
  4. Offline

    ReputationRep:
    1+2+3+4+5+6+7=28

    In general 1+2+3+4+....+(n-2)+(n-1) = n(n-1)/2.
  5. Offline

    ReputationRep:
    (Original post by BabyMaths)
    1+2+3+4+5+6+7=28

    In general 1+2+3+4+....+(n-2)+(n-1) = n(n-1)/2.
    ohhhh! got it. thankyou!!

Reply

Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. By joining you agree to our Ts and Cs, privacy policy and site rules

  2. Slide to join now Processing…

Updated: May 18, 2012
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Poll
Have you ever posted a picture of your food on social media?
Results and Clearing

Results are coming...

No sweat. Here's all you need to make sure you're ready

new on tsr

Join the American Society

Whether you're from the USA or just love it!

Study resources
x

Think you'll be in clearing or adjustment?

Hear direct from unis that want to talk to you

Get email alerts for university course places that match your subjects and grades. Just let us know what you're studying.

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.