Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 402

Diving into Algorithms

Presentation by

Rajmohan De Sarkar
Apr 18, 2012

The Quick Sort

Apr 18, 2012

Quicksort
To sort a[left...right]:
1. if left < right:

1.1. Partition a[left...right] such that:


all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate

Apr 18, 2012

Partitioning
A key step in the Quicksort algorithm is
partitioning the array
We choose some (any) number p in the
array to use as a pivot
We partition the array into three parts:
p

numbers less
than p

numbers greater than


or equal to p
Apr 18, 2012

Partitioning
Choose an array value (say, the first) to use as
the pivot
Starting from the left end, find the first
element that is greater than or equal to the
pivot
Searching backward from the right end, find
the first element that is less than the pivot
Interchange (swap) these two elements
Repeat, searching from where we left off, until
done

Apr 18, 2012

Partitioning
To partition a[left...right]:
1. Set pivot = a[left], l = left + 1, r = right;
2. while l < r, do

2.1. while l < right & a[l] < pivot , set l = l + 1


2.2. while r > left & a[r] >= pivot , set r = r - 1
2.3. if l < r, swap a[l] and a[r]
3. Set a[left] = a[r], a[r] = pivot
4. Terminate

Apr 18, 2012

Example of partitioning
choose pivot:
search:
swap:
search:
swap:
search:
swap:
search:

4
4
4
4
4
4
4
4

3
3
3
3
3
3
3
3

6
6
3
3
3
3
3
3

9
9
9
9
1
1
1
1

2
2
2
2
2
2
2
2

4
4
4
4
4
4
2
2

3
3
3
3
3
3
3
3

1
1
1
1
1
1
1
1

2
2
2
2
2
2
4
4

1
1
1
1
9
9
9
9

8
8
8
8
8
8
8
8

9
9
9
9
9
9
9
9

3
3
6
6
6
6
6
6

5
5
5
5
5
5
5
5

6
6
6
6
6
6
6
6

(left

> right)

swap with pivot:

133122344989656
Apr 18, 2012

The partition method (C/C++)


