Divide and Conquer, Mergesort

(分割統治法、マージソート)

Data Structures and Algorithms

6th lecture, October 26, 2017

http://www.sw.it.aoyama.ac.jp/2017/DA/lecture6.html

Martin J. Dürst

AGU

© 2009-17 Martin J. Dürst 青山学院大学

Today's Schedule

 

Summary of Last Lecture

 

Report: Manual Sorting

Deadline: November 1, 2017 (Wednesday), 19:00.

Now is a good time to ask questions about this report!

 

Last Week's Homework

(no need to submit, but bring the sorting cards)

  1. Cut the sorting cards, shuffle them, and try to find a fast way to sort them. Play against others (who is fastest?).
    (Example: Two players, one player uses selection sort, one player uses insertion sort, who wins?)
  2. Find five different applications of sorting.
  3. Implement joining two (normal) heaps.
  4. Think about the time complexity of creating a heap:
    heapify_down will be called n/2 times and may take up to O(log n) each time.
    Therefore, one guess for the overall time complexity is O(n log n).
    However, this upper bound can be improved by careful analysis.
  5. Continue to work on the report (manual sorting).

 

Homework 2: Computational Complexity of heapify_all

[∑0≤i≤∞ i/2i = 0/1 + 1/2 + 2/4 + 3/8 + 4/16 + 5/32 + 6/64 + 7/128 + 8/256 + 9/512 + ...
< 1/2 + 2/4 + 4/8 + 4/8 + 8/32 + 8/32 + 8/32 + 8/32 + 16/512 + ...
= 1/2 + 1·2/4 +2·4/8 + 4·8/32 + 8·16/512 + 16·32/131072 + ...
= 1/2 + 21/22 + 23/23 + 25/25 + 27/29 + 29/217 + 211/233 + 213/265 + ...
= 1/2 + ∑0≤k≤∞2(1+2k)-(2k+1) = 1/2 + ∑0≤k≤∞22k-2k < 3.254]

 

Importance of Sorting

 

Simple Sorting Algorithms

 

Bubble Sort

Possible improvements:

Pseudocode/example implementation: 6sort.rb

 

Various Ways to Loop in Ruby

 

Looping a Fixed Number of Times

Syntax:

number.times do
  # some work
end

Example:

(length-1).times do
  # bubble
end

 

Looping with an Index

Syntax:

start.upto end do |index|
  # some work using index
end

Example:

0.upto(length-2) do |i|
  # select
end

 

Selection Sort

 

Details of Time Complexity for Selection Sort

 

Insertion Sort

Improvement: Using a sentinel: Add a first data item that is guaranteed to be smaller than any real data items. This saves one index check.

 

Details of Time Complexity for Insertion Sort

 

Comparison: Selection Sort vs. Insertion Sort

Selection Sort Insertion Sort
handling first item O(n) O(1)
handling last item O(1) O(n)
initial area perfectly sorted sorted, but some items still missing
rest of data greater than any items in sorted area any size possible
advantage only O(n) exchanges fast if (almost) sorted
disadvantage always same speed may get slower if many moves needed

 

Divide and Conquer

(Latin: divide et impera)

 

Merge Sort (without recursion)

 

Merge

 

Merge Sort

 

Time Complexity of Merge Sort

 

Properties of Merge Sort

 

Summary

 

Preparation for Next Time

 

Glossary

bubble sort
バブル整列法、バブルソート
selection sort
選択整列法、選択ソート
insertion sort
挿入整列法、挿入ソート
sentinel
番兵
index
指数
divide and conquer
分割統治法
military strategy
軍事戦略
tactics
戦術
design principle
設計方針
merge sort
マージソート
merge
併合
2-way merge
2 ウェイ併合
multiway merge
マルチウェイ併合
external memory
外部メモリ
internal memory
内部メモリ
punchcard
パンチカード
magnetic tape
磁気テープ
hard disk
ハードディスク