• No results found

Selection Sort

N/A
N/A
Protected

Academic year: 2020

Share "Selection Sort"

Copied!
54
0
0

Loading.... (view fulltext now)

Full text

(1)

1

CSD 205 – Design and Analysis Algorithm

(2)

Types of Sorting Algorithms

• 

Non-recursive/Incremental comparison sorting

•  Selection sort

•  Bubble sort

•  Insertion sort

• 

Recursive comparison sorting

•  Merge sort

•  Quick sort

•  Heap sort

• 

Non-comparison linear sorting

•  Count sort

•  Radix sort

(3)

Selection Sort

• 

Idea:

Find the smallest element in the array

Exchange it with the element in the first position

Find the second smallest element and exchange it with the

element in the second position

Continue until the array is sorted

(4)

Alg.:

SELECTION-SORT(A)

n

length[A]

for j

0

to n - 2

smallest

j

for i

j + 1

to n-1

if A[i] < A[smallest]

smallest

i

exchange

A[j]

A[smallest]

Cost Steps

c

1

1

c

2

n-1

c

3

n-1

c

4

c

5

c

6

c

7

n-1

− = 2 0 n j j t ) 1 ( ) 1 ( ) 1 ( )

( 2 7

0 6 2 0 5 2 0 4 3 2

1 + − + − + + + + −

=

− = − = − = n c t c t c t c n c n c c n T n j j n j j n j j 4

− = 2 0 n j j t

− = 2 0 n j j t

Selection Sort Analysis

(5)

Best/Worst/Average Case Analysis

T(n) =

Θ

(n

2

)

Disadvantage:

Θ(n2) running time in all cases

c

bn

an

+

+

=

2 2 ) 1 ( 2 0 − =

− = n n j n j ) 1 ( 2 ) 1 ( 2 ) 1 ( 2 ) 1 ( ) 1 ( ) 1 ( )

(n = c1 +c2 n − +c3 n − +c4 n n − + c5 n n − +c6 n n − +c7 n

T 5 ) 1 ( ) 1 ( ) 1 ( )

( 2 7

0 6 2 0 5 2 0 4 3 2

1 + − + − + + + + −

(6)

Bubble Sort

Given a list of N elements, repeat the following steps N-1 times:

•  For each pair of adjacent numbers, if number on the left is greater than the

number on the right, swap them.

•  “Bubble” the largest value to the end using pair-wise comparisons and swapping.

6

14

8

14

14

8

8

17

17

22

22

17

14

8

12

6

6

14

22

14

12

22

17

17

8

22

14

8

14

22

8

22

8

22

6

12

12

6

6

12

12

6

17

17

8

12

12

8

8

12

8

12

8

14

14

17

17

22

22

12

6

6

12

8

14

14

17

22

22

(7)

BubbleSort(A)

1.

n = Length[A];

2.

for j = 0 to n-2

3.

for i = 0 to n-j-2

4.

if A[i] > A[i+1]

5.

temp = A[i]

6.

A[i] = A[i+1]

7.

A[i+1] = temp

8.

return A;

7

cost times

c

1

1

 

c

2

  

n-1

c

3

c

4

c

5

c

6

c

7

c

8

1

−=

2 0

n j tj

8 2 0 7 2 0 6 2 0 5 2 0 4 2 0 3 2

1 ( 1)

)

(n c c n c t c t c t c t c t c

T n j j n j j n j j n j j n j

j + + + + +

+ − + =

− = − = − = − = − =

tj: # of times the inner for loop statement is executed at iteration j

−= 2 0

n j t j

−=

2 0

n j t j

−=

2 0 n

j t j

=−

2 0

n j t j

(8)

Best/Worst/Average Case Analysis

Since we don’t know at which iteration the list is completely

sorted, the nested loop will execute completely.

a quadratic function of n

T(n) =

Θ

(n

2

)

order of growth in

n

2

8 7 6 5 4 3 2 1 2 ) 1 ( 2 ) 1 ( 2 ) 1 ( 2 ) 1 ( 2 ) 1 ( ) 1 ( )

