• No results found

Insertion Sort

N/A
N/A
Protected

Academic year: 2020

Share "Insertion Sort"

Copied!
205
0
0

Loading.... (view fulltext now)

Full text

(1)

Insertion Sort

while some elements unsorted:

Using linear search, find the location in the sorted portion

where the 1

st

element of the unsorted portion should be

inserted

Move all the elements after the insertion location up one

position to make space for the new element

13 21 45 79 47 22 38 60 66 74 36 94 57 16 29 81 45 66 60 45

(2)
(3)
(4)

Insertion Sort Algorithm

public void insertionSort(Comparable[] arr) {

for (int i = 1; i < arr.length; ++i) { Comparable temp = arr[i];

int pos = i;

// Shuffle up all sorted items > arr[i]

while (pos > 0 &&

arr[pos-1].compareTo(temp) > 0) { arr[pos] = arr[pos–1];

pos--;

} // end while

// Insert the current item arr[pos] = temp;

} }

(5)

public void insertionSort(Comparable[] arr) {

for (int i = 1; i < arr.length; ++i) { Comparable temp = arr[i];

int pos = i;

// Shuffle up all sorted items > arr[i]

while (pos > 0 &&

arr[pos-1].compareTo(temp) > 0) { arr[pos] = arr[pos–1];

pos--;

} // end while

// Insert the current item arr[pos] = temp;

} }

Insertion Sort Analysis

outer loop outer times

inner loop

(6)

Insertion Sort: Number of

Comparisons

# of Sorted

Elements

Best case

Worst case

0

0

0

1

1

1

2

1

2

n-1

1

n-1

n-1

n(n-1)/2

(7)

Insertion Sort: Cost Function

1 operation to initialize the outer loop

The outer loop is evaluated n-1 times

● 5 instructions (including outer loop comparison and increment) ● Total cost of the outer loop: 5(n-1)

How many times the inner loop is evaluated is affected by the

state of the array to be sorted

Best case: the array is already completely sorted so no “shifting”

of array elements is required.

● We only test the condition of the inner loop once (2 operations = 1

comparison + 1 element comparison), and the body is never executed

(8)

Insertion Sort: Cost Function

Worst case: the array is sorted in reverse order (so each item

has to be moved to the front of the array)

In the i-th iteration of the outer loop, the inner loop will perform 4i+1

operations

Therefore, the total cost of the inner loop will be 2n(n-1)+n-1

Time cost:

Best case: 7(n-1)

Worst case: 5(n-1)+2n(n-1)+n-1

What about the number of moves?

Best case: 2(n-1) movesWorst case: 2(n-1)+n(n-1)/2

(9)

Insertion Sort: Average Case

Is it closer to the best case (n comparisons)?

The worst case (n * (n-1) / 2) comparisons?

It turns out that when random data is sorted, insertion sort is

usually closer to the worst case

Around n * (n-1) / 4 comparisons

● Calculating the average number of comparisons more exactly would

require us to state assumptions about what the “average” input data set looked like

● This would, for example, necessitate discussion of how items were

distributed over the array

Exact calculation of the number of operations required to perform

even simple algorithms can be challenging

(for instance, assume that each initial order of elements has the

same probability to occur)

(10)
(11)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted

(12)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted

(13)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted

(14)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted

(15)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted

(16)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted

(17)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted

(18)

Selection Sort

5

1

3

4

6

2

Comparison Data Movement Sorted Largest

(19)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted

(20)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted

(21)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted

(22)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted

(23)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted

(24)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted

(25)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted

(26)

Selection Sort

5

1

3

4

2

6

Comparison Data Movement Sorted Largest

(27)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(28)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(29)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(30)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(31)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(32)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(33)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted Largest

(34)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(35)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(36)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(37)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(38)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(39)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted Largest

(40)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(41)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(42)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(43)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted

(44)

Selection Sort

2

1

3

4

5

6

