0% found this document useful (0 votes)
91 views21 pages

Bubble&Quick Sort

The document describes the bubble sort and selection with interchange sorting algorithms. It provides examples of sorting the numbers 8, 3, 5, 2, 7, 9, 1, 4, 6 using each algorithm. For bubble sort, it shows the list after each iteration, with the numbers being sorted into ascending order. For selection with interchange, it explains how the algorithm works by finding the smallest number and swapping it into the first position on each pass.

Uploaded by

Asyikin99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views21 pages

Bubble&Quick Sort

The document describes the bubble sort and selection with interchange sorting algorithms. It provides examples of sorting the numbers 8, 3, 5, 2, 7, 9, 1, 4, 6 using each algorithm. For bubble sort, it shows the list after each iteration, with the numbers being sorted into ascending order. For selection with interchange, it explains how the algorithm works by finding the smallest number and swapping it into the first position on each pass.

Uploaded by

Asyikin99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Sorting Algorithms Wiltshire

 Something as simple as sorting a list of numbers


needs an algorithm on a computer.
 Look at the list of numbers below;
8, 3, 5, 2, 7, 9, 1, 4, 6
 Clearly it is easy to see how the numbers would go
from smallest to biggest.
 However a computer, does not have the ability to do
this as simply as we can.
 It would use a sorting algorithm; we are going to look
at the following algorithms to sort these numbers:
 Bubble Sort Algorithm
 Selection with interchange Sort Algorithm
 Quick Sort Algorithm
 Shuttle Sort Algorithm
 Insertion Sort Algorithm
Bubble Sort Algorithm Wiltshire

 1st Iteration  We are going to sort out this list


in to ascending order.
8 3 5 2 7 9 1 4 6  The first thing to do is to
compare the first two numbers
3 8 5 2 7 9 1 4 6 to see if they are the correct
3 5 8 2 7 9 1 4 6 way around.
 In this case they are not so we
3 5 2 8 7 9 1 4 6 change them.
3 5 2 7 8 9 1 4 6  Now repeat the process on the
next pair of numbers, and so on.
3 5 2 7 8 9 1 4 6
 The process (or iteration) stops
3 5 2 7 8 1 9 4 6 when all the pairs have been
3 5 2 7 8 1 4 9 6 compared.
 The final line shows the State of
3 5 2 7 8 1 4 6 9 the order after the first iteration.
Bubble Sort Algorithm Wiltshire

 2nd Iteration  The process now


begins again starting
3 5 2 7 8 1 4 6 9
with the new state of
3 5 2 7 8 1 4 6 9 the line.
3 2 5 7 8 1 4 6 9  It is important to show
3 2 5 7 8 1 4 6 9 all of your working and
3 2 5 7 8 1 4 6 9 do not skip or assume
3 2 5 7 1 8 4 6 9 any steps.
3 2 5 7 1 4 8 6 9  Remember to show the
3 2 5 7 1 4 6 8 9
state of the line at the
end of each iteration.
3 2 5 7 1 4 6 8 9
Bubble Sort Algorithm Wiltshire

 3rd Iteration  The process now


begins again starting
3 2 5 7 1 4 6 8 9
with the new state of
2 3 5 7 1 4 6 8 9 the line.
2 3 5 7 1 4 6 8 9  It is important to show
2 3 5 7 1 4 6 8 9 all of your working and
2 3 5 1 7 4 6 8 9 do not skip or assume
2 3 5 1 4 7 6 8 9 any steps.
2 3 5 1 4 6 7 8 9  Remember to show the
2 3 5 1 4 6 7 8 9
state of the line at the
end of each iteration.
2 3 5 1 4 6 7 8 9
Bubble Sort Algorithm Wiltshire

 4th Iteration  The process now


begins again starting
2 3 5 1 4 6 7 8 9
with the new state of
2 3 5 1 4 6 7 8 9 the line.
2 3 5 1 4 6 7 8 9  It is important to show
2 3 1 5 4 6 7 8 9 all of your working and
2 3 1 4 5 6 7 8 9 do not skip or assume
2 3 1 4 5 6 7 8 9 any steps.
2 3 1 4 5 6 7 8 9  Remember to show the
2 3 1 4 5 6 7 8 9
state of the line at the
end of each iteration.
2 3 1 4 5 6 7 8 9
Bubble Sort Algorithm Wiltshire

 5th Iteration  The process now


begins again starting
2 3 1 4 5 6 7 8 9
with the new state of
2 3 1 4 5 6 7 8 9 the line.
2 1 3 4 5 6 7 8 9  It is important to show
2 1 3 4 5 6 7 8 9 all of your working and
2 1 3 4 5 6 7 8 9 do not skip or assume
2 1 3 4 5 6 7 8 9 any steps.
2 1 3 4 5 6 7 8 9  Remember to show the
2 1 3 4 5 6 7 8 9
state of the line at the
end of each iteration.
2 1 3 4 5 6 7 8 9
Bubble Sort Algorithm Wiltshire

 6th Iteration  The process now


begins again starting
2 1 3 4 5 6 7 8 9
with the new state of
1 2 3 4 5 6 7 8 9 the line.
1 2 3 4 5 6 7 8 9  It is important to show
1 2 3 4 5 6 7 8 9 all of your working and
1 2 3 4 5 6 7 8 9 do not skip or assume
1 2 3 4 5 6 7 8 9 any steps.
1 2 3 4 5 6 7 8 9  Remember to show the
1 2 3 4 5 6 7 8 9
state of the line at the
end of each iteration.
1 2 3 4 5 6 7 8 9
Bubble Sort Algorithm Wiltshire

 7th Iteration  You will have seen from the last