(n c c n c n n c n n c n n c n n c n n c

T = + − + − + − + − + − + − +

c

bn

an

+

+

=

2

−= − = 2 0 2 ) 1 ( n j n n j 8 8 2 0 7 2 0 6 2 0 5 2 0 4 2 0 3 2

1 ( 1)

)

(n c c n c t c t c t c t c t c

T n j j n j j n j j n j j n j

j + + + + +

(9)

Bubble Sort Modified

BubbleSort(A)

1.

n = Length[A];

2.

for j = 0 to n-2

3.

swap = 0;

4.

for i = 0 to n-j-2

5.

if A[i] > A[i+1]

6.

temp = A[i]

7.

A[i] = A[i+1]

8.

A[i+1] = temp

swap = swap + 1;

9.

if swap = 0

10.

break;

(10)

Best Case Analysis (Modified)

•  The array is already sorted

•  Keep track of no. of swaps done in an iteration

•  Can finish early if no swapping occurs

•  tj = 1 è n – 1 comparisons in a single iteration

T(n) = c1 + c2(n -1) + c3(n -1) + c4(n -1) + c8 = (c2 + c3 + c4)n + (c1 – c2 – c3 – c4 + c8)

= an + b = Θ(n)

10 8 2 0 7 2 0 6 2 0 5 2 0 4 2 0 3 2

1 ( 1)

)

(n c c n c t c t c t c t c t c

T n j j n j j n j j n j j n j

j + + + + +

(11)

Worst Case Analysis (Modified)

The array is in reverse sorted order

•  Always A[i] > A[i+1]

a quadratic function of n

T(n) =

Θ

(n

2

)

order of growth in n2

c

bn

an

+

+

=

2 11

−= − = 2 0 2 ) 1 ( n j n n j 8 7 6 5 4 3 2 1 2 ) 1 ( 2 ) 1 ( 2 ) 1 ( 2 ) 1 ( 2 ) 1 ( ) 1 ( )

(n c c n c n n c n n c n n c n n c n n c

T = + − + − + − + − + − + − +

8 2 0 7 2 0 6 2 0 5 2 0 4 2 0 3 2

1 ( 1)

)

