Beruflich Dokumente
Kultur Dokumente
Lesson 11 Sorting
Algorithms
Sorting Algorithms
Wiltshire
Wiltshire
1st Iteration
in to ascending order.
The first thing to do is to
compare the first two numbers
to see if they are the correct
way around.
In this case they are not so we
change them.
Now repeat the process on the
next pair of numbers, and so on.
The process (or iteration) stops
when all the pairs have been
compared.
The final line shows the State of
the order after the first iteration.
Wiltshire
Wiltshire
Wiltshire
Wiltshire
Wiltshire
Wiltshire
7th Iteration
1
Wiltshire
8 6 9 2 5
Use the bubble sort algorithm to put the
following numbers in descending order.
8 6 9 2 5
Wiltshire
Wiltshire
Wiltshire
Then the third smallest gets swapped with the third number.
The fourth smallest with the fourth number, and so on.
8
Wiltshire
On the last pass you can see that the list has already been sorted.
However, similar to previous algorithms all steps must be carried out to
Wiltshire
4 1 6 2 5 9
Use the Selection with Interchange
Sort Algorithm to put the following numbers in
descending order.
4 1 6 2 5 9
Wiltshire
Wiltshire
Wiltshire
Wiltshire
Wiltshire
lists.
Two of them are easily dealt
with because both lists are
single digits.
So these can be fixed in the
final list.
The 7 is now the pivot in the
last list.
Make it permanent.
The 6 moves across.
We can fix the 7 in the final
list.
Now all thats left is the 6 so
we can fix that too.
Wiltshire
4 1 6 2 5 9
Use the Selection with Quick Sort Algorithm
to put the following numbers in descending
order.
4 1 6 2 5 9
Wiltshire
Original1st Pass
list
2nd
Pass
3rd
Pass
Wiltshire
Step 4
2
8
7
4th
Pass
5th
Pass
(4th pass)
Compare the
4th and 3rd, 3rd
and 2nd, 1st and
2nd.
Step 5
(5th pass)
Compare the
5th and 4th, 4th
and 3rd, 3rd and
2nd, 1st and 2nd.
6
6th
Pass
Wiltshire
Step 6
(6th pass)
Compare the 6th
and 5th, 5th and 4th,
4th and 3rd, 3rd and
2nd, 2nd and 1st.
6
7th
Pass
Wiltshire
Step 7
(7th pass)
Compare the 7th
and 6th, 6th and
5th, 5th and 4th, 4th
and 3rd, 3rd and
2nd, 2nd and 1st.
Wiltshire
Step 8
1
9
8th
Pass
(8th pass)
Compare the 8th
and 7th, 7th and 6th,
6th and 5th, 5th and
4th, 4th and 3rd, 3rd
and 2nd, 2nd and
1st.
Hopefully you can
see that this
Algorithm will
always have (n-1)
iterations.
Wiltshire
3 8 6 8 1 9
Use the Selection with The Shuttle Sort
Algorithm to put the following numbers in
descending order.
3 8 6 8 1 9
Wiltshire
8
9
Wiltshire
iterations.
8
3
5
2
7
9
1
4
6
8
9
Wiltshire
4 1 6 2 5 9
Use the Selection with Insertion Sort
Algorithm to put the following numbers in
descending order.
4 1 6 2 5 9
Complexity
Bubble Sort
Selection with interchange
Quick Sort
Shuttle Sort
Insertion Sort
Wiltshire
Edexcel
Quick Sort Algorithm
Wiltshire
5
1