92
Analysis and Design of Algorithms Sorting Algorithms I

Algorithms Lecture 4: Sorting Algorithms I

Embed Size (px)

Citation preview

Page 1: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorting Algorithms I

Page 2: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorting Algorithms

Bubble Sort

Selection Sort

Insertion Sort

Page 3: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorting Algorithm is an algorithm made up of a series of instructions

that takes an array as input, and outputs a sorted array.

There are many sorting algorithms, such as:

Selection Sort, Bubble Sort, Insertion Sort, Merge Sort,

Heap Sort, QuickSort, Radix Sort, Counting Sort, Bucket

Sort, ShellSort, Comb Sort, Pigeonhole Sort, Cycle Sort

Page 4: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Bubble Sort

Page 5: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Bubble Sort is the simplest sorting algorithm

that works by repeatedly swapping the

adjacent elements if they are in wrong

order.

Page 6: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Algorithm:

Step1: Compare each pair of adjacent elements in the list

Step2: Swap two element if necessary

Step3: Repeat this process for all the elements until the

entire array is sorted

Page 7: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Example 1 Assume the following Array:

5 1 4 2

Page 8: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

First Iteration:

Compare

5 1 4 2

j

j+1

Page 9: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

First Iteration:

Swap

1 5 4 2

j

j+1

Page 10: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

First Iteration:

Compare

1 5 4 2

j

j+1

Page 11: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

First Iteration:

Swap

1 4 5 2

j

j+1

Page 12: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

First Iteration:

Compare

1 4 5 2

j

j+1

Page 13: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

First Iteration:

Swap

1 4 2 5

j

j+1

Page 14: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

1 4 2 5

Page 15: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Second Iteration:

Compare

1 4 2 5

j

j+1

Page 16: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Second Iteration:

Compare

1 4 2 5

j

j+1

Page 17: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Second Iteration:

Swap

1 2 4 5

j

j+1

Page 18: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

1 2 4 5

Page 19: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Third Iteration:

Compare

1 2 4 5

j

j+1

Page 20: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

1 2 4 5

Page 21: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Array is now sorted

1 2 3 4

Page 22: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

5 4 2 1 3

4 5 2 1 3

4 2 5 1 3

4 2 1 5 3

4 2 1 3 5

4 2 1 3 5

2 4 1 3 5

2 1 4 3 5

2 1 3 4 5

2 1 3 4 5

1 2 3 4 5

1 2 3 4 5

Example 2:

Page 23: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of bubble sort after the 1st iteration given the

following sequence of numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63

b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63

d) 2 4 9 13 18 45 37 63

Page 24: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of bubble sort after the 1st iteration given the

following sequence of numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63

b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63

d) 2 4 9 13 18 45 37 63

Page 25: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Python Code

Page 26: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Page 27: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Time Complexity: O(n2) as there are two nested loops

Example of worst case

5 4 3 2 1

Page 28: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Selection Sort

Page 29: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

The selection sort algorithm sorts an array by

repeatedly finding the minimum element

(considering ascending order) from unsorted

part and putting it at the beginning.

Page 30: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Algorithm:

Step1: Find the minimum value in the list

Step2: Swap it with the value in the current position

Step3: Repeat this process for all the elements until the

entire array is sorted

Page 31: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Example 1 Assume the following Array:

8 12 5 9 2

Page 32: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

8 12 5 9 2

i

j

min

Page 33: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

8 12 5 9 2

i

j

min

Page 34: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

8 12 5 9 2

j

i

min

Page 35: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

8 12 5 9 2

i

min

j

Page 36: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

8 12 5 9 2

i

min

j

Page 37: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

8 12 5 9 2

i

j

min

Page 38: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Smallest

8 12 5 9 2

i

min

Page 39: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Swap

8 12 5 9 2

min

i

Page 40: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorted

Un Sorted

2 12 5 9 8

Sorted

Un Sorted

Page 41: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

2 12 5 9 8

i

min

j

Sorted

Page 42: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

2 12 5 9 8

i

min

j

Sorted

Page 43: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

2 12 5 9 8

Sorted

i

j

min

Page 44: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

2 12 5 9 8

Sorted

i

j

min

Page 45: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Smallest

2 12 5 9 8

Sorted

i

min

Page 46: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Swap

2 12 5 9 8

Sorted

i

min

Page 47: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorted

Un Sorted

2 5 12 9 8

Sorted

Un Sorted

Page 48: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

2 5 12 9 8

Sorted

i

j

min

Page 49: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