(n c c n c t c t c t c t c t c

T n j j n j j n j j n j j n j

j + + + + +

+ − + =

− = − = − = − = − = Advantages

Simple to code

Requires little memory (in-place)

Disadvantages

Θ(n2) running time in worst & average case

Nobody “EVER” uses bubble sort

(12)

Insertion Sort

• 

Idea: like sorting a hand of playing cards

Start with empty left hand and cards face down on the table.

Remove one card at a time from the table, and insert it into the

correct position in the left hand

•  compare it with each card already in the hand, from right to left

The cards held in the left hand are sorted

•  these cards were originally the top cards of the pile on the table

(13)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

13

Insertion Sort

0.56 1.12 1.17 0.32

2.78 7.42 3.14 7.71

Value 6.21 4.42

Iteration 0: step 0.

2 3 4 5

0 1 8 9

(14)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

14

Insertion Sort

0.56 1.12 1.17 0.32

2.78 7.42 3.14 7.71

Value 6.21 4.42

Iteration 1: step 0.

2 3 4 5

0 1 8 9

(15)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

15

Insertion Sort

0.56 1.12 1.17 0.32

2.78 7.42 3.14 7.71

Value 6.21 4.42

Iteration 2: step 0.

0.56 7.42

2 3 4 5

0 1 8 9

(16)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

16

Insertion Sort

7.42 1.12 1.17 0.32

2.78 0.56 3.14 7.71

Value 6.21 4.42

Iteration 2: step 1.

0.56 2.78

2 3 4 5

0 1 8 9

(17)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

17

Insertion Sort

7.42 1.12 1.17 0.32

2.78 0.56 3.14 7.71

Value 6.21 4.42

Iteration 2: step 2.

0.56 2.78

2 3 4 5

0 1 8 9

(18)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

18

Insertion Sort

7.42 1.12 1.17 0.32 2.78

0.56 3.14 7.71

Value 6.21 4.42

Iteration 3: step 0.

1.12 7.42

2 3 4 5

0 1 8 9

(19)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

19

Insertion Sort

7.42

1.12 1.17 0.32

2.78

0.56 3.14 7.71

Value 6.21 4.42

Iteration 3: step 1.

1.12 2.78

2 3 4 5

0 1 8 9

(20)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

20

Insertion Sort

7.42

1.12 1.17 0.32 2.78

0.56 3.14 7.71

Value 6.21 4.42

Iteration 3: step 2.

1.12 2.78

2 3 4 5

0 1 8 9

(21)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

21

Insertion Sort

7.42

1.12 2.78 1.17 0.32

0.56 3.14 7.71

Value 6.21 4.42

Iteration 4: step 0.

1.17 7.42

2 3 4 5

0 1 8 9

(22)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

22

Insertion Sort

7.42

1.12 2.78 1.17 0.32

0.56 3.14 7.71

Value 6.21 4.42

2 3 4 5

0 1 8 9

Array index 6 7

Iteration 4: step 1.

(23)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

23

Insertion Sort

7.42

1.12 1.17 2.78 0.32

0.56 3.14 7.71

Value 6.21 4.42

Iteration 4: step 2.

2 3 4 5

0 1 8 9

(24)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

24

Insertion Sort

7.42

1.12 1.17 2.78 0.32

0.56 3.14 7.71

Value 6.21 4.42

Iteration 5: step 0.

0.32 7.42

2 3 4 5

0 1 8 9

(25)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

25

Insertion Sort

7.42 1.12 1.17 2.78 0.32

0.56 3.14 7.71

Value 6.21 4.42

Iteration 5: step 1.

0.32 2.78

2 3 4 5

0 1 8 9

(26)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

26

Insertion Sort

7.42 1.12 1.17 0.32 2.78

0.56 3.14 7.71

Value 6.21 4.42

Iteration 5: step 2.

0.32 1.17

2 3 4 5

0 1 8 9

(27)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

27

Insertion Sort

7.42 1.12 0.32 1.17 2.78

0.56 3.14 7.71

Value 6.21 4.42

Iteration 5: step 3.

0.32 1.12

2 3 4 5

0 1 8 9

(28)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

28

Insertion Sort

7.42 1.12 1.17

0.32 2.78

0.56 3.14 7.71

Value 6.21 4.42

Iteration 5: step 4.

0.32 0.56

2 3 4 5

0 1 8 9

(29)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

29

Insertion Sort

7.42 1.12 1.17

0.32 2.78

0.56 3.14 7.71

Value 6.21 4.42

Iteration 5: step 5.

0.32 0.56

2 3 4 5

0 1 8 9

(30)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

30

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 6.21 4.42

Iteration 6: step 0.

6.21 7.42

2 3 4 5

0 1 8 9

(31)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

31

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 6.21 4.42

Iteration 6: step 1.

6.21 7.42

2 3 4 5

0 1 8 9

(32)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

32

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 6.21 4.42

Iteration 7: step 0.

4.42 7.42

2 3 4 5

0 1 8 9

(33)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

33

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 6.21 4.42

Iteration 7: step 1.

4.42 6.21

2 3 4 5

0 1 8 9

(34)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

34

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 6.21 4.42

Iteration 7: step 2.

4.42 6.21

2 3 4 5

0 1 8 9

(35)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

35

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 4.42 6.21

Iteration 8: step 0.

3.14 7.42

2 3 4 5

0 1 8 9

(36)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

36

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 4.42 6.21

Iteration 8: step 1.

3.14 6.21

2 3 4 5

0 1 8 9

(37)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

37

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 4.42 6.21

Iteration 8: step 2.

3.14 4.42

2 3 4 5

0 1 8 9

(38)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

38

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 4.42 6.21

Iteration 8: step 3.

3.14 4.42

2 3 4 5

0 1 8 9

(39)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

39

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 4.42 6.21

Iteration 9: step 0.

2 3 4 5

0 1 8 9

(40)

InsertionSort(A, n)

1.  for i = 1 to n-1

2. key = A[i]

3. j = i – 1  

4. while (j > 0) and (A[j] > key)

5.  A[j+1] = A[j]

6.  j = j – 1

7. A[j+1] = key

40

Insertion Sort

7.42 1.12 1.17

0.32 0.56 2.78 3.14 7.71

Value 4.42 6.21

Iteration 10: DONE!.

2 3 4 5

0 1 8 9

(41)

InsertionSort(A, n) {

for i = 1 to n-1 {

key = A[i]

j = i - 1;

while (j > 0) and (A[j] > key){

A[j+1] = A[j]

j = j – 1

}

A[j+1] = key

}

}

cost times

c

1

n-1

c

2

n-1

c

3

n-1

c

4

c

5

c

6

c

7

n-1

41

=−

1 1

n i ti

)

