sorting algorithms - lecture 03 · 3 insertion sort algo example analysis 4 selection sort algo...

200
Sorting Algorithms Lecture 03 Ritu Kundu King’s College London Tue, Oct 10, 2017 Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 1 / 31

Upload: others

Post on 18-Oct-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Sorting AlgorithmsLecture 03

Ritu Kundu

King’s College London

Tue, Oct 10, 2017

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 1 / 31

Page 2: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

1 InroductionComparision

2 Bubble SortAlgoExampleAnalysis

3 Insertion SortAlgoExampleAnalysis

4 Selection SortAlgoExampleAnalysis

5 Merge SortAlgoExampleAnalysis

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 2 / 31

Page 3: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Inroduction

Outline1 Inroduction

Comparision2 Bubble Sort

AlgoExampleAnalysis

3 Insertion SortAlgoExampleAnalysis

4 Selection SortAlgoExampleAnalysis

5 Merge SortAlgoExampleAnalysis

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 3 / 31

Page 4: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Inroduction

Probleminput: A sequence of n numbers < a1,a2, . . . ,an >.output: A permutation (reordering) < a′1,a

′2, . . . ,a

′n > of the input

sequence such that a′1 ≤ a′2 ≤ . . . ≤ a′n.

Example

< 31,41,59,26,41,58 >⇒ SORTING ⇒< 26,31,41,41,58,59 >

ApplicationsEasy searchDatabase operationsBasis of many algos (string processing, partitioning, intersection)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 4 / 31

Page 5: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Inroduction Comparision

Factors

Running-time (worst, best, average): Asymptotic time-complexityAdditional space requirement: In-place (θ(1) memory) or notInitial conditions(input order, key distribution, size) of input data:already-sorted, nearly-sorted, reverse-sorted, few unique keys,small/large sizeStability: => two elements which have the same value will retaintheir relative order after sorting. Example:

< 31,41∗,41+,26,41−,58 >⇒ SORTING ⇒<26,31,41∗,41+,41−,58 >

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 5 / 31

Page 6: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Inroduction Comparision

An ideal algorithm

O(nlogn) worst time comparisons (Ω(n) lower bound with certainassumption about the data. In general can not be less than this a.In-placeStableAdaptive

aA more detailed proof is given in Donald E. Knuth, The Art of ComputerProgramming, Volume 3: Sorting and Searching, 2nd Ed., Addison Wesley,1998, §5.3.1, p.180.)

No algorithm is an absolute ideal.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 6 / 31

Page 7: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Inroduction Comparision

Types

ExternalInternal

BubbleInsertionSelectionMerge

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 7 / 31

Page 8: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort

Outline1 Inroduction

Comparision2 Bubble Sort

AlgoExampleAnalysis

3 Insertion SortAlgoExampleAnalysis

4 Selection SortAlgoExampleAnalysis

5 Merge SortAlgoExampleAnalysis

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 8 / 31

Page 9: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Algo

Pass 1Pass 2

· · ·

Pass 6Pass 7

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 9 / 31

Page 10: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1 Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 11: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

5 1 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 12: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 13: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 5 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 14: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 5 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 15: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 16: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 17: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 18: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 19: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 2 0 4 5 0 6 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 20: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 2 0 4 5 0 6 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 21: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 5 0 6 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 22: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 5 0 6 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 23: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 5 0 6 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 24: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 25: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 26: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

1 0 2 4 0 5 6 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 27: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 4 0 5 6 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 28: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 4 0 5 6 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 29: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 4 0 5 6 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 30: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 31: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 32: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 2 0 4 5 6 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 33: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 2 0 4 5 6 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 34: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 2 0 4 5 6 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 35: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 36: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 37: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 1 0 2 4 5 6 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 38: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 1 0 2 4 5 6 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 39: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 0 1 2 4 5 6 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 40: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 0 1 2 4 5 6 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 41: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 0 1 2 4 5 6 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 42: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 0 1 2 4 5 6 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 43: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 0 1 2 4 5 6 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 44: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 0 1 2 4 5 6 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 45: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 2 0 4 5 0 6 9

Pass 2

1 0 2 4 0 5 6 9

Pass 3

0 1 2 0 4 5 6 9

Pass 4

0 1 0 2 4 5 6 9

Pass 5

0 0 1 2 4 5 6 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 46: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1 Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 10 / 31

