Revision:Sorting Algorithms - The Student Room
The Student Room

Revision:Sorting Algorithms

TSR Wiki > Study Help > Subjects and Revision > Revision Notes > Mathematics > Sorting Algorithms



Contents

Bubble Sort

  1. If there is only one item in the list, stop.
  2. Make one pass through the list: compare the first and second items in the list and swap if the second is smaller than the first, compare the seconds and third items in the list and swap if the third is smaller than the second etc. etc.
  3. If no swaps have occurred, stop. Otherwise make the last item permanent and form a new shorter list with the last item removed. Return to step 1 to do another pass.

You may be required to count the number of comparisons and swaps you do in each pass.

Here is an example:


Shuttle Sort

  • First pass: Compare first and second items and swap if necessary.
  • Second pass: Compare second and third items and swap if necessary. If a swap has occurred, compare the first and second items and swap if necessary.
  • Third pass: Compare the third and fourth items and swap if necessary. If a swap has occurred, compare the second and third and swap if necessary. If a further swap has occurred here, compare the first and second again and swap if necessary.
  • Repeat through the whole list.

The effect of pass n is to pick up item in position n + 1 and insert it in the correct place in the already sorted sublist of the first n items.

Shuffle sort cannot be terminated early. The number of passes is always one less than the length of the original list.


Quick Sort

  • First, the median is selected as a pivot, by calculating (n+1)/2, where n is the number of items in the initial list. The median is then designated in some way, by circling it, for instance.
  • A single pass is then done through the list, putting everything with a value < median before the median, and everything with value > median after it. The items should not move other than away from items above/below the median (whichever they are not).
  • The median should then stick still, and it's designation be changed to something else to avoid confusion (perhaps, a square).
  • A new median should now be found for the list of items each side of the original median. The above steps should be followed for each side.
  • This process should be repeated until each item has been the median, and the list will be sorted when each item has a square around it (or whatever designation you have chosen).


Example

Sort: 2,6,3,9,5,7,8,4 using the Quick Sort algorithm.

(Italics denotes a new pivot, and bold denotes an 'old' (now fixed) pivot).

  1. 2,6,3,9,5,7,8,4
  2. 2,3,4,5,6,9,7,8
  3. 2,3,4,5,6,7,9,8
  4. 2,3,4,5,6,7,8,9
  5. 2,3,4,5,6,7,8,9

Efficiency

We usually measure the efficiency of an algorithm by considering the worst case scenario. For both bubble sort and shuffle sort, this is when the list is in reverse order.

In this case, both algorithms use 1 + 2 + 3 + ... + (n-1) comparisons and swaps.

Both algorithms are quadratic order because the measure of efficiency is a quadratic function of the size of the list.

1 + 2 + 3 + ... + (n-1) = \frac{n^2}{2} - \frac{n}{2}

For a very large problem, the quadratic term would dominate and the run time would be approximately kn2 for some constant k. t \propto n^2

Also See

See the other D1 notes:

  1. Algorithms
  2. Sorting algorithms
  3. Packing algorithms
  4. Graphs and networks
  5. Minimum connector problems
  6. The shortest path
  7. Route inspection
  8. The travelling salesman problem
  9. Linear programming
  10. The simplex algorithm

Comments

Discussions Toggle
Books to read before starting LLB
started by: Blackcosmo
forum: Law
replies: 7
last post: 2 Minutes Ago
Flat mate thinks I'm gay. why?
started by: bestofyou
forum: Advice on Everyday Issues
replies: 19
last post: 2 Minutes Ago
Accounting and Finance applicants for 2012 entry
started by: Choppyy
forum: Accounting and Finance
replies: 1164
last post: 3 Minutes Ago
Cat Lovers Society! =^_^=
started by: Princess_Peach
forum: Animals and Pets
replies: 709
last post: 3 Minutes Ago
How eclectic is your music?
started by: JCC-MGS
forum: Music
replies: 18
last post: 3 Minutes Ago
The African Society Part VII
started by: The Cornerstone
forum: International Lounge
replies: 7102
last post: 4 Minutes Ago
Where to purchase a compass?
started by: PrinceUpsb
forum: Advice on Everyday Issues
replies: 11
last post: 5 Minutes Ago
Should there be legislation imposing a quota on women being on boards of companies?
started by: Herr
forum: UK Politics
replies: 25
last post: 5 Minutes Ago
What do you think of these glasses?
started by: Mr Bee
forum: Fashion and Beauty
replies: 5
last post: 6 Minutes Ago
Cannibalization of Nigerian Muslims
started by: xXxiKillxXx
forum: International
replies: 68
last post: 6 Minutes Ago
Lets face it, life as a trainee will suck.
started by: mirin?
forum: Law
replies: 6
last post: 6 Minutes Ago
Poem: what does this piece mean? x
started by: Flint_09
forum: English
replies: 0
last post: 6 Minutes Ago
6th form messing up my eating habits.
started by: Comeheretellme
forum: Advice on Everyday Issues
replies: 12
last post: 6 Minutes Ago
interpret this poem plz!!
started by: Flint_09
forum: Books, Literature & Poetry
replies: 0
last post: 8 Minutes Ago
Accommodation Questions
started by: Connor_Phillips
forum: Student Accommodation
replies: 2
last post: 8 Minutes Ago
Most annoying thing people do at University?
started by: The Phelps
forum: Student Life
replies: 242
last post: 10 Minutes Ago
Charity shops anyone?
started by: Converse Rocker
forum: Fashion and Beauty
replies: 15
last post: 11 Minutes Ago
What to wear? :)
started by: amy19
forum: Fashion and Beauty
replies: 6
last post: 11 Minutes Ago
FA say Capello resigns
started by: lazy smurf
forum: Football
replies: 517
last post: 11 Minutes Ago
Housemate is denying he told me he cheated
started by: Anonymous
forum: Friends, Family and Work
replies: 27
last post: 11 Minutes Ago
Article Updates Toggle
Contact Us | Site Rules | Staying Safe on TSR | Advertising | Staff Blog | Essays & Coursework | Terms & Conditions | Top
Customise your TSR | Life Advice | Hobbies and Interests | Debate and Current Affairs | Study Help | University and University courses
Universities and HE Colleges | Careers, Employment and Gap Years | General Discussion

Customise your TSR