decision maths 1 sorting algorithm shuttle sort a v ali : 1.compare items 1 and 2; swap them if...

Post on 04-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Decision Maths 1

Sorting AlgorithmShuttle Sort

A V Ali : www.2july-maths.co.uk

1. Compare items 1 and 2; swap them if necessary

2. Compare 2 and 3; swap if necessary; then 1 and 2 again;

3. Compare 3 and 4, then 2 and 3, then 1 and 2;

4. etc.

This is an adaption of the Bubble sort, it sorts the sub list fully for each pass.

5 7 312

95

Shuttle Sort

5 7 312

95 No sort neededPass 1

Pass 2

5 7 312

95

5 5 312

97 No sort needed

Sort needed

Pass 5

5 5 312

97

5 5 312

97 Sort needed

5 5 12

3 97

5 5 12

7 93

5 3 12

7 95

Sort needed

Sort needed

Sort needed

Pass 4 No sort needed

Pass 6

3 5 12

7 95 Sort needed

3 5 97 12

5 No sort needed

3 5 97 12

5 Sorted !!

top related