Algorithms & Data Structure I - Lecture 5
Algorithms & Data Structure I - Lecture 5
1
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Overview
The Sorting Problem
Bubble Sort
Example of Bubble sort
Pseudo Code for Bubble sort
Bubble sort Implementation
Analysis of Bubble sort
Selection Sort
Example of Selection sort
Pseudo Code for Selection sort
Selection sort Implementation
Insertion Sort
Example of Insertion sort
Insertion sort Implementation
Analysis of Insertion sort
Summary
2
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
4
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Merge sort: recursively divide the array in half and sort it.
5
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
6
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
7
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Bubble Sort
Bubble sort is a simple sorting algorithm. This
sorting algorithm is comparison-based algorithm in
which each pair of adjacent elements is compared
and the elements are swapped if they are not in
order.
This algorithm is not suitable for large data sets as
its average and worst case complexity are of Ο(n2)
where n is the number of items.
10
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Bubble Sort
Compare each element (except the last one) with its
neighbor to the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last two) with its
neighbor to the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final places
Compare each element (except the last three) with its
neighbor to the right
Continue as above until you have no unsorted elements on the left
11
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
12
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
13
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Selection Sort
Selection sort is a simple sorting algorithm. This
sorting algorithm is an in-place comparison-based
algorithm
The smallest element is selected from the unsorted
array and swapped with the leftmost element, and
that element becomes a part of the sorted array.
This process continues moving unsorted array
boundary by one element to the right.
This algorithm is not suitable for large data sets as
its average and worst case complexities are of
Ο(n2), where n is the number of items.
16
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Selection Sort
Given an array of length n,
Search elements 0 through n-1 and select the smallest
Swap it with the element in location 0
Search elements 1 through n-1 and select the smallest
Swap it with the element in location 1
Search elements 2 through n-1 and select the smallest
Swap it with the element in location 2
Search elements 3 through n-1 and select the smallest
Swap it with the element in location 3
Continue in this fashion until there’s nothing left to
search.
17
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
7 2 8 5 4
2 7 8 5 4
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
18
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
19
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Insertion Sort
This is an in-place comparison-based sorting
algorithm.
The array is partitioned into two parts, sorted (on
the left) and unsorted (on the right).
sorted
22
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
23
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
24
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Summary
Most of the sorting techniques we have discussed are
O(n2)
We can do much better than this with somewhat more
complicated sorting algorithms
Within O(n2),
Bubble sort is very slow, and should probably never be used for
anything
Selection sort is intermediate in speed
Insertion sort is usually faster than selection sort. In fact, for small
arrays (say, 10 or 20 elements), insertion sort is faster than more
complicated sorting algorithms
Selection sort and insertion sort are “good enough” for small
arrays
25
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Exercise
26