2 5 12 9 8

Sorted

i

j

min

Page 50: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

2 5 12 9 8

Sorted

i

min

j

Page 51: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

2 5 12 9 8

Sorted

i

min

j

Page 52: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Smallest

2 5 12 9 8

Sorted

i

min

Page 53: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Swap

2 5 12 9 8

Sorted

i

min

Page 54: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorted

Un Sorted

2 5 8 9 12

Sorted

Un Sorted

Page 55: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

2 5 8 9 12

Sorted

i

min

j

Page 56: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorted

Un Sorted

2 5 8 9 12

Sorted

Un Sorted

Page 57: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Sorted

Un Sorted

2 5 8 9 12

Sorted

i

min

Page 58: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Array is now sorted

2 5 8 9 12

Sorted

Page 59: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

12 10 16 11 9 7 Example 2:

7 10 16 11 9 12

7 9 16 11 10 12

7 9 10 11 16 12

7 9 10 11 16 12

7 9 10 11 12 16

12 10 16 11 9 7

Page 60: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of selection sort after the 2nd iteration given

the following sequence of numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63

b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63

d) 2 4 9 13 18 45 37 63

Page 61: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of selection sort after the 2nd iteration given

the following sequence of numbers: 13 2 9 4 18 45 37 63

a) 2 4 9 13 18 37 45 63

b) 2 9 4 13 18 37 45 63

c) 13 2 4 9 18 45 37 63

d) 2 4 9 13 18 45 37 63

Page 62: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Python Code

Page 63: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Page 64: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Time Complexity: O(n2) as there are two nested loops

Example of worst case

2 3 4 5 1

Page 65: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Insertion Sort

Page 66: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Insertion sort is a simple sorting

algorithm that works the way we sort

playing cards in our hands.

Page 67: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Algorithm:

Step1: Compare each pair of adjacent elements in the list

Step2: Insert element into the sorted list, until it occupies correct

position.

Step3: Swap two element if necessary

Step4: Repeat this process for all the elements until the entire

array is sorted

Page 68: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Assume the following Array:

5 1 4 2

Page 69: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

Store=

5 1 4 2

i

j

j+1

1

Page 70: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

Store=

5 4 2

i

j

j+1

1

Page 71: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

Store=

1 5 4 2

i

j+1

Page 72: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

Store=

1 5 4 2

i

j

4

j+1

Page 73: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

Store=

1 5 2

i

j

4

j+1

Page 74: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

Store=

1 5 2

i

j

4

j+1

Page 75: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

Store=

1 4 5 2

i

j

j+1

Page 76: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

Store=

1 4 5 2

i

j

2

j+1

Page 77: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

Store=

1 4 5

i

j

2

j+1

Page 78: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

Store=

1 4 5

i

j

2

j+1

Page 79: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Move

Store=

1 4 5

i

j

2

j+1

Page 80: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

Store=

1 4 5

i

j

2

j+1

Page 81: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Compare

Store=

1 2 4 5

i

j

j+1

Page 82: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Array is now sorted

1 2 4 5

Page 83: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

5 1 8 3 9 2

Example 2:

1 5 8 3 9 2

1 5 8 3 9 2

1 3 5 8 9 2

1 3 5 8 9 2

1 2 3 5 8 9

5 1 8 3 9 2

Page 84: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of insertion sort after the 1st iteration given the

following sequence of numbers: 7 3 5 1 9 8 4 6

a) 3 7 5 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Page 85: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of insertion sort after the 1st iteration given the

following sequence of numbers: 7 3 5 1 9 8 4 6

a) 3 7 5 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Page 86: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of insertion sort after the 2nd iteration given the

following sequence of numbers: 7 3 5 1 9 8 4 6

a) 3 5 7 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Page 87: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

What is the output of insertion sort after the 2nd iteration given the

following sequence of numbers: 7 3 5 1 9 8 4 6

a) 3 5 7 1 9 8 4 6

b) 1 3 7 5 9 8 4 6

c) 3 4 1 5 6 8 7 9

d) 1 3 4 5 6 7 8 9

Page 88: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Python Code

Page 89: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Page 90: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

Time Complexity: O(n2)

Example of worst case

5 4 3 2 1

Page 91: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

facebook.com/mloey

[email protected]

twitter.com/mloey

linkedin.com/in/mloey

[email protected]

mloey.github.io

Page 92: Algorithms Lecture 4: Sorting Algorithms I

Analysis and Design of Algorithms

www.YourCompany.com© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.

THANKS FOR YOUR TIME