Comparison Data Movement Sorted Largest

(45)

Selection Sort

1

2

3

4

5

6

Comparison Data Movement Sorted

(46)

Selection Sort

1

2

3

4

5

6

Comparison Data Movement Sorted

DONE!

(47)
(48)

void selection_sort(int n, int list[])

{

int min, temp;

int k, l, index;

for(index = 0; index< n - 1 ; index++)

{

min = index ;

for(k = index + 1; k < n ; k ++)

{

if(list[min] > list[k])

min = k ;

}

if(min != index)

{

temp = `list [index];

list[index] = list[min];

list[min] = temp;

}

printf(" \n Step : %d :",(index+1));

for( l = 0 ; l < n; l++)

printf(" %d", list[l]);

}

printf("\n Sorted list is as follows:\n");

for( index = 0 ; index < n; index++)

printf(" %d", list[index]);

return 0;

}

(49)
(50)

Sorting

Sorting takes an unordered collection and

makes it an ordered one.

5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6

(51)

"Bubbling Up" the Largest

Element

Traverse a collection of elements

Move from the front to the end

“Bubble” the

largest value

to the end using

pair-wise comparisons and swapping

5 12 35 42 77 101 1 2 3 4 5 6

(52)

"Bubbling Up" the Largest

Element

Traverse a collection of elements

Move from the front to the end

“Bubble” the largest value to the end using

pair-wise comparisons and swapping

5 12 35 42 77 101 1 2 3 4 5 6 Swap 42 77

(53)

"Bubbling Up" the Largest

Element

Traverse a collection of elements

Move from the front to the end

“Bubble” the largest value to the end using

pair-wise comparisons and swapping

5 12 35 77 42 101 1 2 3 4 5 6 Swap 35 77

(54)

"Bubbling Up" the Largest

Element

Traverse a collection of elements

Move from the front to the end

“Bubble” the largest value to the end using

pair-wise comparisons and swapping

5 12 77 35 42 101 1 2 3 4 5 6 Swap 12 77

(55)

"Bubbling Up" the Largest

Element

Traverse a collection of elements

Move from the front to the end

“Bubble” the largest value to the end using

pair-wise comparisons and swapping

5 77 12 35 42 101 1 2 3 4 5 6 No need to swap

(56)

"Bubbling Up" the Largest

Element

Traverse a collection of elements

Move from the front to the end

“Bubble” the largest value to the end using

pair-wise comparisons and swapping

5 77 12 35 42 101 1 2 3 4 5 6 Swap 5 101

(57)

"Bubbling Up" the Largest

Element

Traverse a collection of elements

Move from the front to the end

“Bubble” the largest value to the end using

pair-wise comparisons and swapping

77 12 35 42 5 1 2 3 4 5 6 101

(58)

The “Bubble Up” Algorithm

index <- 1

last_compare_at <- n – 1

loop

exitif(index > last_compare_at)

if(A[index] > A[index + 1]) then

Swap(A[index], A[index + 1])

endif

index <- index + 1

endloop

(59)

No, Swap isn’t built in.

Procedure Swap(a, b isoftype in/out

Num

)

t isoftype

Num

t <- a

a <- b

b <- t

endprocedure // Swap

LB

(60)

Items of Interest

Notice that only the largest value is

correctly placed

All other values are still out of order

So we need to repeat this process

77 12 35 42 5 1 2 3 4 5 6 101

(61)

Repeat “Bubble Up” How Many

Times?

If we have N elements…

And if each time we bubble an element,

we place it in its correct location…

Then we

repeat the “bubble up”

process N – 1 times.

This

guarantees we’ll correctly

(62)

“Bubbling” All the Elements

77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101 42 35 12 5 77 1 2 3 4 5 6 101 N - 1

(63)

Reducing the Number of

Comparisons

12 35 42 77 101 1 2 3 4 5 6 5 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101

(64)

Reducing the Number of

Comparisons

On the N

th

“bubble up”, we only need to

do

MAX-N comparisons

.

For example:

This is the 4

th

“bubble up”

MAX is 6

Thus we have

2 comparisons

to do

42 5 35 12 77 1 2 3 4 5 6 101

(65)
(66)

N is … // Size of Array

Arr_Type definesa Array[1..N] of Num

Procedure Swap(n1, n2 isoftype in/out Num)

temp isoftype Num

temp <- n1

n1 <- n2

n2 <- temp

(67)

procedure Bubblesort(A isoftype in/out Arr_Type)

to_do, index isoftype Num

to_do <- N – 1

loop

exitif(to_do = 0)

index <- 1

loop

exitif(index > to_do)

if(A[index] > A[index + 1]) then

Swap(A[index], A[index + 1])

endif

index <- index + 1

endloop

to_do <- to_do - 1

endloop

endprocedure // Bubblesort

(68)

Already Sorted Collections?

What if the collection was already

sorted?

What if only a few elements were out of

place and after a couple of “bubble

ups,” the collection was sorted?

We want to be able to

detect this

and “stop early”!

42 35 12 5 77 1 2 3 4 5 6 101

(69)

Using a Boolean “Flag”

We can use a boolean variable to

determine if any swapping occurred

during the “bubble up.”

If no swapping occurred, then we know

that the collection is already sorted!

This boolean “flag” needs to be reset after

(70)

did_swap isoftype Boolean

did_swap <- true

loop

exitif

(

(to_do = 0)

OR NOT(did_swap))

index <- 1

did_swap <- false

loop

exitif(index > to_do)

if(A[index] > A[index + 1]) then

Swap(A[index], A[index + 1])

did_swap <- true

endif

index <- index + 1

endloop

to_do <- to_do - 1

endloop

(71)

An Animated Example

67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 N 8 did_swap true

(72)

An Animated Example

67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 did_swap false

(73)

An Animated Example

67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap false

(74)

An Animated Example

67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap true

(75)

An Animated Example

67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 did_swap true

(76)

An Animated Example

67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true

(77)

An Animated Example

67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true

(78)

An Animated Example

67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 did_swap true

(79)

An Animated Example

67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true

(80)

An Animated Example

67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true

(81)

An Animated Example

67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 did_swap true

(82)

An Animated Example

67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true

(83)

An Animated Example

67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true

(84)

An Animated Example

67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 did_swap true

(85)

An Animated Example

67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true

(86)

An Animated Example

98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true

(87)

An Animated Example

98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 did_swap true

(88)

An Animated Example

98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true

(89)

An Animated Example

33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true

(90)

An Animated Example

33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 did_swap true

(91)

An Animated Example

33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true

(92)

An Animated Example

33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true

(93)

After First Pass of Outer Loop

33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 7 8 N 8

Finished first “Bubble Up”

(94)

The Second “Bubble Up”

33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false

(95)

The Second “Bubble Up”

33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false No Swap

(96)

The Second “Bubble Up”

33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false

(97)

The Second “Bubble Up”

33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false Swap

(98)

The Second “Bubble Up”

33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap true Swap

(99)

The Second “Bubble Up”

33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true

(100)

The Second “Bubble Up”

33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap

(101)

The Second “Bubble Up”

33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap

(102)

The Second “Bubble Up”

33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true

(103)

The Second “Bubble Up”

33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true No Swap

(104)

The Second “Bubble Up”

33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true

(105)

The Second “Bubble Up”

33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap

(106)

The Second “Bubble Up”

67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap

(107)

The Second “Bubble Up”

67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true

(108)

The Second “Bubble Up”

67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap

(109)

The Second “Bubble Up”

42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap

(110)

After Second Pass of Outer

Loop

42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 6 7 N 8 did_swap true

(111)

The Third “Bubble Up”

42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false

(112)

The Third “Bubble Up”

42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false Swap

(113)

The Third “Bubble Up”

42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap true Swap

(114)

The Third “Bubble Up”

42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true

(115)

The Third “Bubble Up”

42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap

(116)

The Third “Bubble Up”

42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap

(117)

The Third “Bubble Up”

42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true

(118)

The Third “Bubble Up”

42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true No Swap

(119)

The Third “Bubble Up”

42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true

(120)

The Third “Bubble Up”

42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap

(121)

The Third “Bubble Up”

42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap

(122)

The Third “Bubble Up”

42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true

(123)

The Third “Bubble Up”

42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap

(124)

The Third “Bubble Up”

45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap

(125)

After Third Pass of Outer Loop

45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 6 N 8 did_swap true

(126)

The Fourth “Bubble Up”

45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false

(127)

The Fourth “Bubble Up”

45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false Swap

(128)

The Fourth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap true Swap

(129)

The Fourth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true

(130)

The Fourth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true No Swap

(131)

The Fourth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true

(132)

The Fourth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true No Swap

(133)

The Fourth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true

(134)

The Fourth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true No Swap

(135)

After Fourth Pass of Outer

Loop

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 5 N 8 did_swap true

(136)

The Fifth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false

(137)

The Fifth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false No Swap

(138)

The Fifth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false

(139)

The Fifth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false No Swap

(140)

The Fifth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false

(141)

The Fifth “Bubble Up”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false No Swap

(142)

After Fifth Pass of Outer Loop

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false

(143)

Finished “Early”

45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed.

We can “skip” the last two passes of the outer loop.

(144)

Summary

“Bubble Up” algorithm will

move largest

value to its correct location

(to the right)

Repeat “Bubble Up” until all elements are

correctly placed:

Maximum of N-1 times

Can finish early if

no swapping

occurs

We reduce the number of elements we

(145)

Truth in CS Act

NOBODY EVER USES BUBBLE SORT

NOBODY

NOT EVER

BECAUSE IT IS EXTREMELY INEFFICIENT

(146)
(147)

Sorting

Sorting takes an unordered collection and

makes it an ordered one.

5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6

(148)

Divide and Conquer

Divide and Conquer cuts the problem in half

each time, but

uses the result of both halves

:

cut the problem in half until the problem is

trivial

solve for both halves

(149)

Mergesort

A divide-and-conquer algorithm:

Divide the unsorted array into 2 halves until the

sub-arrays only contain one element

Merge the sub-problem solutions together:

Compare the sub-array’s 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

(150)

How to Remember Merge Sort?

That’s easy. Just remember Mariah Carey.

As a singing star, Ms. Carey has perfected the “wax-on” wave

motion--a clockwise sweep of her hand used to emphasize lyrics.

The Maria

“Wax-on” Angle:

ƒ

(

θ

,t)

(151)

How To Remember Merge Sort?

Just as Mariah recursively moves her

hands into smaller circles, so too does

merge sort recursively split an array

into smaller segments.

ƒ

(

θ

,t)

We need two such recursions,

one for each half of the split

array.

(152)

Algorithm

Mergesort(Passed an array)

if array size > 1

Divide array in half

Call Mergesort on first half.

Call Mergesort on second half.

Merge two halves.

Merge(Passed two arrays)

Compare leading element in each array

Select lower and place in new array.

(If one input array is empty then place

(153)

More TRUTH in CS

We don’t really pass in two arrays!

We pass in one array with indicator variables

which tell us where one set of data starts and

finishes and where the other set of data starts

and finishes.

Honest.

s1 f1 s2 f2

(154)

Algorithm

Mergesort(Passed an array)

if array size > 1

Divide array in half

Call Mergesort on first half.

Call Mergesort on second half.

Merge two halves.

Merge(

Passed two arrays

)

Compare leading element in each array

Select lower and place in new array.

(If one input array is empty then place

remainder of other array in output array)

(155)

67 45

23 14 6 33

(156)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42

(157)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98

(158)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98

(159)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 Merge

(160)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 23 Merge

(161)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 23 98 Merge

(162)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 23 98

(163)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98

(164)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 14 Merge 23 98

(165)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 45 Merge 23 98 14

(166)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 98 14 45 23

(167)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 98 14 14 23 45

(168)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 14 14 23 98 45

(169)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98 14 45 14 23 45

(170)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98 14 45 14 23 45 98

(171)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 23 98 14 45 14 23 45 98

(172)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 23 98 14 45 14 23 45 98

(173)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 Merge 23 98 14 45 14 23 45 98

(174)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 6 Merge 23 98 14 45 14 23 45 98

(175)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 67 Merge 23 98 14 45 6 14 23 45 98

(176)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 23 98 14 45 6 67 14 23 45 98

(177)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 14 23 45 98

(178)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 33 23 98 14 45 6 67 14 23 45 98

(179)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 42 23 98 14 45 6 67 33 14 23 45 98

(180)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 45 98

(181)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 33 42 14 23 45 98 6 67

(182)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 33 14 23 45 98 6 33 67 42

(183)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 33 42 14 23 45 98 6 33 42 67

(184)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67

(185)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 23 45 98 33 42 67 14 6

(186)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 23 45 98 6 42 67 6 14 33

(187)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 45 98 6 42 67 6 14 23 33

(188)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 98 6 42 67 6 14 23 45 33

(189)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 98 6 33 67 6 14 23 33 45 42

(190)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 98 6 33 42 6 14 23 33 42 45 67

(191)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 45 6 33 42 6 14 23 33 42 45 98 67

(192)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67

(193)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98

(194)

67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98

(195)

67 45

23 14 6 33

98 42

(196)

Summary

Divide

the unsorted collection

into two

Until the sub-arrays only

contain one element

Then

merge the sub-problem solutions

(197)
(198)

O-notation Introduction

Exact counting of operations is often difficult (and

tedious), even for simple algorithms

Often, exact counts are not useful due to other

factors, e.g. the language/machine used, or the

implementation of the algorithm (different types of

operations do not take the same time anyway)

O-notation is a mathematical language for

evaluating the running-time (and memory usage) of

algorithms

(199)

Growth Rate of an Algorithm

We often want to compare the performance of

algorithms

When doing so we generally want to know how they

perform when the problem size (n) is large

Since cost functions are complex, and may be

difficult to compute, we approximate them using O

notation

(200)

Example of a Cost Function

Cost Function: t

A

(n) = n

2

+ 20n + 100

Which term dominates?

It depends on the size of n

n = 2, t

A

(n) = 4 + 40 + 100

The constant, 100, is the dominating term

n = 10, t

A

(n) = 100 + 200 + 100

20n is the dominating term

n = 100, t

A

(n) = 10,000 + 2,000 + 100

n

2

is the dominating term

n = 1000, t

A

(n) = 1,000,000 + 20,000 + 100

References

Related documents

In this post hoc analysis derived from a prospective, mul- ticenter cohort study of 1011 critically ill adults patients with hematologic malignancies admitted to ICU, direct

of large pack goods by using sales promotion tactics like price off and percentage extra...

We confirmed that IMP-68 exhibited weaker imipenem- hydrolyzing activity than that for other carbapenems, demonstrating that the antimi- crobial susceptibility profile of

In our evaluation samples Asian paint orange and British paint snow white were within the limit of lead content.. Lead in soils ranges from 5.6925 to

of Pulmonary, allergy and Critical Care Medicine, Department of Internal Medicine, seoul st Mary’s hospital, College of Medicine, The Catholic University of Korea, seoul, 3

The aim of this study was to investigate the efficacy of a combined treatment of tubastatin A (TUB-A) and palladium nanoparticles (PdNPs) against MDA-MB-231 human breast cancer

Abstract: The purpose of this study consists to verify if Er:YAG laser, at low fluences, is able to debond porcelain veneers, successfully used to improve anterior tooth