1

(

)

1

(

)

1

(

)

1

(

)

(

1 7

1 6 1 1 5 1 1 4 3 2

1

+

+

+

+

+

+

=

− = − = − =

n

c

t

c

t

c

t

c

n

c

n

c

n

c

n

T

n i i n i i n i i

ti: # of times the while statement is executed at iteration i

=−

1 1

n i ti

=−

1 1 n i ti

(42)

Best Case Analysis

• 

The array is already sorted in ascending order

• 

Inner loop will not be executed

• 

The number of moves: 2*(n-1)

• 

The number of key comparisons: (n-1)

T(n) = c1(n-1) + c2(n -1) + c3(n -1) + c4(n -1) + c7(n-1)

= (c1 + c2 + c3 + c4 + c7)n - (c1 + c2 + c3 + c4 + c7) = an + b = Θ(n)

42

“while

j > 0 and A[j] > key”

)

1

(

)

1

(

)

1

(

)

1

(

)

(

1 7

1 6 1 1 5 1 1 4 3 2

1

+

+

+

+

+

+

(43)

Worst Case Analysis

The array is in reverse sorted order

• 

The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2

• 

The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2

•  Always A[j] > key in while loop test

•  Have to compare key with all elements to the left of the j-th position ⇒ compare with i-1 elements ⇒ tj = i

a quadratic function of n

T(n) = Θ(n2) order of growth in n2

) 1 ( 2 ) 1 ( ) ( ) 1 ( ) 1 ( ) 1 ( )

( 1 2 3 4 5 6 ⎟+ 7

⎠ ⎞ ⎜ ⎝ ⎛ − + + + − + − + −

= c n c n c n c c c n n c n

n T

43

c

bn

an

+

+

=

2

“while

j > 0 and A[j] > key”

2 ) 1 ( 1 1 − =

− = n n t n i i

)

1

(

)

1

(

)

1

(

)

1

(

)

