algorithms lecture 5: sorting algorithms ii

115
Analysis and Design of Algorithms Sorting Algorithms II

Upload: httpwwwbuedueg

Post on 23-Jan-2018

337 views

Category:

Education


13 download

TRANSCRIPT

Page 1: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sorting Algorithms II

Page 2: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Counting Sort

Radix Sort

Merge Sort

Page 3: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Counting Sort

Page 4: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Counting sort is a sorting technique based on keys

between a specific range. It works by counting the

number of objects having distinct key values (kind of

hashing). Then doing some arithmetic to calculate

the position of each object in the output sequence.

Page 5: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Algorithm:

Step1: Create a count array to store the count of each unique

object

Step2 : Modify count array by adding the previous number.

Step3 : Create output array by decrease count array

Page 6: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Example 1 Assume the following Array in range of 0 to 5:

1 4 3 2 3 5 2

Page 7: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

Page 8: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 0 0 0 0

Page 9: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 0 0 0 0

Page 10: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 0 0 0 0

Page 11: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0

Page 12: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0

Page 13: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0

Page 14: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 0 0

Page 15: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 1 0

Page 16: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 1 0

Page 17: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 0 1 0

Page 18: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 1 1 0

Page 19: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 1 1 0

Page 20: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 0 1 1 0

Page 21: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 1 1 0

Page 22: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 1 1 0

Page 23: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 0

Page 24: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 0

Page 25: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 0

Page 26: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 1

Page 27: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 1

Page 28: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 1 2 1 1

Page 29: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Create a count array to store the count of each unique object:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 2 2 1 1

Page 30: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Modify count array by adding the previous number :

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 2 2 1 1

Page 31: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Modify count array by adding the previous number :

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 2 2 1 1

Page 32: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Modify count array by adding the previous number :

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 2 1 1

Page 33: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Modify count array by adding the previous number :

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 1 1

Page 34: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Modify count array by adding the previous number :

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 1

Page 35: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Modify count array by adding the previous number :

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7

Page 36: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7

Page 37: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7

1 2 3 4 5 6 7

Page 38: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7

1 2 3 4 5 6 7

Page 39: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7

1 2 3 4 5 6 7

Page 40: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 1 3 5 6 7

1 2 3 4 5 6 7

1

Page 41: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 5 6 7

1 2 3 4 5 6 7

1

Page 42: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 5 6 7

1 2 3 4 5 6 7

1

Page 43: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 5 5 7

1 2 3 4 5 6 7

1 4

Page 44: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 5 5 7

1 2 3 4 5 6 7

1 4

Page 45: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 5 5 7

1 2 3 4 5 6 7

1 4

Page 46: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 5 5 7

1 2 3 4 5 6 7

1 3 4

Page 47: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 4 5 7

1 2 3 4 5 6 7

1 3 4

Page 48: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 4 5 7

1 2 3 4 5 6 7

1 3 4

Page 49: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 4 5 7

1 2 3 4 5 6 7

1 3 4

Page 50: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 3 4 5 7

1 2 3 4 5 6 7

1 2 3 4

Page 51: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 4 5 7

1 2 3 4 5 6 7

1 2 3 4

Page 52: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 4 5 7

1 2 3 4 5 6 7

1 2 3 4

Page 53: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 4 5 7

1 2 3 4 5 6 7

1 2 3 4

Page 54: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 4 5 7

1 2 3 4 5 6 7

1 2 3 3 4

Page 55: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 7

1 2 3 4 5 6 7

1 2 3 3 4

Page 56: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 7

1 2 3 4 5 6 7

1 2 3 3 4

Page 57: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 7

1 2 3 4 5 6 7

1 2 3 3 4

Page 58: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 7

1 2 3 4 5 6 7

1 2 3 3 4 5

Page 59: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 6

1 2 3 4 5 6 7

1 2 3 3 4 5

Page 60: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 6

1 2 3 4 5 6 7

1 2 3 3 4 5

Page 61: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 6

1 2 3 4 5 6 7

1 2 3 3 4 5

Page 62: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 2 3 5 6

1 2 3 4 5 6 7

1 2 2 3 3 4 5

Page 63: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 1 3 5 6

1 2 3 4 5 6 7

1 2 2 3 3 4 5

Page 64: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Output each object from the input sequence followed by

decreasing its count by 1:

1 4 3 2 3 5 2

0 1 2 3 4 5

0 0 1 3 5 6

1 2 3 4 5 6 7

1 2 2 3 3 4 5

Page 65: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Array is now sorted

1 2 2 3 3 4 5

Page 66: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

2 4 1 1 3 Example 2:

0 1 2 3 4

0 2 1 1 1

0 2 3 4 5

2

Count

Range=[0-4]

Add

1 2 3 4 5

