Chapter 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Bubble Sort, Selection Sort,

Insertion and heap Sort

MARLINA BINTI MOHAMAD


BBP 25203
Learning Outcomes

 In this session, you will:


 Explore how to sort an array using the bubble sort, selection
sort, heap sort and insertion sort algorithms
Sorting : Bubble Sort

 Suppose list[0]...list[n - 1] is a list of n


elements, indexed 0 to n – 1
 In a series of n - 1 iterations, compare successive
elements, list[index] and list[index + 1]
 If list[index] is greater than list[index +
1], then swap them
 It follows that the smaller elements move toward the
top (beginning), and the larger elements move
toward the bottom (end) of the list
Sorting : Bubble Sort

 First iteration: list[0]…list [n-1]


 Second iteration: list [0]…list [n-2]
 Third iteration: list[0]… list [n-3]
 ..so on

 Consider list[0]…list[4],as show in the next


slide
Sorting : Bubble Sort
Sorting : Bubble Sort
Sorting : Bubble Sort
Sorting : Bubble Sort
Sorting : Bubble Sort
Example: Bubble sort
Example: Bubble sort (cont..)
Sorting : Selection Sort

 Rearrange list by selecting an element and moving it


to its proper position
 Find the smallest (or largest) element and move it to
the beginning (end) of the list
1. Locate the smallest item in the entire list
2. Locate the smallest item in the list starting from the second
element in the list, and so on
Sorting : Selection Sort
Sorting : Selection Sort
Sorting : Selection Sort
Sorting : Selection Sort
Example: Selection Sort
Example: Selection Sort
Sorting : Selection Sort

• For a list length n, selection sort makes exactly


𝑛(𝑛−1)
key comparisons and 3 𝑛 − 1 item
2
assignments

• Therefore, if n=1000, then to sort the list, selection


sort makes about 500,000 key comparisons and
about 3000 item assignments
Sorting : Insertion Sort

 Previously, for a list length 1000, both bubble sort


and selection sort make approximately 500,000 key
comparison, which is quite high
 Insertion sort, tries to improve, that is, reduce the
number of key comparisons
 The insertion sort algorithm sorts the list by moving
each element to its proper place.
Sorting : Insertion Sort
Sorting : Insertion Sort
Sorting : Insertion Sort
Sorting : Insertion Sort
Sorting : Insertion Sort
Sorting : Insertion Sort
Sorting : Insertion Sort
Example: Insertion Sort
Example: Insertion Sort
Sorting : Selection Sort

• For a list length n, on average, insertion sort makes


𝑛2+3𝑛−4 𝑛(𝑛−1)
about key comparisons and about
4 4
item assignments

• Therefore, if n=1000, then to sort the list, insertion


sort makes about 250,000 key comparisons and
about 250,000 item assignments
Sorting: Heap Sort

 A heap is a list in which element contains a key, such


that the key in the element at position k in the list is
at least as large as the key in the element at position
2k+1 (it if exist) and 2k+2 (if t exist)
 Recall that, in C++ the array index starts at 0.
Therefore, the element at position k is in fact the
k+1th element of the list
Sorting: Heap Sort
Sorting: Heap Sort
Sorting: Heap Sort
Sorting: Heap Sort – Build Heap

 The general algorithm:


 Suppose length denotes the length of the list

 Let index = length/2-1

 list[index] is the last element in the list which is not a


leaf, that is, this element has at least one child
 Thus, elements list[index+1] …. list[length-1] are
leaves
Sorting: Heap Sort – Build Heap

 First, convert the subtree with the root node


list[index] into a heap
 Suppose that list[a] is the root node of the subtree,
list[b] is the left child, and list[c], if it exist, is the right
child of list[a]
 Compare list[b] with list[c] to determine the larger
child
 If list[c] does not exist, then list[b] is the larger child
 Suppose that largerIndex indicates the larger child (notice
that, largerIndex is either b or c)
Sorting: Heap Sort – Build Heap

 Compare list[a] with list[largerIndex]


 If list[a] < list[largerIndex], then swap list[a]
with [largerIndex];
 Otherwise, the subtree with root node list[a] is already in a
heap
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort – Build Heap

 Now list[4]=56. the children of list[4] are


list[4*2+1] and list[4*2+2], that is, list[9]
and list[10]
 To convert the tree with the root node list[4],
perform the previous 3 steps:
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort – Build Heap
Sorting: Heap Sort

 How to do heap sort?...watch the video…


Sorting summary

 Previously you have presented with three sorting


algorithm
 Why so many different sorting algorithm? The
performance of each sorting algorithm is different
 Some make more comparisons, whereas others make
fewer item assignments
 There are also algorithm that make fewer
comparisons as well as fewer item assignments
 Analysis of the number of key comparisons and item
assignments allows the user to decide which
algorithm to use in a particular situation

You might also like