Sorting Basics
Sorting Basics
1 Revision quizzes
2
Sorting basics
31 Problems
4 sorting Algo Insertion sort
stack heap
10
a
changell 50
a
mainll
so
If
as
g 4k 10 20 30
a
main
sorting
arranging data in a particular manner
Eis
1 It helps to do searthing or analysing data in efficient manne
increasing order
A 40 30 20 15 5
0 1 2 3 4
decreasing data
basis count
of factors
A I 13 9 6 12 yes
Dev DSA
an't sort data without knowing
si so the basis
52 55 50
increasing
r
to
P Ts ochdogh
dec
Hw what will be the syntax to sort array descending
Java python
inti A 5 9 4 33 A s 9 4 33
11point A print A
13 4 5 93
The ios
Given an AI find min cost
of emptying the array
is defined as the sum of all elements
of removing an element
at time of removal
array
A
tost 2
11 11 1
775 4
remove 2
16
I 4
remove 1
a
remove 4
17
A
lost 2 1 4
21
remove 4 7 3 1
2 1 11
remove 2
remove 1
cost 4 61 4 1
A 4 6 17
o I 2
11 5 1
remove 6
17
4 17
cost 3
A 13 5 1 3 III 31
11 1 t 3
remove 5 31
13 1 37 6 1 1 2 3
2
remove 3
1 3
remove 1
remove 3
I
A
soot
sum 1 4 6 0 4
1
cost If
17
on
A1
junction solve
A sort 11 11 ascending
A
sum 0
cost 0
for in 0 to n it
sunt Ali i
i
sum 7 2
4 4 21
Jor i n t to 01
costt sum lost 2 4
1
Sum Ali 01
3
11
return lost
3
T1 O ndogn
find count
a Given an ACT of distinit ele of noble integers
elements smaller than Ali is Sam
Note Aris is noble if count of
as value Ali
A ans 3
19
countos
smaller
now using
Brute force go on every element using outer loop
inner loop find the count smaller dements than it
of
code HW if count Ali Asi is noble otherwise not
T1 O n
A 3 0 2 5
ans I
0 1 2 3
county
smaller
A 1 19
sort ascendenging
countofsmaller
A for Ali
things on left
Junction solve AC
A sort 1
Ts O ndogn
ans 0
it to n it
for 0
if Ali it 9
anstt
3
3
return ans
3
AC I need not to be distinct find count
a Given an of
noble integers
elements smaller than Ali
Note Aris is noble if count of
A 1
sort
A 0 I 3 3 4 4 9 ans 4
0 I 2 3 4 5 6
3 countof smaller i
first
next 3 countof smaller countofsmaller for first 3
A 3 ans 3
18 10,0
A 10 I 1 2 4 4 4 8 10 ans 5
4 s 6 7 8
0 1 2 3
5 5 8 8 10 10 10 14 and
A 1 3 0 2
2
2
3
5
4
5
5 6 7 8 9 10 11 12 13
0 1
cantor
smaller
unction solve 1 A1
A sort C 11 ascending
ans o
I count smaller
cos 0 of
A 10
if 0
anstt
J
A 1
n t
Jor lit 1 to
Ali 1 Ali 1
if
cos i an 0 105 7
3 44
if cos Ali
anstt
3
3
return cens
T1 ochlogn
sorting algorithm Insertion sort
1 8
1 8
3 8 6 2 5 I I 5 0 to o
o I 2 3 4
3 6 8 2 5 i 2 j 1 to 0
I 2 3 4
i 3 j 2 too
2 3 5 6 8
4
i y j 3 to 0
I 2 3
m
unction Insertion sort AI
for i i 1 to n it
for j i i to 0
A 15 1 9
if As
Iswapping
temp A j
A j Aljti TC O m
Alita temp
else 9
break
3
3
5
3
Junction Insertionsort A
for i i 1 to n it
A I 2 3 4
for j i i to 0 o i 2 3
A15 1
if As
Iswapping
temp Alj
A j Aljti
Aliti temp
else 9
break
3
5
5