4: sorting and complexity - github pages · compare some sorting algorithms ‣ see/understand a...

20
4: Sorting and Complexity

Upload: others

Post on 13-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

4: Sorting andComplexity

Page 2: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Today• Insertion sort (again)• Common complexity• Other sorting algorithms‣Merge sort‣Quick sort

• Project 2 – Comparison of sorting algorithms

2

Page 3: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Last Week• Sorting Problem‣Order data in sequence

• Insertion Sort‣ Insert value one at a time

3

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Sort

Page 4: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Insertion Sort – Complexity

• Time complexity‣How long time does it take?‣How much longer if the input data size doubles?‣Can we answer without/before executing?

• Not real time Number of test/swap operations4

def insert_sort_inplace(a):for i in range(len(a)):

for j in range(i-1, -1, -1):if a[j] > a[j+1]:

swap(a, j, j+1)return a

Page 5: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Insertion Sort – Complexity

• 𝒏𝒏 = input size; here len(a)• #𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭 = 𝟏𝟏 + 𝟐𝟐 + ⋯+ 𝒏𝒏 − 𝟏𝟏

= �𝒊𝒊=𝟏𝟏

𝒏𝒏−𝟏𝟏

𝒊𝒊 =𝒏𝒏(𝒏𝒏 − 𝟏𝟏)

𝟐𝟐=𝒏𝒏𝟐𝟐

𝟐𝟐−𝒏𝒏

𝟐𝟐5

def insert_sort_inplace(a):for i in range(len(a)):

for j in range(i-1, -1, -1):if a[j] > a[j+1]:

swap(a, j, j+1)return a

= 𝑂𝑂(𝑛𝑛2)

Page 6: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Insertion Sort – Optimized

• Better algorithm• Fewer tests• But still same

complexity 𝑂𝑂 𝑛𝑛2

6

def insert_sort_inplace(a):for i in range(len(a)):

for j in range(i-1, -1, -1):if a[j] > a[j+1]:

swap(a, j, j+1)return a

def insert_sort_inplace_optimized(a):for i in range(len(a)):

for j in range(i-1, -1, -1):if a[j] > a[j+1]:

swap(a, j, j+1)else:

breakreturn a

Page 7: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Common Complexity

Classes

7

Page 8: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Common Complexity Classes

8

constant 定数時間

logarithmic 対数

linear 線形関数

linearithmic,loglinear 準線形、線形対数

quadratic 二乗時間

cubic 三乗時間

Page 9: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Common Complexity Classes

9

input size (n)

com

puta

tion

time

Page 10: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

More Complexity Classes

10

input size (n)

com

puta

tion

time

Page 11: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

More Complexity Classes

11input size (n)

com

puta

tion

time

Page 12: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Merge SortWikipedia

Page 13: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Merge Sort

13

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

SORT SORT

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

MERGE

Page 14: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Merge Sort

14

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0 1 2 3 4 5 6 7

0 1 2 3

0 1 2 3

4 5 6 7

4 5 6 7

8 9 10 11 12 13 14 15

8 9 10 11

8 9 10 11

12 13 14 15

12 13 14 15

Page 15: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

QuicksortWikipedia

Page 16: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Quicksort

16

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

pivot1 2 3 4 5 67 8 9 10 11 12 13 14 15

1 2 3 4 5 6 7 8 9 10 11 12 13 14 150

pivot

SORT1 2 3 4 5 6

pivot

SORT7 8 9 10 11 12 13 14 15

7

Pivotvalue

1 2 3 4 5 6 8 9 10 11 12 13 14 15 16

Page 17: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Summary• Sort Algorithms‣ Insertion sort‣Merge sort‣Quicksort

• Complexity‣Depends on problem size‣Count number of steps:(assignment / test / …)‣Asymptotic order

• Divide-and-conquer‣Solve smaller problems and then combine the results (link)‣Merge sort algorithm ‣Binary search algo (link)

17

Page 18: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Project 2Comparison of

sorting algorithms

Page 19: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Project 2 – Objectives• Goals:‣Compare some sorting algorithms‣See/understand a more complex program‣Obtain nice curves and write a report

•What to do:‣ Implement merge sort‣Evaluate three sorting algorithms (insertion, merge, quick)‣Write a report (2-3 pages) including graphic

19

Page 20: 4: Sorting and Complexity - GitHub Pages · Compare some sorting algorithms ‣ See/understand a more complex program ‣ Obtain nice curves and write a report • What to do: ‣

Project 2 – Evaluation (25pts)• Implement merge sort (~5pts)• Obtain curves comparing sorting algos (~5pts)‣ Use the provided code test_sort.py.‣ You may change some parts (e.g. REPETITIONS, TEST_SIZES, …)

• Write the report (~10pts)• Add something personal (~5pts)‣ Other algorithm (bubble sort, selection sort, …)‣ Improve/simplify the given code‣ …

• Deadline: End of January (exact date TBD) 20

+ some bonus pointsfor great solutions,

and/or nice reports