iteration that the final state of
the line was in the correct order.
1 2 3 4 5 6 7 8 9  You might think that this means
1 2 3 4 5 6 7 8 9 you can stop the algorithm.
 Remember that an algorithm is
1 2 3 4 5 6 7 8 9 a set or list of instructions that
solve a particular problem.
1 2 3 4 5 6 7 8 9
 There is no intelligence within
1 2 3 4 5 6 7 8 9 the algorithm to tell a computer
that the correct answer has
1 2 3 4 5 6 7 8 9 been reached.
1 2 3 4 5 6 7 8 9  So you must run through the list
one last time.
1 2 3 4 5 6 7 8 9  The fact that no changes are
made will tell the operator that
1 2 3 4 5 6 7 8 9 the list must be in the correct
order.
Bubble Sort Algorithm Wiltshire

 Now see if you can complete the Algorithm


with the questions below.
 Use the bubble sort algorithm to put the
following numbers in ascending order.
 8 6 9 2 5
 Use the bubble sort algorithm to put the
following numbers in descending order.
 8 6 9 2 5
Selection with Interchange
Sort Algorithm Wiltshire

 In this Algorithm the smallest number is


located and changed with the first.
8 1
3 3
5 5
2 2
7 7
9 9
1 8
4 4
6 6
Selection with Interchange
Sort Algorithm Wiltshire

 The smallest number is now in the correct place, so you


locate the second smallest and change it with the second
number.
8 1 1
3 3 2
5 5 5
2 2 3
7 7 7
9 9 9
1 8 8
4 4 4
6 6 6
Selection with Interchange
Sort Algorithm Wiltshire

 Then the third smallest gets swapped with the third number.
 The fourth smallest with the fourth number, and so on.

8 1 1 1 1 1 1 1
3 3 2 2 2 2 2 2
5 5 5 3 3 3 3 3
2 2 3 5 4 4 4 4
7 7 7 7 7 5 5 5
9 9 9 9 9 9 6 6
1 8 8 8 8 8 8 7
4 4 4 4 5 7 7 8
6 6 6 6 6 6 9 9
Selection with Interchange
Sort Algorithm 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
ensure the process works correctly.

8 1 1 1 1 1 1 1 1
3 3 2 2 2 2 2 2 2
5 5 5 3 3 3 3 3 3
2 2 3 5 4 4 4 4 4
7 7 7 7 7 5 5 5 5
9 9 9 9 9 9 6 6 6
1 8 8 8 8 8 8 7 7
4 4 4 4 5 7 7 8 8
6 6 6 6 6 6 9 9 9
Selection with Interchange
Sort Algorithm Wiltshire

 Now see if you can complete the Algorithm


with the questions below.
 Use the Selection with Interchange
Sort Algorithm to put the following numbers in
ascending order.
 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
Quick Sort Algorithm Wiltshire

 The quick sort Algorithm is


based on a pivot number
8 3 5 2 7 9 1 4 6 and sub-lists.
 The way of obtaining a pivot
number can vary. In the
Edexcel course they use a
middle number (see very last
slide).
 For this course we are going
to use the first number in the
list.
 Starting with your un-ordered
list pick the pivot number.
 Here the pivot is the first
number in the list, 8.
Quick Sort Algorithm Wiltshire

 Now the pivot number is


made permanent by placing
8 3 5 2 7 9 1 4 6
everything bigger than it to
3 5 2 7 1 4 6 8 9 the right and everything
smaller to the left.
 This number is definitely in
the correct place so it can be
fixed.
 Notice how the order of the
numbers has not changed.
 You now have two sub-lists
either side of 8.
8  Repeat the process of
picking a pivot and sorting
the list on each sub-list
Quick Sort Algorithm Wiltshire

 The pivot for the left list is 3.


 Fix the 3 as a permanent
8 3 5 2 7 9 1 4 6
number and put everything
3 5 2 7 1 4 6 8 9 bigger to the right and
smaller to the left.
2 1 3 5 7 4 6 9
 Remembering not to change
the order of the numbers.
 Once again we are certain
that the 3 is in the correct
place so we can fix its
position.
 The pivot for the right list is
the 9.
3 8 9  As this is the only number in
the sub-list then it to can be
made fixed.
Quick Sort Algorithm Wiltshire

 We now have two sub-lists


either side of the 3.
8 3 5 2 7 9 1 4 6
 For the left list the pivot is 2.
3 5 2 7 1 4 6 8 9
 Make it permanent.
2 1 3 5 7 4 6 9  Arrange smaller numbers left
1 2 4 5 7 6 and bigger numbers right.
 Fix 2 in final list.
 For the right list the pivot is 5.
 Make it permanent.
 Arrange smaller numbers left
and bigger numbers right.
2 3 5 8 9  Fix 5 in final list.
Quick Sort Algorithm Wiltshire

 There are now three sub-


lists.
8 3 5 2 7 9 1 4 6
 Two of them are easily dealt
3 5 2 7 1 4 6 8 9 with because both lists are
single digits.
2 1 3 5 7 4 6 9
 So these can be fixed in the
1 2 4 5 7 6 final list.
1 4 6 7  The 7 is now the pivot in the
last list.
6  Make it permanent.
 The 6 moves across.
 We can fix the 7 in the final
list.
1 2 3 4 5 6 7 8 9
 Now all that’s left is the 6 so
we can fix that too.
Quick Sort Algorithm Wiltshire

 Now see if you can complete the Algorithm


with the questions below.
 Use the Selection with Quick Sort Algorithm
to put the following numbers in ascending
order.
 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
Quick Sort Algorithm Wiltshire

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

1 2 3 4 5 6 7 8 9

You might also like