quick question about sorting algorithms!

Maths and statistics discussion, revision, exam and homework help.

Announcements Posted on
Please change your TSR password 23-05-2013
IMPORTANT: You must wait until midnight (morning exams)/4.30AM (afternoon exams) to discuss Edexcel exams and until 1pm/6pm the following day for STEP and IB exams. Please read before posting, including for rules for practical and oral exams. 28-04-2013
Sign in to Reply
  1. georgiaaaxo's Avatar
    • Adored and Respected Member
    • Location: Everywhere
    • Posts: 585
    quick question about sorting algorithms!
    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. BabyMaths's Avatar
    • Peer Of The TSR Realm
    • Posts: 1,576
    Re: quick question about sorting algorithms!
    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. georgiaaaxo's Avatar
    • Adored and Respected Member
    • Location: Everywhere
    • Posts: 585
    Re: quick question about sorting algorithms!
    (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. BabyMaths's Avatar
    • Peer Of The TSR Realm
    • Posts: 1,576
    Re: quick question about sorting algorithms!
    1+2+3+4+5+6+7=28

    In general 1+2+3+4+....+(n-2)+(n-1) = n(n-1)/2.
  5. georgiaaaxo's Avatar
    • Adored and Respected Member
    • Location: Everywhere
    • Posts: 585
    Re: quick question about sorting algorithms!
    (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!!
Sign in to Reply
Share this discussion:  
Article updates
Moderators

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

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.