(

1 7

1 6 1 1 5 1 1 4 3 2

1

+

+

+

+

+

+

(44)

• 

Running time not only depends on the size of the array but also

the contents of the array.

• 

Average-case:

è

O(n

2

)

•  We have to look at all possible initial data organizations.

• 

Advantages

•  Good running time for “almost sorted” arrays Θ(n)

• 

Disadvantages

•  Θ(n2) running time in worst and average case

•  n2/2 comparisons and exchanges 44

(45)

Types of Sorting Algorithms

• 

Non-recursive/incremental comparison sorting

•  Selection sort

•  Bubble sort

•  Insertion sort

• 

Recursive comparison sorting

•  Merge sort

•  Quick sort

• 

Non-comparison linear sorting

•  Count sort

•  Radix sort

•  Bucket sort

(46)

Recursive Comparison Sorting

There are many ways to design algorithms:

Insertion/bubble/selection sort uses an incremental approach

•  Having sorted to array A[i..j]

•  We insert the single element A[j+1] into its proper place, to get a sorted array A[i..j+1]

An alternative design approach is

“Divide and Conquer”

•  Divide the problems into a number of sub-problems.

•  Conquer the sub-problems by solving them recursively. If sub-problem sizes are small enough, just solve them in a straight forward manner.

•  Combine the solutions to the sub-problems into the solution for the original

(47)

Merge Sort

•  Merge sort: orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining

•  Invented by John von Neumann in 1945

•  More specifically:

•  Divide the n element sequence to be sorted into two subsequences

of n/2 elements each.

•  Conquer: Sort the two subsequences to produce the sorted answer.

•  Combine: Merge the two sorted sub sequences to produce the

sorted answer.

47

sort merge(0, n/2, n-1) sort

(48)

Base Case: When the sequences to be sorted has length 1.

108 56

14

89

12

34

66

Unsorted

108 56

14

66

Divide

108

66

Divide

66

Divide

66

BCase

66

Merge

108

Divide

108

BCase

108

Merge

66

108

Merge

56

14

Divide

56

Divide

56

BCase

56

Merge

14

Divide

14

BCase

14

Merge

14

56

Merge

56

66 108

14

Merge

12

89

34

Divide

12

89

Divide

89

Divide

89

BCase

89

Merge

12

Divide

12

BCase

12

Merge

89

12

Merge

34

Divide

34

BCase

34

Merge

34

12

89

Merge

14

34

56

66

89

108

12

Sorted

48

(49)

Merge Function

Given two sorted arrays, merge operation produces a sorted

array with all the elements of the two arrays

Temporary array is used to store the sorted elements from the

two arrays

Running time of merge: O(n), where n is the number of

elements in the merged array.

A

6

13 18 21

B

4

8

9

20

C

4

6

8

9

13 18 20 21

(50)

Merge Sort Pseudo Code

(51)

Merge Sort Pseudo Code (2)

(52)

Merge Sort Analysis

Let T(n) be the time taken by this algorithm to sort an array of n

elements dividing A into sub-arrays A

1

and A

2

. Merge operation

takes the linear time. Consequently,

T(n) = T(n/2) + T(n/2) + θ(n)

T(n) = 2T (n/2) + θ(n)

The above recurrence relation is non-homogenous and can be

solved by any of the methods:

•  Substitution

•  Recursion tree

•  Master method

(53)

n

n/2 n/2

n/4 n/4 n/4 n/4

1 1 1

. . .

1 1 1

Level 0

Level 1

Level 2

Tree Height : log2n

Merge n items: O(n)

Merge two n/2 items: O(n)

Each level requires O(n) operations

Each level O(n) operations & O(log2n) levels à O(n*log2n)

53

(54)

• 

Worst case:

• 

Average case:

• 

Performance is independent of the initial order of the array

items.

• 

Advantage:

Mergesort is an extremely fast algorithm.

• 

Disadvantage:

Mergesort requires a second array as large as the original

array.

O(

n

* log

2

n

).

O(

n

* log

2

n

).

54

References

Related documents

The standard deviation (SD) of Level I metrics across PET-AS methods, given with the median value in Table III, ranged between 40% (absolute error in volume) and 94% (er- ror in

Building on the marketing analytics in the core part of the program, this course looks at Search Engine Optimization (improving the ranking of web- sites on Google and other

a Sagittal T2 image demonstrates short segment hyperintensity (bracket) with slight expansion of the cervical cord b Axial T2 image demon- strates greater than 2/3 cord in-

Each patient in either the operated or turned down groups had a data set collected, which included patient related, cardiac related and operation related factors allowing

In the previous study there was a significant association from 18 months to 3 years between motor skills and later language performance, but neither gross nor fine motor performance

Dimitrova R, Gibson J, Turkel C, BONT-A CDH Study Group (2005) Botulinum toxin type A (BOTOX) for the prophylactic treatment of chronic daily headache: a randomized, double-

Finally, we provided a proof-of-concept by combining rep B and placI for expression of GFP gene in Shewanella , and followed by demonstrated expression of Mtr path- way as