int partition(int a[], int left, int right) {
int p = a[left];
int l = left + 1;
int r = right;
while (l < r) {
while (l <= right && a[l] <= p)
l++;
while (r > left && a[r] >= p)
r--;
if (l < r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
}
a[left] = a[r];
a[r] = p;
return r;
Apr 18, 2012
}

The quicksort method (in C/C++)


void quicksort(int array[], int left, int right) {
if (left < right) {
if (left == right - 1) {
if (array[left] > array[right]) {
int temp

= array[left];

array[left] = array[right];
array[right] = temp;
}
}
else {
int p = partition(array, left, right);
quicksort(array, left, p - 1);
quicksort(array, p + 1, right);
}
}
}

Apr 18, 2012

Analysis of quicksortbest case


Suppose each partition operation divides the
array almost exactly in half
Then the depth of the recursion in log2n
Because thats how many times we can halve n

However, there are many recursions!


How can we figure this out?
We note that
Each partition is linear over its subarray
All the partitions at one level cover the array

Apr 18, 2012

10

Partitioning at various levels

Apr 18, 2012

11

Best case II
We cut the array size in half each time
So the depth of the recursion in log2n
At each level of the recursion, all the partitions at
that level do work that is linear in n
O(log2n) * O(n) = O(n log2n)
Hence in the average case, quicksort has time
complexity O(n log2n)
What about the worst case?

Apr 18, 2012

12

Worst case
In the worst case, partitioning always divides the
size n array into these three parts:
A length one part, containing the pivot itself
A length zero part, and
A length n-1 part, containing everything else

We dont recur on the zero-length part


Recurring on the length n-1 part requires (in the
worst case) recurring to depth n-1

Apr 18, 2012

13

Worst case partitioning

Apr 18, 2012

14

Worst case for quicksort


In the worst case, recursion may be n levels deep
(for an array of size n)
But the partitioning work done at each level is still
n
O(n) * O(n) = O(n2)
So worst case for Quicksort is O(n2)
When does this happen?
There are many arrangements that could make this
happen
Here are two common cases:
When the array is already sorted
When the array is inversely sorted
(sorted in the
Apr 18, 2012
opposite order)

15

Typical case for quicksort


If the array is sorted to begin with, Quicksort
is terrible: O(n2)
It is possible to construct other bad cases
However, Quicksort is usually O(n log2n)
The constants are so good that Quicksort is
generally the fastest algorithm known
Most real-world sorting is done by Quicksort

Apr 18, 2012

16

Final comments
Quicksort is the fastest known sorting algorithm
For optimum efficiency, the pivot must be chosen
carefully
Median of three is a good technique for choosing
the pivot
However, no matter what you do, there will be
some cases where Quicksort runs in O(n2) time

Apr 18, 2012

17

The Bubble Sort

The Bubble Sort


Algorithm
The Bubble Sort compares adjacent elements in a list, and
swaps them if they are not in order. Each pair of adjacent
elements is compared and swapped until the smallest
element bubbles to the top.

Repeat this process each

time stopping one indexed element less, until you compare


only the first (or last) two elements in the list. The largest
element will have sunk to the bottom.

Apr 18, 2012

19

A Bubble Sort Example

6
5
4
3
2
1

Compare
We start by comparing the first
two elements in the List.
This list is an example of a worst
case scenario for the Bubble
Sort, because the List is in the
exact opposite order from the way
we want it sorted (the computer
program does not know that it is
sorted at all).
Apr 18, 2012

20

A Bubble Sort Example


5
6
4
3
2
1

Swap

Apr 18, 2012

21

A Bubble Sort Example


5
6
4
3
2
1

Compare

Apr 18, 2012

22

A Bubble Sort Example


5
4
6
3
2
1

Swap

Apr 18, 2012

23

A Bubble Sort Example


5
4
6
3
2
1

Compare

Apr 18, 2012

24

A Bubble Sort Example


5
4
3
6
2
1

Swap

Apr 18, 2012

25

A Bubble Sort Example


5
4
3
6
2
1

Compare

Apr 18, 2012

26

A Bubble Sort Example


5
4
3
2
6
1

Swap

Apr 18, 2012

27

A Bubble Sort Example


5
4
3
2
6
1

Compare

Apr 18, 2012

28

A Bubble Sort Example


5
4
3
2
1
6

As you can see, the largest number


has bubbled down, or sunk to the
bottom of the List after the first pass
through the List.

Swap

Apr 18, 2012

29

A Bubble Sort Example


5
4
3
2
1
6

Compare
For our second pass through
the List, we start by
comparing these first two
elements in the List.

Apr 18, 2012

30

A Bubble Sort Example


4
5
3
2
1
6

Swap

Apr 18, 2012

31

A Bubble Sort Example


4
5
3
2
1
6

Compare

Apr 18, 2012

32

A Bubble Sort Example


4
3
5
2
1
6

Swap

Apr 18, 2012

33

A Bubble Sort Example


4
3
5
2
1
6

Compare

Apr 18, 2012

34

A Bubble Sort Example


4
3
2
5
1
6

Swap

Apr 18, 2012

35

A Bubble Sort Example


4
3
2
5
1
6

Compare

Apr 18, 2012

36

A Bubble Sort Example


4
3
2
1
5
6

At the end of the second pass, we


stop at element number n - 1,
because the largest element in the
List is already in the last position.
This places the second largest
element in the second to last spot.
Swap

Apr 18, 2012

37

A Bubble Sort Example


4
3
2
1
5
6

Compare
We start with the first two
elements again at the beginning of
the third pass.

Apr 18, 2012

38

A Bubble Sort Example


3
4
2
1
5
6

Swap

Apr 18, 2012

39

A Bubble Sort Example


3
4
2
1
5
6

Compare

Apr 18, 2012

40

A Bubble Sort Example


3
2
4
1
5
6

Swap

Apr 18, 2012

41

A Bubble Sort Example


3
2
4
1
5
6

Compare

Apr 18, 2012

42

A Bubble Sort Example


3
2
1
4
5
6

At the end of the third pass, we stop


comparing and swapping at element
number n - 2.
Swap

Apr 18, 2012

43

A Bubble Sort Example


3
2
1
4
5
6

Compare
The beginning of the fourth pass...

Apr 18, 2012

44

A Bubble Sort Example


2
3
1
4
5
6

Swap

Apr 18, 2012

45

A Bubble Sort Example


2
3
1
4
5
6

Compare

Apr 18, 2012

46

A Bubble Sort Example


2
1
3
4
5
6

Swap
The end of the fourth pass
stops at element number n - 3.

Apr 18, 2012

47

A Bubble Sort Example


2
1
3
4
5
6

Compare

The beginning of the fifth pass...

Apr 18, 2012

48

A Bubble Sort Example


1
2
3
4
5
6

Swap
The last pass compares only
the first two elements of the
List. After this comparison
and possible swap, the
smallest element has
bubbled to the top.

Apr 18, 2012

49

What Swapping Means


6
5
4
3
2
1

TEMP

6
Place the first element into the
Temporary Variable.

Apr 18, 2012

50

What Swapping Means


5
5
4
3
2
1

TEMP

6
Replace the first element with
the second element.

Apr 18, 2012

51

What Swapping Means


5
6
4
3
2
1

TEMP

6
Replace the second element with
the Temporary Variable.

Apr 18, 2012

52

C Code for Swap Procedure


void swap (int *number1, int *number2)
{
int temp;
temp = *number1;
*number1 = *number2;
*number2 = temp;
}

Apr 18, 2012

53

C Code For Bubble Sort


void bubbleSort (char v [], int n) {
int element, index;
for (element = 0; element < n - 1; ++element)
for (index = 0; element < n element - 1; ++index)
if (v [index] < v [index + 1])
swap (&v [index+1], &v [index]);
}

Apr 18, 2012

54

Big - O Notation
Big - O notation is used to describe the efficiency
of a search or sort. The actual time necessary to
complete the sort varies according to the speed of
your system. Big - O notation is an approximate
mathematical formula to determine how many
operations are necessary to perform the search or
sort. The Big - O notation for the Bubble Sort is
O(n2), because it takes approximately n2 passes to
sort the elements.

Apr 18, 2012

55

Insertion Sort

Insertion Sort
Consists of N-1 passes
For pass P = 1 through N-1, insertion sort ensures
that the elements in positions 0 through P are in
the sorted order
In pass P, the element in the position P is moved
left until its correct place is found among the first
P + 1 elements.
Complexity - O(n2)

Apr 18, 2012

57

34

64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

58

34

64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

59

34

64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

60

34

64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

61

34

64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

62

34

64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

63

34

64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

64

34

64 51 32 21

8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

65

34

64 51 32 21

8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

66

34

64 51 32 21

8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

67

34 64 51 32 21

8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

68

34 64 51 32 21

8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

69

34 64 51 32 21

8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

70

34 64 51 32 21

8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

71

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

72

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

73

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

74

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

75

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

76

34

51 32 21

64
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

77

34

51 32 21

64
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

78

34

51 32 21

64
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

79

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

80

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

81

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

82

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

83

34 64 51 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

84

34 64

32 21

51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

85

34 64

32 21

51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

86

34 64

32 21

51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

87

34

64 32 21

51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

88

34

64 32 21

51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

89

34

64 32 21

51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

90

34

64 32 21

51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

91

34 51 64 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

92

34 51 64 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

93

34 51 64 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

94

34 51 64 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

95

34 51 64 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

96

34 51 64 32 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

97

34 51 64

21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

98

34 51 64

21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

99

34 51 64

21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

100

34 51

64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

101

34 51

64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

102

34 51

64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

103

34 51

64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

104

34

51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

105

34

51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

106

34

51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

107

34

51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

108

34 51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

109

34 51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

110

34 51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

111

34 51 64 21

32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

112

32 34 51 64 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

113

32 34 51 64 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

114

32 34 51 64 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

115

32 34 51 64 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

116

32 34 51 64 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

117

32 34 51 64 21

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

118

32 34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

119

32 34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

120

32 34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

121

32 34 51

64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

122

32 34 51

64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

123

32 34 51

64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

124

32 34 51

64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

125

32 34

51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

126

32 34

51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

127

32 34

51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

128

32 34

51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

129

32

34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

130

32

34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

131

32

34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

132

32

34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

133

32 34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

134

32 34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

135

32 34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

136

32 34 51 64

21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

137

21 32 34 51 64

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

138

21 32 34 51 64

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

139

21 32 34 51 64

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

140

21 32 34 51 64

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

141

21 32 34 51 64

void InsertionSort(ElementType A[ ], int N)


{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012

142

The Selection Sort

The Selection Sort


Description
The Selection Sort searches (linear search) all of the
elements in a list until it finds the smallest element. It
swaps this with the first element in the list. Next it finds
the smallest of the remaining elements, and swaps it with
the second element. Repeat this process until you
compare only the last two elements in the list.

Apr 18, 2012

144

The Selection Sort


Algorithm
For each index position i
Find the smallest data value in the array
from positions i through length - 1, where
length is the number of data values stored.
Exchange (swap) the smallest value with
the value at position i.

Apr 18, 2012

145

A Selection Sort Example

6
2
1
3
5
4

Smallest ?
We start by searching for the
smallest element in the List.

Apr 18, 2012

146

A Selection Sort Example


Smallest ?

Apr 18, 2012

6
2
1
3
5
4
147

A Selection Sort Example


6
2
1
3
5
4

Smallest !

Apr 18, 2012

148

A Selection Sort Example

Swap

Apr 18, 2012

6
2
1
3
5
4
149

A Selection Sort Example


1
2
6
3
5
4

Swapped

Apr 18, 2012

150

A Selection Sort Example


Smallest ?
After the smallest element
is in the first position, we
continue searching with the
second element and look
for the next smallest
element.
Apr 18, 2012

1
2
6
3
5
4
151

A Selection Sort Example


1
2
6
3
5
4

Smallest !
In this special case, the
next smallest element is in
the second position
already. Swapping keeps it
in this position.
Apr 18, 2012

152

A Selection Sort Example


1
2
6
3
5
4

Swapped
Swapping keeps it in this
position.

Apr 18, 2012

153

A Selection Sort Example

Smallest ?
After the next smallest element is in
the second position, we continue
searching with the third element and
look for the next smallest element.

Apr 18, 2012

1
2
6
3
5
4
154

A Selection Sort Example


1
2
6
3
5
4

Smallest !

Apr 18, 2012

155

A Selection Sort Example

Swap

Apr 18, 2012

1
2
6
3
5
4
156

A Selection Sort Example

Swapped

Apr 18, 2012

1
2
3
6
5
4
157

A Selection Sort Example

Smallest ?

Apr 18, 2012

1
2
3
6
5
4
158

A Selection Sort Example

Smallest ?

Apr 18, 2012

1
2
3
6
5
4
159

A Selection Sort Example


1
2
3
6
5
4

Smallest !
Apr 18, 2012

160

A Selection Sort Example

Swap
Apr 18, 2012

1
2
3
6
5
4
161

A Selection Sort Example


1
2
3
4
5
6

Swapped
Apr 18, 2012

162

A Selection Sort Example


1
2
3
4
5
6

The last two elements are in


order, so no swap is necessary.

Apr 18, 2012

163

What Swapping Means


6
2
1
3
5
4

TEMP

6
Place the first element into
the Temporary Variable.

Apr 18, 2012

164

What Swapping Means


1
2
1
3
5
4

TEMP

6
Replace the first element
with the value of the
smallest element.

Apr 18, 2012

165

What Swapping Means


1
2
6
3
5
4

TEMP

6
Replace the third element
(in this example) with the
Temporary Variable.

Apr 18, 2012

166

C/C + + Code For Selection Sort


void selectionSort (int a[],int n) {
for (int i = 0; i < n; ++i) {
int pos = min (a, n, i);
swap (a, i, pos);
}
printf ("SORTED ARRAY: ");
for (i = 0; i < n; ++i)
printf ("%d ",a [i]);
printf ("\n");
}
Apr 18, 2012

167

C/C + + Code For Find Minimum


int min (int a[], int n, int i) {
int min = a [i];
int pos = i;
for (int j = i + 1; j < n; ++j) {
if (a [j] < min) {
pos = j;
min = a [j];
}
}
return pos;
}
Apr 18, 2012

168

C/C + + Code for Swap Procedure


void swap (int a[], int i, int j) {
int temp = a [i];
a [i] = a [j];
a [j] = temp;
}

Apr 18, 2012

169

Big - O Notation
Big - O notation is used to describe the
efficiency of a search or sort. The actual
time necessary to complete the sort varies
according to the speed of your system. Big
- O notation is an approximate mathematical
formula to determine how many operations
are necessary to perform the search or sort.
The Big - O notation for the Selection Sort is
O(n2), because it takes approximately n2
passes to sort the elements.

Apr 18, 2012

170

Heapsort

The Heap
A binary heap is a nearly complete binary tree
Implemented as an array A
Two similar attributes:
length[A] is the size (number of slots) in A
heap-size[A] is the number of elements in A
Thus, heap-size[A] length[A]
Also, no element past A[heap-size[A]] is an
element

Apr 18, 2012

172

The Heap

Can be a min-heap or a max-heap


87
51

25

67

41

55

43

87
21
87

17

33

35

51 67 25 41

55

43

21 17 33 35
Apr 18, 2012

173

Simple Functions
PARENT(i)
return (i/2)
LEFT(i)
return (2i)
RIGHT(i)
return (2i + 1)

Apr 18, 2012

174

Properties
Max-heap property:
A[PARENT(i)] A[i]
Min-heap property:
A[PARENT(i)] A[i]
Max-heaps are used for sorting
Min-heaps are used for priority queues (later)
We define the height of a node to be the longest path from
the node to a leaf.
The height of the tree is (lg n)

Apr 18, 2012

175

MAX-HEAPIFY
This is the heart of the algorithm
Determines if an individual node is smaller than its
children
Parent swaps with largest child if that child is
larger
Calls itself recursively
Runs in O(lg n) or O(h)

Apr 18, 2012

176

HEAPIFY
MAX-HEAPIFY (A, i)
l LEFT (i)
r RIGHT(i)
if l heap-size[A] and A[l] > A[i]
then largest l
else largest i
if r heap-size[A] and A[r]>A[largest]
then largest r
if largest i
then exchange A[i] with A[largest]
MAX-HEAPIFY (A, largest)
Apr 18, 2012

177

16

10

14

Apr 18, 2012

178

16

14

10

Apr 18, 2012

179

16

14

10

Apr 18, 2012

180

BUILD-HEAP
Use MAX-HEAPIFY in bottom up manner
Why does the loop start at length[A]/2?
At the start of each loop, each node i is the
root of a max-heap!

BUILD-HEAP (A)
heap-size[A] length[A]
for i length[A]/2 downto 1
do MAX-HEAPIFY(A, i)
Apr 18, 2012

181

HEAPSORT
HEAPSORT (A)
BUILD-HEAP(A)
for i length[A] downto 2
do exchange A[1] with A[i]
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

182

16

14

10

Apr 18, 2012

183

14

10

Swap A[1] A[i]

16

Apr 18, 2012

184

14

10

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

185

10

14

Swap A[1] A[i]

16

Apr 18, 2012

186

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

187

10

14

Swap A[1] A[i]

16

Apr 18, 2012

188

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

189

10

14

Swap A[1] A[i]

16

Apr 18, 2012

190

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

191

10

14

Swap A[1] A[i]

16

Apr 18, 2012

192

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

193

10

14

Swap A[1] A[i]

16

Apr 18, 2012

194

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

195

10

14

Swap A[1] A[i]

16

Apr 18, 2012

196

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

197

10

14

Swap A[1] A[i]

16

Apr 18, 2012

198

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

199

10

14

Swap A[1] A[i]

16

Apr 18, 2012

200

10

14

16

heap-size heap-size 1
MAX-HEAPIFY(A, 1)

Apr 18, 2012

201

C/C++ Code
void MAX_HEAPIFY (int A[], int i) {
int l = LEFT(i);
int r = RIGHT(i);
int largest;
if (l <= heapsize && A[l] > A[i])
largest = l;
else
largest = i;
if (r <= heapsize && A[r]>A[largest])
largest = r;
if (largest != i) {
exchange (&A[i], &A[largest]);
MAX_HEAPIFY (A, largest);
}
}

Apr 18, 2012

202

C/C++ Code
void BUILD_HEAP (int A []) {
heapsize = length;
for (int i = length/2;i >= 1; --i)
MAX_HEAPIFY(A, i);
}
void HEAPSORT (int A[]) {
BUILD_HEAP(A);
for (int i = length;i >= 2; --i) {
exchange (&A[1], &A[i]);
heapsize = heapsize - 1;
MAX_HEAPIFY(A, 1);
}
}
Apr 18, 2012

203

C/C++Code
int length = 5;
int heapsize = 0;
#define LEFT(i) 2*i
#define RIGHT(i) (2*i + 1)
void exchange (int *number1, int *number2) {
int temp;
temp = *number1;
*number1 = *number2;
*number2 = temp;
}
void main () {
int A[] = {0, 9999, 8888, 7777, 6666, 5555};
HEAPSORT (A);
for (int i = 1; i <= 5; ++i)
printf ("%d\n", A [i]);
}

Apr 18, 2012

204

Heapsort: Time complexity


MAX_HEAPIFY takes O(n) time
n BUILD_HEAPs each take O( log2n ) time
Best, worst, average-case
O(nlog2n)

Apr 18, 2012

205

Mergesort

Merge Sort
Apply divide-and-conquer to sorting problem
Problem: Given n elements, sort elements into nondecreasing order
Divide-and-Conquer:
If n=1 terminate (every one-element list is already
sorted)
If n>1, partition elements into two sub-collections; sort
each; combine into a single sorted list
How do we partition?

Apr 18, 2012

207

The Process
Divide the unsorted array into 2 halves until
the sub-arrays only contain one element
Merge the sub-problem solutions together:
Compare the sub-arrays first elements
Remove the smallest element and put it into
the result array
Continue the process until all elements have
been put into the result array

Apr 18, 2012

208

Mergesort: Illustration
85

24

63

45

17

31

Apr 18, 2012

96

50

209

Mergesort: Illustration

85

24

63

45

17

Apr 18, 2012

31

96

50

210

Mergesort: Illustration

17
85

24

63

31

96

50

45

Apr 18, 2012

211

Mergesort: Illustration

17
63
85

31

96

50

45

24

Apr 18, 2012

212

Mergesort: Illustration

17
63
85

31

96

50

45

24

Apr 18, 2012

213

Mergesort: Illustration

17
24

63

31

96

50

45

85

Apr 18, 2012

214

Mergesort: Illustration

17
24

63

31

96

50

45

85

Apr 18, 2012

215

Mergesort: Illustration

17
24

85

63

31

96

50

45

Apr 18, 2012

216

Mergesort: Illustration

17
24

85

63

31

96

50

45

Apr 18, 2012

217

Mergesort: Illustration

17
24

85

63

31

96

50

45

Apr 18, 2012

218

Mergesort: Illustration

17
24

31

96

50

85
63

45

Apr 18, 2012

219

Mergesort: Illustration

17
24

31

96

50

85
63

45

Apr 18, 2012

220

Mergesort: Illustration

17
24

85

31

96

50

45
63

Apr 18, 2012

221

Mergesort: Illustration

17
24

85

31

96

50

45
63

Apr 18, 2012

222

Mergesort: Illustration

17
24

85

45

31

96

50

63

Apr 18, 2012

223

Mergesort: Illustration

17
24

85

45

31

96

50

63

Apr 18, 2012

224

Mergesort: Illustration

17
24

85

45

31

96

50

63

Apr 18, 2012

225

Mergesort: Illustration

17
24

85

45

31

96

50

63

Apr 18, 2012

226

Mergesort: Illustration

24

17
85

45

31

96

50

63

Apr 18, 2012

227

Mergesort: Illustration

24

17
85

45

31

96

50

63

Apr 18, 2012

228

Mergesort: Illustration

24

45
85

17

31

96

50

63

Apr 18, 2012

229

Mergesort: Illustration

24

45
85

17

31

96

50

63

Apr 18, 2012

230

Mergesort: Illustration

24

45

63

17

31

96

50

85

Apr 18, 2012

231

Mergesort: Illustration

24

45

63

17

31

96

50

85

Apr 18, 2012

232

Mergesort: Illustration

24

45

63

85

17

Apr 18, 2012

31

96

50

233

Mergesort: Illustration

24

45

63

85

17

Apr 18, 2012

31

96

50

234

Mergesort: Illustration

24

45

63

85

17

Apr 18, 2012

31

96

50

235

Mergesort: Illustration

24

45

63

85
17

31

Apr 18, 2012

96

50

236

Mergesort: Illustration

24

45

63

85
96
17

50

31

Apr 18, 2012

237

Mergesort: Illustration

24

45

63

85
96
17

50

31

Apr 18, 2012

238

Mergesort: Illustration

24

45

63

85
17

96

50

31

Apr 18, 2012

239

Mergesort: Illustration

24

45

63

85
17

96

50

31

Apr 18, 2012

240

Mergesort: Illustration

24

45

63

85
17

Apr 18, 2012

31

96

50

241

Mergesort: Illustration

24

45

63

85
17

Apr 18, 2012

31

96

50

242

Mergesort: Illustration

24

45

63

85
17

Apr 18, 2012

31

96

50

243

Mergesort: Illustration

24

45

63

85
17

31
96

Apr 18, 2012

50

244

Mergesort: Illustration

24

45

63

85
17

31
96

Apr 18, 2012

50

245

Mergesort: Illustration

24

45

63

85
17

31

50
96

Apr 18, 2012

246

Mergesort: Illustration

24

45

63

85
17

31

50
96

Apr 18, 2012

247

Mergesort: Illustration

24

45

63

85
17

Apr 18, 2012

31

50

96

248

Mergesort: Illustration

24

45

63

85
17

Apr 18, 2012

31

50

96

249

Mergesort: Illustration

24

45

63

85
17

Apr 18, 2012

31

50

96

250

Mergesort: Illustration

24

45

63

85
17

Apr 18, 2012

31

50

96

251

Mergesort: Illustration

24

45

63

85

17
31

Apr 18, 2012

50

96

252

Mergesort: Illustration

24

45

63

85

17
31

Apr 18, 2012

50

96

253

Mergesort: Illustration

24

45

63

85

17

31
50

Apr 18, 2012

96

254

Mergesort: Illustration

24

45

63

85

17

31
50

Apr 18, 2012

96

255

Mergesort: Illustration

24

45

63

85

17

Apr 18, 2012

31

50

96

256

Mergesort: Illustration

24

45

63

85

17

Apr 18, 2012

31

50

96

257

Mergesort: Illustration

24

45

63

85

17

Apr 18, 2012

31

50

96

258

Mergesort: Illustration

24

45

63

85

17

Apr 18, 2012

31

50

96

259

Mergesort: Illustration
17
24

45

63

85

31

Apr 18, 2012

50

96

260

Mergesort: Illustration
17
24

45

63

85

31

Apr 18, 2012

50

96

261

Mergesort: Illustration
17

24
45

63

85

31

Apr 18, 2012

50

96

262

Mergesort: Illustration
17

24
45

63

85

31

Apr 18, 2012

50

96

263

Mergesort: Illustration
17

24
45

31
63

85

50

Apr 18, 2012

96

264

Mergesort: Illustration
17

24
45

31
63

85

50

Apr 18, 2012

96

265

Mergesort: Illustration
17

24

31
63

45
85

50

Apr 18, 2012

96

266

Mergesort: Illustration
17

24

31
63

45
85

50

Apr 18, 2012

96

267

Mergesort: Illustration
17

24

31
63

45

50

85

96

Apr 18, 2012

268

Mergesort: Illustration
17

24

31
63

45

50

85

96

Apr 18, 2012

269

Mergesort: Illustration
17

24

31

45

50

63

85

96

Apr 18, 2012

270

Mergesort: Illustration
17

24

31

45

50

63

85

96

Apr 18, 2012

271

Mergesort: Illustration
17

24

31

45

50

63

85
96

Apr 18, 2012

272

Mergesort: Illustration
17

24

31

45

50

63

85
96

Apr 18, 2012

273

Mergesort: Illustration
17

24

31

45

50

63

Apr 18, 2012

85

96

274

Mergesort: Illustration
17

24

31

45

50

63

Apr 18, 2012

85

96

275

Mergesort: Illustration
17

24

31

45

50

63

Apr 18, 2012

85

96

276

Mergesort: Time complexity


Best, worst, average-case
Each merge operation takes 0(k) time for 2 lists
each k/2 elements long (merged into one list k
elements long)
There will be log2n levels
1st level: 2 n/2 long lists to be merged into 1 n long list
2nd level: 4 n/4 long lists to be merged into 2 n/2 long
lists
3rd level: 8 n/8 long lists to be merged into 4 n/4 long
lists

Time Complexity: O(nlog2n)

Apr 18, 2012

277

Method mergeSort()
void mergeSort(int a [], int left, int right)
{
// sort a[left:right]
if (left < right)
{// at least two elements
int mid = (left+right)/2;
//midpoint
mergeSort(a, left, mid);
mergeSort(a, mid + 1, right);
merge(a, b, left, mid, right); //merge from a to b
copy(b, a, left, right); //copy result back to a
}
}
Apr 18, 2012

278

void merge (int a[], int b [], int left, int mid, int right) {
int i, j, k = 0;
for (i = left, j = mid + 1; i <= mid && j <= right;) {
if (a [i] < a [j]) {
b [k] = a [i];
i++;
k++;
}
else {
b [k] = a [j];
j++;
k++;
}
}
for (; i <= mid; i++)
b [k++] = a [i];
for (; j <= right; j++)
b [k++] = a [j];
}
Apr 18, 2012

279

void copy (int b [], int a [], int left, int right) {
for (int i = 0, j = left; i <= right - left; ++i, j++)
a [j] = b [i];
}

Apr 18, 2012

280

AVL tree
self-adjusting tree

AVL tree
discovers: Adelson-Velskii and Landis
balanced Binary search tree
the depth of the tree: O(lg N)
definition: An AVL tree is a binary search tree with the
additional balance property that, for any node in the tree,
the height of the left and right subtrees can be differ by at
most 1.

Apr 18, 2012

282

Balance factor (BF)

For a tree the height of left subtree is Hl, the


height of right subtree is Hr, then the BF of node T
is Hl- Hr

AVL Tree: BF(t)<2 for any node t

Apr 18, 2012

283

Insertion
case 1: an insertion into the left subtree of the
child of X
case 2: an insertion into the right subtree of
left child of X
case 3: an insertion into the left subtree of
right child of X
case 4: an insertion into the right subtree of
right child of X

Apr 18, 2012

left
the
the
the

284

case 1: single rotation left subtree


.A single rotation switches the role of the parent and the child
while maintaining the search order
rotate binary tree with left child
Node* withLeftChild (Node* k2) {
Node* k1=k2 -> left;
k2 -> left=k1 -> right;
k1 -> right=k2;
return k1;
}

Apr 18, 2012

285

case 1 (single L-L rotation)


K2

+2

+1 K1

K1 0
single rotation

K2 0

C
B

A
Apr 18, 2012

286

case 1

k2

12
8

12

k1 4 c 10 14
A 2

k1 4

16

6B

16

A 2 K2 8 14
1

6
B

10
C

1
Insert 1
Apr 18, 2012

287

case 2: double rotation (double L-R rotation)

double rotate binary tree node: first left child with its right child;
then, node k3 with new left child.
Node* doubleWithLeftChild(Node* k3) {
k3 -> left=withRightChild(k3 -> left);
return withLeftChild(k3);
}

Apr 18, 2012

288

case 2: double rotation

K3

+2

K3

K1 -1
K2

K2

K2

K1

D
A

A
B

K3

K1
D

B
Apr 18, 2012

289

case 2: double rotation


12
k3 8

12

k1 4 D10 14
A 2

B 5

K2 6

16

k2

16

k1 4 K3 8 14
2
A

10

B C D

Insert 5
Apr 18, 2012

290

case 3:double rotation (double R-L rotation)

double rotate binary tree node: first right child with its left
child; then, node k1 with new right child.
Node* doubleWithRightChild (Node* k1) {
k1 -> right=withLeftChild(k1 -> right);
return withRightChild(k1);
}

Apr 18, 2012

291

case 3
K1 -2

K1
K2

K3 +1

K2

K3

K1
K3

A
D

K2

B
C

D
Apr 18, 2012

292

R-L type

80
0
70

-2
95

+1/-1 90

80
+1
70
99

85 92

90

90
85

80
95
92

99

95

70 85 92

Insert 85 or 92
Apr 18, 2012

293

99

case 4: single rotation right subtree


rotate binary tree with right child
Node* withRightChild (Node* k1) {
Node* k2=k1 -> right;
k1 -> right = k2 -> left;
k2 -> left = k1;
return k2;
}

Apr 18, 2012

294

case 4 (single R-R rotation)


K1 -2

-1 K2

K2 0
single rotation

K1

A
B

C
Apr 18, 2012

295

R-R type

80
k1

95

70
k2

80

73

95

73
70

76

76
Insert 76
Apr 18, 2012

296

Bucket Sort
Given that, we have N integers in the range 0 to
M-1
Maintain an array count of size M, which is
initialized to zero. Thus, count has M cells
(buckets).
Read Ai
Increment count[Ai] by 1
After all the input array is read, scan the count
array, printing out a representation of sorted list.

Apr 18, 2012

297

Radix Sort
Radix sort is generalization of bucket sort.
It uses several passes of bucket sort
Perform the bucket sorts by least
significant digits
First sort by digit in units place
Second sort by digit in tens place
Third sort by digit in hundreds place
.

Apr 18, 2012

298

Lets sort the following array using radix sort:

64

8 216 512 27 729 0

1 343 125

Apr 18, 2012

299

Pass 1

Apr 18, 2012

300

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

301

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

302

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

303

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4

64

5
6
7
8
9
Apr 18, 2012

304

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4

64

5
6
7
8
9
Apr 18, 2012

305

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4

64

5
6
7
8

9
Apr 18, 2012

306

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4

64

5
6
7
8

9
Apr 18, 2012

307

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4

64

5
6

216

7
8

9
Apr 18, 2012

308

64

8 216 512 27 729 0

1 343 125

0
1
2
3
4

64

5
6

216

7
8

9
Apr 18, 2012

309

64

8 216 512 27 729 0

1 343 125

0
1
2

512

3
4

64

5
6

216

7
8

9
Apr 18, 2012

310

64

8 216 512 27 729 0

1 343 125

0
1
2

512

3
4

64

5
6

216

7
8

9
Apr 18, 2012

311

64

8 216 512 27 729 0

1 343 125

0
1
2

512

3
4

64

5
6

216

27

9
Apr 18, 2012

312

64

8 216 512 27 729 0

1 343 125

0
1
2

512

3
4

64

5
6

216

27

9
Apr 18, 2012

313

64

8 216 512 27 729 0

1 343 125

0
1
2

512

3
4

64

5
6

216

27

729
Apr 18, 2012

314

64

8 216 512 27 729 0

1 343 125

0
1
2

512

3
4

64

5
6

216

27

729
Apr 18, 2012

315

64
0

8 216 512 27 729 0

1 343 125

1
2

512

3
4

64

5
6

216

27

729
Apr 18, 2012

316

64
0

8 216 512 27 729 0

1 343 125

1
2

512

3
4

64

5
6

216

27

729
Apr 18, 2012

317

64
0

512

8 216 512 27 729 0

1 343 125

3
4

64

5
6

216

27

729
Apr 18, 2012

318

64
0

512

8 216 512 27 729 0

1 343 125

3
4

64

5
6

216

27

729
Apr 18, 2012

319

64
0

512

343

64

8 216 512 27 729 0

1 343 125

5
6

216

27

729
Apr 18, 2012

320

64
0

512

343

64

8 216 512 27 729 0

1 343 125

5
6

216

27

729
Apr 18, 2012

321

64
0

512

343

64

125

216

27

729

8 216 512 27 729 0

Apr 18, 2012

1 343 125

322

64
0

512

343

64

125

216

27

729

8 216 512 27 729 0

Apr 18, 2012

1 343 125

323

512

343

64

125

216

27

729
Apr 18, 2012

324

512

343

64

125

216

27

729
Apr 18, 2012

325

0
0

512

343

64

125

216

27

729
Apr 18, 2012

326

0
0
1

512

343

64

125

216

27

729
Apr 18, 2012

327

0
0
1

512

343

64

125

216

27

729
Apr 18, 2012

328

0
1

512

343

64

125

216

27

729
Apr 18, 2012

329

0
1
2

512

343

64

125

216

27

729
Apr 18, 2012

330

1 512

0
1
2

512

343

64

125

216

27

729
Apr 18, 2012

331

1 512

0
1
2
3

343

64

125

216

27

729
Apr 18, 2012

332

Pass 2

Apr 18, 2012

333

1 512 343 64 125 216 27

8 729

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

334

00 1 512 343 64 125 216 27

8 729

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

335

0
0

1 512 343 64 125 216 27

8 729

1
2
3
4
5
6
7
8
9
Apr 18, 2012

336

0
0

01 512 343 64 125 216 27

8 729

1
2
3
4
5
6
7
8
9
Apr 18, 2012

337

0
0

1 512 343 64 125 216 27

8 729

1
2
3
4
5
6
7
8
9
Apr 18, 2012

338

0
0

1 512 343 64 125 216 27

8 729

1
2
3
4
5
6
7
8
9
Apr 18, 2012

339

0
0

512

1 512 343 64 125 216 27

8 729

2
3
4
5
6
7
8
9
Apr 18, 2012

340

0
0

512

1 512 343 64 125 216 27

8 729

2
3
4
5
6
7
8
9
Apr 18, 2012

341

0
0

512

1 512 343 64 125 216 27

8 729

2
3
4

343

5
6
7
8
9
Apr 18, 2012

342

0
0

512

1 512 343 64 125 216 27

8 729

2
3
4

343

5
6
7
8
9
Apr 18, 2012

343

0
0

512

1 512 343 64 125 216 27

8 729

2
3
4

343

5
6

64

7
8
9
Apr 18, 2012

344

0
0

512

1 512 343 64 125 216 27

8 729

2
3
4

343

5
6

64

7
8
9
Apr 18, 2012

345

0
0

512

125

1 512 343 64 125 216 27

8 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

346

0
0

512

125

1 512 343 64 125 216 27

8 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

347

0
0

512

216

125

1 512 343 64 125 216 27

8 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

348

0
0

512

216

125

1 512 343 64 125 216 27

8 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

349

0
0

512

216

125

27

1 512 343 64 125 216 27

8 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

350

0
0

512

216

125

27

1 512 343 64 125 216 27 08 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

351

0
0

512

216

125

27

1 512 343 64 125 216 27

8 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

352

0
0

512

216

125

27

1 512 343 64 125 216 27

8 729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

353

0
0

512

216

125

27

1 512 343 64 125 216 27

8 729

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

354

0
0

512

216

125

27

1 512 343 64 125 216 27

8 729

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

355

512

216

125

27

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

356

0
0

512

216

125

27

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

357

0
1

512

216

125

27

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

358

8 512 216

0
1

512

216

125

27

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

359

8 512 216

0
1
2

125

27

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

360

8 512 216 125 27 729

0
1
2

125

27

729

3
4

343

5
6

64

7
8
9
Apr 18, 2012

361

8 512 216 125 27 729

0
1
2
3
4

343

5
6

64

7
8
9
Apr 18, 2012

362

8 512 216 125 27 729 343

0
1
2
3
4

343

5
6

64

7
8
9
Apr 18, 2012

363

8 512 216 125 27 729 343

0
1
2
3
4
5
6

64

7
8
9
Apr 18, 2012

364

8 512 216 125 27 729 343 64

0
1
2
3
4
5
6

64

7
8
9
Apr 18, 2012

365

Pass 3

Apr 18, 2012

366

8 512 216 125 27 729 343 64

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

367

000 1

8 512 216 125 27 729 343 64

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

368

0
0

8 512 216 125 27 729 343 64

1
2
3
4
5
6
7
8
9
Apr 18, 2012

369

0 001 8 512 216 125 27 729 343 64


0

1
2
3
4
5
6
7
8
9
Apr 18, 2012

370

0
0

8 512 216 125 27 729 343 64

1
2
3
4
5
6
7
8
9
Apr 18, 2012

371

0
0

1 008 512 216 125 27 729 343 64

1
2
3
4
5
6
7
8
9
Apr 18, 2012

372

0
0

8 512 216 125 27 729 343 64

1
2
3
4
5
6
7
8
9
Apr 18, 2012

373

0
0

8 512 216 125 27 729 343 64

1
2
3
4
5
6
7
8
9
Apr 18, 2012

374

0
0

8 512 216 125 27 729 343 64

1
2
3
4
5

512

6
7
8
9
Apr 18, 2012

375

0
0

8 512 216 125 27 729 343 64

1
2
3
4
5

512

6
7
8
9
Apr 18, 2012

376

0
0

8 512 216 125 27 729 343 64

1
2

216

3
4
5

512

6
7
8
9
Apr 18, 2012

377

0
0

8 512 216 125 27 729 343 64

1
2

216

3
4
5

512

6
7
8
9
Apr 18, 2012

378

0
0

125

216

8 512 216 125 27 729 343 64

3
4
5

512

6
7
8
9
Apr 18, 2012

379

0
0

125

216

8 512 216 125 027 729 343 64

3
4
5

512

6
7
8
9
Apr 18, 2012

380

0
0

125

216

8 512 216 125 27 729 343 64

27

3
4
5

512

6
7
8
9
Apr 18, 2012

381

0
0

125

216

8 512 216 125 27 729 343 64

27

3
4
5

512

6
7
8
9
Apr 18, 2012

382

0
0

125

216

8 512 216 125 27 729 343 64

27

3
4
5

512

6
7

729

8
9
Apr 18, 2012

383

0
0

125

216

8 512 216 125 27 729 343 64

27

3
4
5

512

6
7

729

8
9
Apr 18, 2012

384

0
0

125

216

343

8 512 216 125 27 729 343 64

27

4
5

512

6
7

729

8
9
Apr 18, 2012

385

0
0

125

216

343

8 512 216 125 27 729 343 064

27

4
5

512

6
7

729

8
9
Apr 18, 2012

386

0
0

125

216

343

1
27

8 512 216 125 27 729 343 64


64

4
5

512

6
7

729

8
9
Apr 18, 2012

387

0
0

125

216

343

1
27

8 512 216 125 27 729 343 64


64

4
5

512

6
7

729

8
9
Apr 18, 2012

388

125

216

343

27

64

4
5

512

6
7

729

8
9
Apr 18, 2012

389

0
0

125

216

343

1
27

27 64
64

4
5

512

6
7

729

8
9
Apr 18, 2012

390

27 64

0
1

125

216

343

4
5

512

6
7

729

8
9
Apr 18, 2012

391

27 64 125

0
1

125

216

343

4
5

512

6
7

729

8
9
Apr 18, 2012

392

27 64 125

0
1
2

216

343

4
5

512

6
7

729

8
9
Apr 18, 2012

393

27 64 125 216

0
1
2

216

343

4
5

512

6
7

729

8
9
Apr 18, 2012

394

27 64 125 216

0
1
2
3

343

4
5

512

6
7

729

8
9
Apr 18, 2012

395

27 64 125 216 343

0
1
2
3

343

4
5

512

6
7

729

8
9
Apr 18, 2012

396

27 64 125 216 343

0
1
2
3
4
5

512

6
7

729

8
9
Apr 18, 2012

397

27 64 125 216 343 512

0
1
2
3
4
5

512

6
7

729

8
9
Apr 18, 2012

398

27 64 125 216 343 512

0
1
2
3
4
5
6
7

729

8
9
Apr 18, 2012

399

27 64 125 216 343 512 729

0
1
2
3
4
5
6
7

729

8
9
Apr 18, 2012

400

27 64 125 216 343 512 729

0
1
2
3
4
5
6
7
8
9
Apr 18, 2012

401

The End

Apr 18, 2012

402

You might also like