merge and count algorithm

25
1 10 14 18 19 3 7 16 17 23 25 2 11 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array Total: i = 6

Upload: cagatay-yuksel

Post on 04-Jun-2015

294 views

Category:

Technology


1 download

DESCRIPTION

Demonstration of Merge and Count algorithm by an example

TRANSCRIPT

Page 1: Merge and Count Algorithm

1

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

auxiliary array

Total:

i = 6

Page 2: Merge and Count Algorithm

2

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

i = 6

two sorted halves

2 auxiliary array

Total: 6

6

Page 3: Merge and Count Algorithm

3

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

2 auxiliary array

i = 6

Total: 6

6

Page 4: Merge and Count Algorithm

4

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

2 3 auxiliary array

i = 6

Total: 6

6

Page 5: Merge and Count Algorithm

5

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

2 3 auxiliary array

i = 5

Total: 6

6

Page 6: Merge and Count Algorithm

6

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

72 3 auxiliary array

i = 5

Total: 6

6

Page 7: Merge and Count Algorithm

7

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

72 3 auxiliary array

i = 4

Total: 6

6

Page 8: Merge and Count Algorithm

8

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 102 3 auxiliary array

i = 4

Total: 6

6

Page 9: Merge and Count Algorithm

9

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 102 3 auxiliary array

i = 3

Total: 6

6

Page 10: Merge and Count Algorithm

10

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 112 3 auxiliary array

i = 3

Total: 6 + 3

6 3

Page 11: Merge and Count Algorithm

11

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 112 3 auxiliary array

i = 3

Total: 6 + 3

6 3

Page 12: Merge and Count Algorithm

12

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 auxiliary array

i = 3

Total: 6 + 3

6 3

Page 13: Merge and Count Algorithm

13

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 auxiliary array

i = 2

Total: 6 + 3

6 3

Page 14: Merge and Count Algorithm

14

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 16 auxiliary array

i = 2

Total: 6 + 3 + 2

6 3 2

Page 15: Merge and Count Algorithm

15

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 16 auxiliary array

i = 2

Total: 6 + 3 + 2

6 3 2

Page 16: Merge and Count Algorithm

16

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 16 17 auxiliary array

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

Page 17: Merge and Count Algorithm

17

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 16 17 auxiliary array

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

Page 18: Merge and Count Algorithm

18

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 1816 17 auxiliary array

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

Page 19: Merge and Count Algorithm

19

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 1816 17 auxiliary array

i = 1

Total: 6 + 3 + 2 + 2

6 3 2 2

Page 20: Merge and Count Algorithm

20

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 18 1916 17 auxiliary array

i = 1

Total: 6 + 3 + 2 + 2

6 3 2 2

Page 21: Merge and Count Algorithm

21

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 18 1916 17 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2

first half exhausted

6 3 2 2

Page 22: Merge and Count Algorithm

22

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 18 19 2316 17 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2 + 0

6 3 2 2 0

Page 23: Merge and Count Algorithm

23

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 18 19 2316 17 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2 + 0

6 3 2 2 0

Page 24: Merge and Count Algorithm

24

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 18 19 23 2516 17 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2 + 0 + 0

6 3 2 2 0 0

Page 25: Merge and Count Algorithm

25

10 14 18 193 7 16 17 23 252 11

Merge and Count

Merge and count step. Given two sorted halves, count number of inversions where ai

and aj are in different halves. Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 142 3 18 19 23 2516 17 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2 + 0 + 0 = 13

6 3 2 2 0 0