1 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count...

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. smallest i = 6 smallest j = 6 N/2 = 6 auxiliary array current Inversions: Total: 0

Transcript of 1 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count...

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.

smallesti = 6

smallestj = 6

N/2 = 6

auxiliary array

current Inversions: Total: 0

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.

smallesti = 6

smallestj = 6

N/2 = 6

auxiliary array

current Inversions: Total: 0

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.

smallesti = 6

smallestj = 5

N/2 = 6

2 auxiliary array

current Inversions: 6 Total: 6

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.

smallesti = 6

smallestj = 5

N/2 = 6

2 auxiliary array

current Inversions: 6 Total: 6

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.

smallesti = 5

smallestj = 5

N/2 = 6

2 3 auxiliary array

current Inversions: 6 Total: 6

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.

smallesti = 5

smallestj = 5

N/2 = 6

2 3 auxiliary array

Inversions: 6 Total: 6

current

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.

smallesti = 4

smallestj = 5

N/2 = 6

72 3 auxiliary array

Inversions: 6 Total: 6

current

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.

smallesti = 4

smallestj = 5

N/2 = 6

72 3 auxiliary array

Inversions: 6 Total: 6

current

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.

smallesti = 3

smallestj = 5

N/2 = 6

7 102 3 auxiliary array

Inversions: 6 Total: 6

current

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.

smallesti = 3

smallestj = 5

N/2 = 6

7 102 3 auxiliary array

Inversions: 6 Total: 6

current

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.

smallesti = 3

smallestj = 4

N/2 = 6

7 10 112 3 auxiliary array

Inversions: 6 + 3 Total: 9

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.

smallesti = 3

smallestj = 4

N/2 = 6

7 10 112 3 auxiliary array

Inversions: 6 + 3 Total: 9

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.

smallesti = 2

smallestj = 4

N/2 = 6

7 10 11 142 3 auxiliary array

Inversions: 6 + 3 Total: 9

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.

smallesti = 2

smallestj = 4

N/2 = 6

7 10 11 142 3 auxiliary array

Inversions: 6 + 3 Total: 9

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.

smallesti = 2

smallestj = 3

N/2 = 6

7 10 11 142 3 16 auxiliary array

Inversions: 6 + 3 + 2 Total: 11

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.

smallesti = 2

smallestj = 3

N/2 = 6

7 10 11 142 3 16 auxiliary array

Inversions: 6 + 3 + 2 Total: 11

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.

smallesti = 2

smallestj = 2

N/2 = 6

7 10 11 142 3 16 17 auxiliary array

Inversions: 6 + 3 + 2 + 2 Total: 13

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.

smallesti = 2

smallestj = 2

N/2 = 6

7 10 11 142 3 16 17 auxiliary array

Inversions: 6 + 3 + 2 + 2 Total: 13

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.

smallesti = 1

smallestj = 2

N/2 = 6

7 10 11 142 3 1816 17 auxiliary array

Inversions: 6 + 3 + 2 + 2 Total: 13

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.

smallesti = 1

smallestj = 2

N/2 = 6

7 10 11 142 3 1816 17 auxiliary array

Inversions: 6 + 3 + 2 + 2 Total: 13

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.

smallesti = 0

smallestj = 2

N/2 = 6

7 10 11 142 3 18 1916 17 auxiliary array

Inversions: 6 + 3 + 2 + 2 Total: 13

first half exhausted

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.

smallesti = 0

smallestj = 2

N/2 = 6

7 10 11 142 3 18 1916 17 auxiliary array

Inversions: 6 + 3 + 2 + 2 Total: 13

first half exhausted

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.

smallesti = 0

smallestj = 2

N/2 = 6

7 10 11 142 3 18 19 2316 17 auxiliary array

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

first half exhausted

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.

smallesti = 0

smallestj = 1

N/2 = 6

7 10 11 142 3 18 19 2316 17 auxiliary array

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

first half exhausted

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.

smallesti = 0

smallestj = 0

N/2 = 6

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

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

first half exhausted

second half exhausted