Page 47: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Algorithm 1 BubbleSort algorithm

1: procedure BUBBLE(a[],n)2: for i ← n − 1 to 1 do3: for j ← 1 to i do4: if a[j − 1] > a[j] then5: swap(a[j − 1],a[j])6: end if7: end for8: end for9: end procedure

Running Time

Σn−10 Θ(i) = Θ(Σn−1

1 i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 11 / 31

Page 48: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Algorithm 2 BubbleSort algorithm

1: procedure BUBBLE(a[],n)2: for i ← n − 1 to 1 do3: for j ← 1 to i do4: if a[j − 1] > a[j] then5: swap(a[j − 1],a[j])6: end if7: end for8: end for9: end procedure

Θ(1)

Running Time

Σn−10 Θ(i) = Θ(Σn−1

1 i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 11 / 31

Page 49: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Algorithm 3 BubbleSort algorithm

1: procedure BUBBLE(a[],n)2: for i ← n − 1 to 1 do3: for j ← 1 to i do4: if a[j − 1] > a[j] then5: swap(a[j − 1],a[j])6: end if7: end for8: end for9: end procedure

Θ(1) Θ(i)

Running Time

Σn−10 Θ(i) = Θ(Σn−1

1 i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 11 / 31

Page 50: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Algorithm 4 BubbleSort algorithm

1: procedure BUBBLE(a[],n)2: for i ← n − 1 to 1 do3: for j ← 1 to i do4: if a[j − 1] > a[j] then5: swap(a[j − 1],a[j])6: end if7: end for8: end for9: end procedure

Θ(1) Θ(i) Σn−11 Θ(i)

Running Time

Σn−10 Θ(i) = Θ(Σn−1

1 i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 11 / 31

Page 51: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Algorithm 5 BubbleSort algorithm

1: procedure BUBBLE(a[],n)2: for i ← n − 1 to 1 do3: for j ← 1 to i do4: if a[j − 1] > a[j] then5: swap(a[j − 1],a[j])6: end if7: end for8: end for9: end procedure

Θ(1) Θ(i) Σn−11 Θ(i)

Running Time

Σn−10 Θ(i) = Θ(Σn−1

1 i) = Θ(n2)Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 11 / 31

Page 52: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running TimeWorst case:Reverse sorted:Already sorted:

In-place

Stable

Adaptive(Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 53: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Worst case: O(n2)

Reverse sorted:Already sorted:

In-place

Stable

Adaptive(Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 54: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Worst case: O(n2)

Reverse sorted: Θ(n2)

Already sorted:

In-place

Stable

Adaptive(Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 55: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Worst case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Can be designed for Θ(n)

In-place

Stable

Adaptive(Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 56: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Worst case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Can be designed for Θ(n)

In-place

Stable

Adaptive(Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 57: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Worst case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Can be designed for Θ(n)

In-place

O(1) extra space

Stable

Adaptive(Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 58: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Worst case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Can be designed for Θ(n)

In-place

O(1) extra space

StableYes

Adaptive(Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 59: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Bubble Sort Analysis

Running Time

Worst case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Can be designed for Θ(n)

In-place

O(1) extra space

StableYes

AdaptiveYes (Suggest the change in the previous implementation.)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 12 / 31

Page 60: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort

Outline1 Inroduction

Comparision2 Bubble Sort

AlgoExampleAnalysis

3 Insertion SortAlgoExampleAnalysis

4 Selection SortAlgoExampleAnalysis

5 Merge SortAlgoExampleAnalysis

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 13 / 31

Page 61: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Algo

An intermediate step Pass 1

Pass 2

· · ·

Pass 7

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 14 / 31

Page 62: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1 Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 63: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

5 1 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 64: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 65: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 66: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 5 2 0 4 6 0 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 67: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 5 2 0 4 6 0 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 68: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 69: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 70: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

1 2 5 0 4 6 0 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 71: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

1 2 5 0 4 6 0 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 72: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

1 2 0 5 4 6 0 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 73: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

1 0 2 5 4 6 0 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 74: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 75: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 76: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 5 4 6 0 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 77: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 5 4 6 0 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 78: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 79: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 80: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 81: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 82: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 83: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 1 2 4 5 6 0 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 84: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 1 2 4 5 6 0 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 85: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 1 2 4 5 0 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 86: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 1 2 4 0 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 87: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 1 2 0 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 88: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 1 0 2 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 89: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 90: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 91: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 92: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 93: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

1 5 2 0 4 6 0 9

Pass 2

1 2 5 0 4 6 0 9

Pass 3

0 1 2 5 4 6 0 9

Pass 4

0 1 2 4 5 6 0 9

Pass 5

0 1 2 4 5 6 0 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 94: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1 Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 15 / 31

Page 95: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Algorithm 6 Insertion algorithm

1: procedure INERTION(a[],n)2: for i ← 1 to n − 1 do3: key ← a[i]4: j ← i5: while j > 0 and a[j − 1] > key do6: a[j]← a[j − 1]7: j ← j − 18: end while9: a[j]← key

10: end for11: end procedure

Running Time

Σn−11 ti where ti is the time taken in i th loop.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 16 / 31

Page 96: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Algorithm 7 Insertion algorithm

1: procedure INERTION(a[],n)2: for i ← 1 to n − 1 do3: key ← a[i]4: j ← i5: while j > 0 and a[j − 1] > key do6: a[j]← a[j − 1]7: j ← j − 18: end while9: a[j]← key

10: end for11: end procedure

O(1)

Running Time

Σn−11 ti where ti is the time taken in i th loop.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 16 / 31

Page 97: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Algorithm 8 Insertion algorithm

1: procedure INERTION(a[],n)2: for i ← 1 to n − 1 do3: key ← a[i]4: j ← i5: while j > 0 and a[j − 1] > key do6: a[j]← a[j − 1]7: j ← j − 18: end while9: a[j]← key

10: end for11: end procedure

O(1)

ti times

Running Time

Σn−11 ti where ti is the time taken in i th loop.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 16 / 31

Page 98: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Algorithm 9 Insertion algorithm

1: procedure INERTION(a[],n)2: for i ← 1 to n − 1 do3: key ← a[i]4: j ← i5: while j > 0 and a[j − 1] > key do6: a[j]← a[j − 1]7: j ← j − 18: end while9: a[j]← key

10: end for11: end procedure

O(1)

ti times Σn−11 ti

Running Time

Σn−11 ti where ti is the time taken in i th loop.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 16 / 31

Page 99: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Algorithm 10 Insertion algorithm

1: procedure INERTION(a[],n)2: for i ← 1 to n − 1 do3: key ← a[i]4: j ← i5: while j > 0 and a[j − 1] > key do6: a[j]← a[j − 1]7: j ← j − 18: end while9: a[j]← key

10: end for11: end procedure

O(1)

ti times Σn−11 ti

Running Time

Σn−11 ti where ti is the time taken in i th loop.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 16 / 31

Page 100: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running TimeWorst case:Best case:Reverse sorted:Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 101: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running Time

Worst case: ti = i ⇒ Running time Σn−11 i = O(n2)

Best case: ti = 1⇒ Running time Σn−11 1 = O(n)

Reverse sorted:Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 102: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running Time

Worst case: ti = i ⇒ Running time Σn−11 i = O(n2)

Best case: ti = 1⇒ Running time Σn−11 1 = O(n)

Reverse sorted: Θ(n2)

Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 103: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running Time

Worst case: ti = i ⇒ Running time Σn−11 i = O(n2)

Best case: ti = 1⇒ Running time Σn−11 1 = O(n)

Reverse sorted: Θ(n2)

Already sorted: Θ(n)

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 104: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running Time

Worst case: ti = i ⇒ Running time Σn−11 i = O(n2)

Best case: ti = 1⇒ Running time Σn−11 1 = O(n)

Reverse sorted: Θ(n2)

Already sorted: Θ(n)

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 105: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running Time

Worst case: ti = i ⇒ Running time Σn−11 i = O(n2)

Best case: ti = 1⇒ Running time Σn−11 1 = O(n)

Reverse sorted: Θ(n2)

Already sorted: Θ(n)

In-place

O(1) extra space

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 106: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running Time

Worst case: ti = i ⇒ Running time Σn−11 i = O(n2)

Best case: ti = 1⇒ Running time Σn−11 1 = O(n)

Reverse sorted: Θ(n2)

Already sorted: Θ(n)

In-place

O(1) extra space

StableYes

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 107: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running Time

Worst case: ti = i ⇒ Running time Σn−11 i = O(n2)

Best case: ti = 1⇒ Running time Σn−11 1 = O(n)

Reverse sorted: Θ(n2)

Already sorted: Θ(n)

In-place

O(1) extra space

StableYes

AdaptiveYes, and less overhead

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 108: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running TimeWorst case:Best case:Reverse sorted:Already sorted:

In-place

Stable

Adaptive

What if binary search is used?

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 109: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Insertion Sort Analysis

Running TimeWorst case:Best case:Reverse sorted:Already sorted:

In-place

Stable

Adaptive

What if a linked list is used instead of an array?

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 17 / 31

Page 110: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort

Outline1 Inroduction

Comparision2 Bubble Sort

AlgoExampleAnalysis

3 Insertion SortAlgoExampleAnalysis

4 Selection SortAlgoExampleAnalysis

5 Merge SortAlgoExampleAnalysis

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 18 / 31

Page 111: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Algo

An intermediate step

*

Pass 1

*

Pass 2

*

· · ·

Pass 7

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 19 / 31

Page 112: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1 Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 113: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

5 1 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 114: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

5 1 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 115: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

5 1 2 0 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 116: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 117: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 1 2 5 4 6 0 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 118: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 1 2 5 4 6 0 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 119: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 1 2 5 4 6 0 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 120: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 121: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 2 5 4 6 1 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 122: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 2 5 4 6 1 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 123: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 2 5 4 6 1 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 124: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 125: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 5 4 6 2 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 126: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 5 4 6 2 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 127: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 5 4 6 2 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 128: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 129: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 130: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 131: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 132: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 133: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

0 0 1 2 4 6 5 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 134: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

0 0 1 2 4 6 5 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 135: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

0 0 1 2 4 6 5 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 136: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 137: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 138: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 139: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1

0 1 2 5 4 6 0 9

Pass 2

0 0 2 5 4 6 1 9

Pass 3

0 0 1 5 4 6 2 9

Pass 4

0 0 1 2 4 6 5 9

Pass 5

0 0 1 2 4 6 5 9

Pass 6

0 0 1 2 4 5 6 9

Pass 7

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 140: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Example

Input

5 1 2 0 4 6 0 9

Pass 1 Pass 2

Pass 3 Pass 4

Pass 5 Pass 6

Pass 7

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 20 / 31

Page 141: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Algorithm 11 Selection algorithm

1: procedure SELECTION(a[],n)2: for i ← 0 to n − 2 do3: minPos ← i4: for j ← i + 1 to n − 1 do5: if a[j] < a[minPos] then6: minPos ← j7: end if8: end for9: swap(a[i],a[minPos])

10: end for11: end procedure

Running Time

Σn−20 Θ(n − i) = Θ(Σn−1

1 (n − i)) = · · · = Θ(Σn1i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 21 / 31

Page 142: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Algorithm 12 Selection algorithm

1: procedure SELECTION(a[],n)2: for i ← 0 to n − 2 do3: minPos ← i4: for j ← i + 1 to n − 1 do5: if a[j] < a[minPos] then6: minPos ← j7: end if8: end for9: swap(a[i],a[minPos])

10: end for11: end procedure

Θ(1)

Running Time

Σn−20 Θ(n − i) = Θ(Σn−1

1 (n − i)) = · · · = Θ(Σn1i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 21 / 31

Page 143: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Algorithm 13 Selection algorithm

1: procedure SELECTION(a[],n)2: for i ← 0 to n − 2 do3: minPos ← i4: for j ← i + 1 to n − 1 do5: if a[j] < a[minPos] then6: minPos ← j7: end if8: end for9: swap(a[i],a[minPos])

10: end for11: end procedure

Θ(1) Θ(n − i)times

Running Time

Σn−20 Θ(n − i) = Θ(Σn−1

1 (n − i)) = · · · = Θ(Σn1i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 21 / 31

Page 144: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Algorithm 14 Selection algorithm

1: procedure SELECTION(a[],n)2: for i ← 0 to n − 2 do3: minPos ← i4: for j ← i + 1 to n − 1 do5: if a[j] < a[minPos] then6: minPos ← j7: end if8: end for9: swap(a[i],a[minPos])

10: end for11: end procedure

Θ(1) Θ(n − i)times

Σn−20 Θ(n −

i)

Running Time

Σn−20 Θ(n − i) = Θ(Σn−1

1 (n − i)) = · · · = Θ(Σn1i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 21 / 31

Page 145: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Algorithm 15 Selection algorithm

1: procedure SELECTION(a[],n)2: for i ← 0 to n − 2 do3: minPos ← i4: for j ← i + 1 to n − 1 do5: if a[j] < a[minPos] then6: minPos ← j7: end if8: end for9: swap(a[i],a[minPos])

10: end for11: end procedure

Θ(1) Θ(n − i)times

Σn−20 Θ(n −

i)

Running Time

Σn−20 Θ(n − i) = Θ(Σn−1

1 (n − i)) = · · · = Θ(Σn1i) = Θ(n2)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 21 / 31

Page 146: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running TimeWorst case:Best case:Reverse sorted:Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 147: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running Time

Worst case: O(n2)

Best case: O(n2)

Reverse sorted:Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 148: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running Time

Worst case: O(n2)

Best case: O(n2)

Reverse sorted: Θ(n2)

Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 149: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running Time

Worst case: O(n2)

Best case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Θ(n2)

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 150: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running Time

Worst case: O(n2)

Best case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Θ(n2)

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 151: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running Time

Worst case: O(n2)

Best case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Θ(n2)

In-place

O(1) extra space

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 152: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running Time

Worst case: O(n2)

Best case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Θ(n2)

In-place

O(1) extra space

StableNo

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 153: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running Time

Worst case: O(n2)

Best case: O(n2)

Reverse sorted: Θ(n2)

Already sorted: Θ(n2)

In-place

O(1) extra space

StableNo

AdaptiveNO

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 154: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Selection Sort Analysis

Running TimeWorst case:Best case:Reverse sorted:Already sorted:

In-place

Stable

Adaptive

Is there anything good about this algo?What if a heap is used to find the minimum? (Heap Sort)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 22 / 31

Page 155: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort

Outline1 Inroduction

Comparision2 Bubble Sort

AlgoExampleAnalysis

3 Insertion SortAlgoExampleAnalysis

4 Selection SortAlgoExampleAnalysis

5 Merge SortAlgoExampleAnalysis

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 23 / 31

Page 156: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Divide and Conquer

Divide the n-elements sequence into 2 subsequences of n/2elements.Recursively sort each subsequence using merge sort.Merge the two sorted subsequences to produce the sortedanswer.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 24 / 31

Page 157: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Divide and Conquer

Divide the n-elements sequence into 2 subsequences of n/2elements.Recursively sort each subsequence using merge sort.Merge the two sorted subsequences to produce the sortedanswer.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 24 / 31

Page 158: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Divide and Conquer

Divide the n-elements sequence into 2 subsequences of n/2elements.Recursively sort each subsequence using merge sort.Merge the two sorted subsequences to produce the sortedanswer.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 24 / 31

Page 159: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Divide and Conquer

Divide the n-elements sequence into 2 subsequences of n/2elements.Recursively sort each subsequence using merge sort.Merge the two sorted subsequences to produce the sortedanswer.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 24 / 31

Page 160: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Divide and Conquer

Divide the n-elements sequence into 2 subsequences of n/2elements.Recursively sort each subsequence using merge sort.Merge the two sorted subsequences to produce the sortedanswer.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 24 / 31

Page 161: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Divide and Conquer

Divide the n-elements sequence into 2 subsequences of n/2elements.Recursively sort each subsequence using merge sort.Merge the two sorted subsequences to produce the sortedanswer.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 24 / 31

Page 162: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Divide and Conquer

Divide the n-elements sequence into 2 subsequences of n/2elements.Recursively sort each subsequence using merge sort.Merge the two sorted subsequences to produce the sortedanswer.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 24 / 31

Page 163: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 164: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 165: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 166: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 167: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 168: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 169: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 170: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Algo

Merging

· · ·

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 25 / 31

Page 171: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 172: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 173: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5

0 2 4 6 0 9

Pass 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 174: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2

4 6 0 9

Pass 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 175: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6

0 9

Pass 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 176: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 177: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

1 5 0 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 178: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

1 5 2

4 6 0 9

0

0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 179: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

1 5 2

4 6 0 9

0

0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 180: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

5 2

4 6 0 9

0 1

0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 181: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

5 2

4 6 0 9

0 1

0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 182: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

5

4 6 0 9

0 1 2

0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 183: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 9

0 1 2 5

0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 184: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 90 1 2 5

0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 185: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 9

0 1 2 5 0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 186: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 9

0 1 2 5 0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 187: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 9

0 1 2 5 0 4 6 9

Pass 3

0 1 2 5 0 4 6 9

0 0 1 2 4 5 6 9

Output

0 0 1 2 4 5 6 9

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 188: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Example

Input

5 1 2 0 4 6 0 9

5 1 2 0 4 6 0 9

Pass 1

1 5 0 2 4 6 0 9

Pass 2

4 6 0 90 4 6 9

Pass 3

0 1 2 5 0 4 6 9

Output

0 0 1 2 4 5 6 9Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 26 / 31

Page 189: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Algorithm 16 MergeSort algorithm1: procedure MERGESORT(a[], left , right)2: if left < right then3: mid ← b(left + right)/2c4: MergeSort(a, left ,mid)5: MergeSort(a,mid + 1, right)6: Merge(a, left ,mid , right)7: end if8: end procedure9: procedure MERGE(a[], left ,mid , right)10: L← a[left ..mid ]11: R ← a[mid + 1..right]12: k ← left13: while there are still elements in L or R do14: Compare the ‘first’ elements in L and R15: Move the minimum of them from its corresponding list to a[k ]16: k ← k + 117: end while18: end procedure

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 27 / 31

Page 190: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Time analysis

If the problem size is small, say n ≤ c for some constant c, we cansolve the problem in constant time, i.e.,Θ(1).Let T (n) be the time needed to sort for input of size n.Let C(n) be the time needed to merge 2 lists of total size n. Weknow that C(n) = Θ(n).Assume that the problem can be split into 2 subproblems inconstant time and c = 1. then

T (n) =

Θ(1) if n ≤ 12T (n

2 ) + Θ(n) if n > 1.

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 28 / 31

Page 191: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

T (n) = 2T (n2 ) +cn

= 2[2T (n4 ) + c n

2 ] +cn= 4T (n

4 ) +2cn= 4[2T (n

8 ) + c n4 ] +2cn

= 8T (n8 ) +3cn

=...

= 2kT ( n2k ) +kcn

w.l.o.g. , assume 2k = n ⇒ k = log2 n

T (n) = cn log2 n + n = Θ(n log2 n)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 29 / 31

Page 192: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running TimeWorst case:Best case:Reverse sorted:Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 193: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running Time

Worst case: O(n log2 n)

Best case: O(n log2 n)

Reverse sorted:Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 194: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running Time

Worst case: O(n log2 n)

Best case: O(n log2 n)

Reverse sorted: Θ(n log2 n)

Already sorted:

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 195: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running Time

Worst case: O(n log2 n)

Best case: O(n log2 n)

Reverse sorted: Θ(n log2 n)

Already sorted: Current implementation: Θ(n log2 n). Can bemade linear. Any suggestion?

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 196: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running Time

Worst case: O(n log2 n)

Best case: O(n log2 n)

Reverse sorted: Θ(n log2 n)

Already sorted: Current implementation: Θ(n log2 n). Can bemade linear. Any suggestion?

In-place

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 197: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running Time

Worst case: O(n log2 n)

Best case: O(n log2 n)

Reverse sorted: Θ(n log2 n)

Already sorted: Current implementation: Θ(n log2 n). Can bemade linear. Any suggestion?

In-place

O(1) extra space

Stable

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 198: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running Time

Worst case: O(n log2 n)

Best case: O(n log2 n)

Reverse sorted: Θ(n log2 n)

Already sorted: Current implementation: Θ(n log2 n). Can bemade linear. Any suggestion?

In-place

O(1) extra space

StableYes

Adaptive

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 199: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

Merge Sort Analysis

Running Time

Worst case: O(n log2 n)

Best case: O(n log2 n)

Reverse sorted: Θ(n log2 n)

Already sorted: Current implementation: Θ(n log2 n). Can bemade linear. Any suggestion?

In-place

O(1) extra space

StableYes

AdaptiveNo (Current implementation)/ Yes (After a modification)

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 30 / 31

Page 200: Sorting Algorithms - Lecture 03 · 3 Insertion Sort Algo Example Analysis 4 Selection Sort Algo Example Analysis 5 Merge Sort Algo Example Analysis Ritu Kundu (King’s College London)

References

References

Cormen, Leiserson, Rivest : Introduction to Algorithmshttp://www.sorting-algorithms.com/

Ritu Kundu (King’s College London) Sorting Algorithms Tue, Oct 10, 2017 31 / 31