Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11...

25
1 10 14 18 19 3 7 16 17 23 25 2 11 two sorted halves Total: i = 6 Inversions between two lists Given two sorted halves, count number of inversions where a i and a j are in different halves.

Transcript of Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11...

Page 1: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

1

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

Total:

i = 6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 2: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

2

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

i = 6

two sorted halves

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 3: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

3

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

i = 6

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 4: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

4

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

i = 6

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 5: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

5

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

i = 5

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 6: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

6

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

i = 5

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 7: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

7

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

i = 4

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 8: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

8

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

i = 4

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 9: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

9

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

i = 3

Total: 6

6

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 10: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

10

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

i = 3

Total: 6 + 3

6 3

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 11: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

11

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

i = 3

Total: 6 + 3

6 3

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 12: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

12

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

i = 3

Total: 6 + 3

6 3

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 13: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

13

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

i = 2

Total: 6 + 3

6 3

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 14: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

14

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

i = 2

Total: 6 + 3 + 2

6 3 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 15: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

15

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

i = 2

Total: 6 + 3 + 2

6 3 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 16: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

16

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

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 17: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

17

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

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 18: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

18

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

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 19: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

19

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

i = 1

Total: 6 + 3 + 2 + 2

6 3 2 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 20: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

20

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

i = 1

Total: 6 + 3 + 2 + 2

6 3 2 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 21: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

21

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

i = 0

Total: 6 + 3 + 2 + 2

first half exhausted

6 3 2 2

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 22: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

22

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

i = 0

Total: 6 + 3 + 2 + 2 + 0

6 3 2 2 0

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 23: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

23

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

i = 0

Total: 6 + 3 + 2 + 2 + 0

6 3 2 2 0

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 24: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

24

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

i = 0

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

6 3 2 2 0 0

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

Page 25: Inversions between two lists - mmolinaro/AA17-1/05demo-count-invert.pdf · 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves Total: i = 6 Inversions between two lists Given two

25

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

Inversions between two lists

Given two sorted halves, count number of inversions where ai and aj

are in different halves.

two sorted halves

i = 0

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

6 3 2 2 0 0