Академический Документы
Профессиональный Документы
Культура Документы
Insertion Sort
Algorithm:
Move left-to-right through array.
Exchange next element with larger
elements to its left, one-by-one.
Example
Contd.....
Average case
???
Analysis
Simplifications
Ignore actual and abstract statement costs
Order of growth is the interesting measure:
Highest-order term is what counts
Remember, we are doing asymptotic analysis
As the input size grows larger it is the high order term that
dominates
Merge Sort
Apply divide-and-conquer to sorting problem
Problem: Given n elements, sort elements into
non-decreasing order
Divide-and-Conquer:
If n=1 terminate (every one-element list is already
sorted)
If n>1, partition elements into two or more subcollections; sort each; combine into a single sorted list
How do we partition?
9
Partitioning - Choice 1
First n-1 elements into set A, last element set B
Sort A using this partitioning scheme recursively
B already sorted
Worst case =
c i =
i=2
n(n 1)
2
10
Partitioning - Choice 2
Put element with largest key in B, remaining elements
in A
Sort A recursively
To combine sorted A and B, append B to sorted A
Use Max() to find largest element recursive
SelectionSort()
Use bubbling process to find and move largest element to
right-most position recursive BubbleSort()
All O(n2)
11
Partitioning - Choice 3
13
MergeSort
14 23 45 98
33 42 67
14
14 23 45 98
33 42 67
Merge
15
14 23 45 98
33 42 67
6
Merge
16
14 23 45 98
6
33 42 67
14
Merge
17
14 23 45 98
6
33 42 67
14 23
Merge
18
14 23 45 98
6
33 42 67
14 23 33
Merge
19
14 23 45 98
6
33 42 67
14 23 33 42
Merge
20
14 23 45 98
6
33 42 67
14 23 33 42 45
Merge
21
14 23 45 98
6
33 42 67
14 23 33 42 45 67
Merge
22
14 23 45 98
6
33 42 67
14 23 33 42 45 67 98
Merge
23
//midpoint
}
}
24
Evaluation
Recurrence equation:
Assume n is a power of 2
c1
if n=1
T(n) =
2T(n/2) + c2n
if n>1, n=2k
25
Solution
By Substitution:
T(n) = 2T(n/2) + c2n
T(n/2) = 2T(n/4) + c2n/2
T(n) = 4T(n/4) + 2 c2n
T(n) = 8T(n/8) + 3 c2n
T(n) = 2iT(n/2i) + ic2n
Assuming n = 2k, expansion halts when we get T(1) on right side; this happens
when i=k T(n) = 2kT(1) + kc2n
Since 2k=n, we know k=logn; since T(1) = c1, we get
T(n) = c1n + c2nlogn;
Quicksort Algorithm
Given an array of n elements (e.g., integers):
If array only contains one element, return
Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
Example
We are given array of n integers to sort:
40 20 10 80 60 50
30 100
28
30 100
29
Partitioning Array
Given a pivot, partition the elements of the array
such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
pivot_index = 0
40
20
10
80 60 50
30 100
too_small_index
31
pivot_index = 0
40
20
10
80 60 50
30 100
too_small_index
32
pivot_index = 0
40
20
10
80 60 50
30 100
too_small_index
33
pivot_index = 0
40
20
10
80 60 50
30 100
too_small_index
34
pivot_index = 0
40
20
10
80 60 50
30 100
too_small_index
35
pivot_index = 0
40
20
10
80 60 50
30 100
too_small_index
36
pivot_index = 0
40
20
10
80 60 50
30 100
too_small_index
37
pivot_index = 0
40
20
10
30 60 50
80 100
too_small_index
38
pivot_index = 0
40
20
10
30 60 50
80 100
too_small_index
39
pivot_index = 0
40
20
10
30 60 50
80 100
too_small_index
40
pivot_index = 0
40
20
10
30 60 50
80 100
too_small_index
41
pivot_index = 0
40
20
10
30 60 50
80 100
too_small_index
42
pivot_index = 0
40
20
10
30 60 50
80 100
too_small_index
43
pivot_index = 0
40
20
10
30 60 50
80 100
too_small_index
44
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
45
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
46
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
47
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
48
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
49
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
50
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
51
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
52
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
53
pivot_index = 0
40
20
10
30
50 60 80 100
too_small_index
54
pivot_index = 4
20
10
30 40 50 60 80 100
too_small_index
55
Partition Result
7
20 10 30
40
50
60 80 100
<= data[pivot]
> data[pivot]
56
20 10 30
40
50
60 80 100
<= data[pivot]
> data[pivot]
57
58
Quicksort Analysis
Assume that keys are random, uniformly
distributed.
What is best case running time?
59
Quicksort Analysis
60
Quicksort Analysis
Quicksort Analysis
Quicksort Analysis
Quicksort Analysis
Quicksort Analysis
65
Quicksort Analysis
66
10
12
13
50 57 63 100
67
10
12
13
50 57 63 100
10
12
13
50 57 63 100
10
12
13
50 57 63 100
10
12
13
50 57 63 100
10
12
13
50 57 63 100
10
12
13
50 57 63 100
10
12
13
50 57 63 100
> data[pivot]
74
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
79
Quicksort Analysis
80
81
Improving Performance of
Quicksort
Improved selection of pivot.
For sub-arrays of size 3 or less, apply brute
force search:
Sub-array of size 1: trivial
Sub-array of size 2:
if(data[first] > data[second]) swap them