0% found this document useful (0 votes)
15 views

Download

1. The document describes how to find the number of inversion pairs in an integer array using merge sort. 2. Merge sort works by dividing the array into two halves, recursively sorting each half, and then merging the halves back together while counting inversions. 3. During the merging process, if an element from the left half is greater than an element from the right half, they form an inversion pair. The algorithm counts these pairs to return the total number of inversions.

Uploaded by

akash gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Download

1. The document describes how to find the number of inversion pairs in an integer array using merge sort. 2. Merge sort works by dividing the array into two halves, recursively sorting each half, and then merging the halves back together while counting inversions. 3. During the merging process, if an element from the left half is greater than an element from the right half, they form an inversion pair. The algorithm counts these pairs to return the total number of inversions.

Uploaded by

akash gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

A 15 8 1 15 7 6 2

Q Find more element TC 01N Sc 011


are Alo
i I to N D
for Ali
if ans
ars Ali
return ans

Q S Find second man element Tc 012N 01N SC 0127 0 1


Q Find third more element s Tc 013N 01N Sc 0133 011

Q Find Kth more element Tc O KN SC 01k old

A 15,81 T is I I I K E
K l t v5 selection sort
K 2
K 3 70 0 NZ SC 011
K 4 152 t É 7 8 15
K 5 I 5
V
4 6 I 2 5 6 7 8 15 K Nd

for i s N r to a a

man Id O

for j r to i
Almond
if AgmoseLd
j
swap Ali Almon Id

A 15 8
V
I is 7 6 2
2 3 4 5 6
i N P
mould K j r toi
Q hiver integer array where all odd elements are sorted
an

all ever elements are sorted sort the array

A L3 9 2 4 15 10 19

2 3 4 9 10 15 19

A L3 9 2 4 15 10 19
0in

I É ra is a no
I I
s 2 3 4 9 10 15 89
I
4 re 01N

Tc O NTN 0 N V
A
LIFETIME Sc Oln V
2 3

Merge two sorted array into one V

11AINTM I P BIN CIM


st mid mid i end

i o 1B j Okc
for k
if.li
o to Ntm 1
N
B 1
E i n
c
In If
AIR IG A 0 2 4 5 6 10
j t t

Alk Bti
i p
I
else
Alk Chi
j t t Hans N i
Merge Sort Divide conquer

a 1 in
9 8 7 3 6 4 1 5 0 10
9 8 7 3 6 4 I 5 0 to f
d
9 8 7 3 6 I 5 o yo
d
3 6 O ro

8 9 13 6 I 43 10103
3 6 77 10
5 10
3 6 7 89 0 1 4 5 10 econ

O 1 3 4 5 6 7 8 9 10

void sort A stierdTL TCN 271N 2 01N

if 1st end a 2 6 2 d r
return bd 21 2 a
mid 1st end 2 70 0 Nd N 01N
log logan
sort A st mid
sort CA midtr end
merge A st mid end
e TC OINK Sc Oln v

Sc O Nt login ON V
N N
a
Oln p N g
t ng
p
L
N2
c s a s
N 01N 1 n 3
T I I
N K r
IK K N r
N 2K K logan TC 0 N
TC 0 NloyCN V
10 40 PM
Q s Giver integer array court
as the number
of
inversion pairs in the array
Inversion pair i j
i j and Ali Ag
A 18 3 47 Co D
0 2 Ans 2

A I s f 32 I 35
0,27 0,3 0,5 1,2 1,3 11,5 4,5
Ans I
a ciii i is Bruteforce Vi j check
Ang z for inversion
Tc OIND SC 010
merge sort
is it I 3

Et
A L
4 5 1 2 6 3
4 5 I 2 6 34
4 5 2 6
4,5 Ans z
2 Ts a j
O 2 T
t t
5
t i
2 3 6
i 2 f f j Tc 01NlongN
I 2 3 4 5 6 Sc O N
2 2
Co 3 10 5 Ans E remainingelements N i in
left part
4,3 11,5 if an elementfrom
rightpartis selected.fr
sorted
Array
A list I
3
titis an
large
as on
2 r
left
A I 2 3 4 5 6 3 5 2 3
0 I 2 3 4 5

sortedA A originalindex sorted index


n e 4 O 3 3
5 4 E 5 o 4 3
6 5 4 1 small 6 4 5 1
I oneight y
sortedindese originalindex notrecommended

At É l
sorted 1

stability Relative order of equalelements should not change


I 8 2

3
2
5
I
feud equal
4 6
5 2
6 3 0 Hint 3
7 9 2 Aditya
8 7 3 Gaurav 4
9 4 Hemanth 2
10 10 same location

stability is array if elements are equal


compare eider

left right
8 3 15 E É
IF 5timid 3 2
of mid I tend so 2
I 2 3 6 8 10 7 8 6

You might also like