Insertion Merge Sort
Insertion Merge Sort
Insertion Merge Sort
Sorting I / Slide 2
Sorting
☛ Selection sort or bubble sort
1. Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for remainder of the list (starting at the
second position)
☛ Insertion sort
☛ Merge sort
☛ Quicksort
☛ Shellsort
☛ Heapsort
☛ Topological sort
☛ …
Sorting I / Slide 3
☛ Mergesort:
■ Divide and conquer principle
☛ Insertion:
■ Incremental algorithm principle
Sorting I / Slide 5
Mergesort
Based on divide-and-conquer strategy
Dividing
☛ If an array A[0..N-1]: dividing takes O(1) time
■ we can represent a sublist by two integers left
and right: to divide A[left..Right], we
compute center=(left+right)/2 and obtain
A[left..Center] and A[center+1..Right]
Sorting I / Slide 7
Mergesort
☛ Divide-and-conquer strategy
■ recursively mergesort the first half and the second
half
■ merge the two sorted halves together
Sorting I / Slide 8
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/MSort.html
Sorting I / Slide 9
How to merge?
☛ Input: two sorted array A and B
☛ Output: an output sorted array C
☛ Three counters: Actr, Bctr, and Cctr
■ initially set to the beginning of their respective arrays
(1) The smaller of A[Actr] and B[Bctr] is copied to the next entry in
C, and the appropriate counters are advanced
(2) When either input list is exhausted, the remainder of the other
list is copied to C
Sorting I / Slide 10
Example: Merge
Sorting I / Slide 11
Example: Merge...
Analysis of mergesort
Let T(N) denote the worst-case running time of
mergesort to sort N numbers.
Recurrence equation:
T(1) = 1
T(N) = 2T(N/2) + N
Sorting I / Slide 14
N 2
= 4T ( ) + 2 N
4 = N + N log N
N N = O ( N log N )
= 4(2T ( ) + ) + 2 N
8 4
N
= 8T ( ) + 3 N =
8
N
= 2 T ( k ) + kN
k
2
Sorting I / Slide 15
Insertion sort
1) Initially p = 1
2) Let the first p elements be sorted.
3) Insert the (p+1)th element properly in the list (go inversely from
right to left) so that now p+1 elements are sorted.
4) increment p and go to step (3)
Sorting I / Slide 16
Insertion Sort
Sorting I / Slide 17
Insertion Sort
http://www.cis.upenn.edu/~matuszek/cse121-2003/Applets/Chap03/Insertion/InsertSort.html
☛ Consists of N - 1 passes
☛ For pass p = 1 through N - 1, ensures that the elements in
positions 0 through p are in sorted order
■ elements in positions 0 through p - 1 are already sorted
■ move the element in position p left until its correct place is found
among the first p + 1 elements
Sorting I / Slide 18
Extended Example
To sort the following numbers in increasing order:
34 8 64 51 32 21
p = 1; tmp = 8;
34 > tmp, so second element a[1] is set to 34: {8, 34}…
We have reached the front of the list. Thus, 1st position a[0] = tmp=8
After 1st pass: 8 34 64 51 32 21
(first 2 elements are sorted)
Sorting I / Slide 19
P = 2; tmp = 64;
34 < 64, so stop at 3rd position and set 3rd position = 64
After 2nd pass: 8 34 64 51 32 21
(first 3 elements are sorted)
P = 3; tmp = 51;
51 < 64, so we have 8 34 64 64 32 21,
34 < 51, so stop at 2nd position, set 3rd position = tmp,
After 3rd pass: 8 34 51 64 32 21
(first 4 elements are sorted)
P = 4; tmp = 32,
32 < 64, so 8 34 51 64 64 21,
32 < 51, so 8 34 51 51 64 21,
next 32 < 34, so 8 34 34, 51 64 21,
next 32 > 8, so stop at 1st position and set 2nd position = 32,
After 4th pass: 8 32 34 51 64 21
P = 5; tmp = 21, . . .
After 5th pass: 8 21 32 34 51 64
Analysis: worst-case running
Sorting I / Slide 20
time
An experiment
☛ Code from textbook (using template)
☛ Unix time utility
Sorting I / Slide 25