![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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/1.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/2.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/3.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/4.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/5.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/6.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/7.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/8.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/9.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/10.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/11.jpg)
More Complexity Classes
11input size (n)
com
puta
tion
time
![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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/13.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/14.jpg)
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 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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/16.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/17.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/18.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/19.jpg)
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: ‣](https://reader034.vdocuments.us/reader034/viewer/2022042317/5f062d367e708231d416ad6d/html5/thumbnails/20.jpg)
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