Merge and Count Merge and count step. Given two sorted halves, - - PowerPoint PPT Presentation

merge and count
SMART_READER_LITE
LIVE PREVIEW

Merge and Count Merge and count step. Given two sorted halves, - - PowerPoint PPT Presentation

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. i = 6 3 7 10 14 18 19 2 11 16 17 23 25 two


slide-1
SLIDE 1

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves auxiliary array

Total: i = 6

slide-2
SLIDE 2

2

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 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

slide-3
SLIDE 3

3

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 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

slide-4
SLIDE 4

4

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 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

slide-5
SLIDE 5

5

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 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

slide-6
SLIDE 6

6

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 2 3

auxiliary array

i = 5 Total: 6

6

slide-7
SLIDE 7

7

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 2 3

auxiliary array

i = 4 Total: 6

6

slide-8
SLIDE 8

8

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 2 3

auxiliary array

i = 4 Total: 6

6

slide-9
SLIDE 9

9

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 2 3

auxiliary array

i = 3 Total: 6

6

slide-10
SLIDE 10

10

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 2 3

auxiliary array

i = 3 Total: 6 + 3

6 3

slide-11
SLIDE 11

11

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 2 3

auxiliary array

i = 3 Total: 6 + 3

6 3

slide-12
SLIDE 12

12

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3

auxiliary array

i = 3 Total: 6 + 3

6 3

slide-13
SLIDE 13

13

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3

auxiliary array

i = 2 Total: 6 + 3

6 3

slide-14
SLIDE 14

14

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 16

auxiliary array

i = 2 Total: 6 + 3 + 2

6 3 2

slide-15
SLIDE 15

15

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 16

auxiliary array

i = 2 Total: 6 + 3 + 2

6 3 2

slide-16
SLIDE 16

16

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 16 17

auxiliary array

i = 2 Total: 6 + 3 + 2 + 2

6 3 2 2

slide-17
SLIDE 17

17

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 16 17

auxiliary array

i = 2 Total: 6 + 3 + 2 + 2

6 3 2 2

slide-18
SLIDE 18

18

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 16 17

auxiliary array

i = 2 Total: 6 + 3 + 2 + 2

6 3 2 2

slide-19
SLIDE 19

19

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 16 17

auxiliary array

i = 1 Total: 6 + 3 + 2 + 2

6 3 2 2

slide-20
SLIDE 20

20

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 19 16 17

auxiliary array

i = 1 Total: 6 + 3 + 2 + 2

6 3 2 2

slide-21
SLIDE 21

21

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 19 16 17

auxiliary array

i = 0 Total: 6 + 3 + 2 + 2 first half exhausted

6 3 2 2

slide-22
SLIDE 22

22

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 19 23 16 17

auxiliary array

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

6 3 2 2

slide-23
SLIDE 23

23

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 19 23 16 17

auxiliary array

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

6 3 2 2

slide-24
SLIDE 24

24

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 19 23 25 16 17

auxiliary array

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

6 3 2 2

slide-25
SLIDE 25

25

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 ai and aj

are in different halves.

■ Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14 2 3 18 19 23 25 16 17

auxiliary array

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

6 3 2 2