Output

0 1 2 3 4

0 2 2 4 5Reduce

0 1 2 3 4

2 4

1 2 3 4 5

Output

0 2 2 4 4Reduce

0 1 2 3 4

1 2 4

1 2 3 4 5

Output

0 1 2 4 4Reduce

0 1 2 3 4

1 1 2 4

1 2 3 4 5

Output

0 0 2 4 4Reduce

0 1 2 3 4

1 1 2 3 4

1 2 3 4 5

Output

1 1 2 3 4

1 2 3 4 5

Sorted

Page 67: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Python

Code

Page 68: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Page 69: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Page 70: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Time Complexity: O(n+k) where n is the number of elements in input

array, and k is the range of input.

Example of worst case

Range between 1 to 10K

10 5 10k 5k 200

Page 71: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Radix Sort

Page 72: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Radix sort is an algorithm that sorts numbers by processing digits

of each number either starting from the least significant digit (LSD)

or starting from the most significant digit (MSD).

The idea of Radix Sort is to do digit by digit sort starting from

least significant digit to most significant digit. Radix sort uses

counting sort as a subroutine to sort.

Page 73: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Algorithm:

Step1: Take the least significant digit of each element

Step2 : Sort the list of elements based on that digit

Step3 : Repeat the sort with each more significant digit

Page 74: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Assume the following Array:

170 45 75 90 802 24 2 66

Page 75: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

The Sorted list will appear after three steps

170 45 75 90 802 24 2 66

170 90 802 2 24 45 75 66

802 2 24 45 66 170 75 90

2 24 45 66 75 90 170 802

Page 76: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Step1: Sorting by least significant digit (1s place)

170 45 75 90 802 24 2 66

170 90 802 2 24 45 75 66

Page 77: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Step2: Sorting by next digit (10s place)

170 90 802 2 24 45 75 66

802 2 24 45 66 170 75 90

Page 78: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Step3: Sorting by most significant digit (100s place)

802 2 24 45 66 170 75 90

2 24 45 66 75 90 170 802

Page 79: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Array is now sorted

2 24 45 66 75 90 170 802

Page 80: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Example 2

1 2 3

5 8 3

1 5 4

5 6 7

6 8 9

6 2 5

4 5 6

1 2 3

5 8 3

1 5 4

6 2 5

4 5 6

5 6 7

6 8 9

1 2 3

6 2 5

1 5 4

4 5 6

5 6 7

5 8 3

6 8 9

1 2 3

1 5 4

4 5 6

5 6 7

5 8 3

6 2 5

6 8 9

1s 10s 100s

Page 81: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Page 82: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Python Code

Page 83: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Python Code

Page 84: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Time Complexity: O(n+k/d) where n is the number

of elements in input array, k is the range of input,

and d is number of digits.

Page 85: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Merge Sort

Page 86: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Merge Sort is a Divide and Conquer algorithm. It

divides input array in two halves, calls itself for the

two halves and then merges the two sorted halves.

Page 87: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Algorithm:

Step1: Divide the list recursively into two halves until it can

no more be divided

Step2 : Merge (Conquer) the smaller lists into new list in

sorted order

Page 88: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Assume the following Array:

85 24 63 45 17 31 96 50

Page 89: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Divide

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

Page 90: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Divide

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

Page 91: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Divide

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

85 24 63 45 17 31 96 50

Page 92: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

85 24 63 45 17 31 96 50

Page 93: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

85 24 63 45 17 31 96 50

Page 94: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85

85 24 63 45 17 31 96 50

Page 95: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85

85 24 63 45 17 31 96 50

Page 96: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85 45 63

85 24 63 45 17 31 96 50

Page 97: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85 45 63

85 24 63 45 17 31 96 50

Page 98: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85 45 63 17 31

85 24 63 45 17 31 96 50

Page 99: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85 45 63 17 31

85 24 63 45 17 31 96 50

Page 100: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 101: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 102: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 103: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 45

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 104: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 45 63

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 105: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 45 63 85

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 106: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

24 45 63 85 17 31 50 96

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 107: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Sort & Merge

17 24 31 45 50 63 85 96

24 45 63 85 17 31 50 96

24 85 45 63 17 31 50 96

85 24 63 45 17 31 96 50

Page 108: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Array is now sorted

17 24 31 45 50 63 85 96

Page 109: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Example 2

Page 110: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Page 111: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Page 112: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Page 113: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

Time Complexity: O(n * log(n) )

Page 114: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

facebook.com/mloey

[email protected]

twitter.com/mloey

linkedin.com/in/mloey

[email protected]

mloey.github.io

Page 115: Algorithms Lecture 5: Sorting Algorithms II

Analysis and Design of Algorithms

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

THANKS FOR YOUR TIME