Join TSR now and get all your revision questions answeredSign up now

quick question about sorting algorithms! Watch

    • Thread Starter
    Offline

    0
    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!
    • Study Helper
    Offline

    0
    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.
    • Thread Starter
    Offline

    0
    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!
    • Study Helper
    Offline

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

    In general 1+2+3+4+....+(n-2)+(n-1) = n(n-1)/2.
    • Thread Starter
    Offline

    0
    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!!
 
 
 
Poll
What is your favourite film genre?
Useful resources

Make your revision easier

Maths

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

Equations

How to use LaTex

Writing equations the easy way

Student revising

Study habits of A* students

Top tips from students who have already aced their exams

Study Planner

Create your own Study Planner

Never miss a deadline again

Polling station sign

Thinking about a maths degree?

Chat with other maths applicants

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

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