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

Cpplecture 23 Notes

It Contains Sorting Algorithms used in Programming, Selection and Insertion Sort

Uploaded by

Pranit Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Cpplecture 23 Notes

It Contains Sorting Algorithms used in Programming, Selection and Insertion Sort

Uploaded by

Pranit Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

C++

PROBLEMS ON SORTING
Lecture-23

Raghav Garg
Today’s checklist

1) MCQ
2) Programming questions
Ques : Which sorting technique is used here?
A player is sorting a deck of cards numbered from 1
to 52. She first picks one card then picks the next
card and puts it after the first card if if it is bigger or
before the first card if it is smaller, then she picks
another card and puts it into its proper position.
a) Bubble sort
~
b) Insertion sort
c) Selection sort
d) None of these
Ques : Which of the following is not a stable
sorting algorithm?

a) Insertion sort
~
b) Selection sort
c) Bubble sort
d) None of these
Ques : Majority Element [LeetCode 169]

1) Bubble +
o(n2

2) Selection +
O(n2)

3) Insection +
o(v2)

4) Built in sort-o (nlogn)

n 7
=

22 I 1 22 2

Sort1] n/2 3 =

I I 12222
Ques : Given an array with N distinct elements,
convert the given array to a form where all
elements are in the range from 0 to N-1. The order
of elements is the same, i.e., 0 is placed in the
place of the smallest element, 1 is placed for the
second smallest element, … N-1 is placed for the
largest element.
19 12238 16 n S
=

Sort
6 3

8
140

12 16
2

1923
⑧ I 23 Y
19 12 238 16
tion

[
16
Not
1912238
T.C. -> O(n2
230 16
19-1 * 5.2. 0(1)
-

230 2
19-1
-

3 +
230- 2

1 Y 0 -
2
3
-

-
-

E 3 I Y 0 2
is visited
2 3 4
O I ⑧ I 2 3 Y

3 I Y O 2
f
I I ⑧ I

T.c. ->
O(n2)

S.c. - o(n)

t.2. O(nlogn)
Amaps: e

S.C. O(n)
Ques : Assign Cookies
*
[LeetCode 455]
CI 12 c3
2 3 1

E I
sorting

23 I 2
Ques : Assign Cookies [LeetCode 455]
Greed Array
Cookie Array
7
36 7 8 9 IS 14 13 1016
10 IS in

E G 56 7 8 9 1013 14 15 16
M
7 IS 16

i
j

while ( I

if (s[i]> g(i)) ehei++;

I
count++;

it + ;

j +;
+

3
Ques : Given an array, arr[] containing ‘n’ integers, the
task is to find an integer (say K) such that after
replacing each and every index of the array by |ai – K|
where ( i ∈ [1, n]), results in a sorted array. If no such
integer exists that satisfies the above condition then
return -1. Find the range of K'
⑧ kS
I
=

2
S 2 10

S 3 8
K=6
1-11 1-4) (4)
a, as 93
I 4 y sorted

1011-21 131
0 2 3 + sorted
S 2 10 8
(a1(b)
↓ d I a2 =b2
a- b2 0 =

15-R112-K) 110-11 -
sorted

12-R) >, 15-R1 and 110-k/, 12-R1

[2- k), [S-r)


k
(k-2"
=>

(k
=> -

5)(k
2
-

k
+
(K-5-,
-
-
z
0

(r 5))
-

>,0
↑ kx3.5
R >, y
(2R-7)(3)>0
=> => 2K -

77,0
I
am and o

al 92 as an 9s 96
L (am am-1] , 0

In-alk-arid
24 -
+

2K
=> >am+am-1
IK- as1
R
↓ adm
=>
>

min
Generalising...

1k-am) Ik -am=1 If am-1 -Am <0

amml
k <

(k-am" - (K- am-1)2 > 0


max

(k -

9m + R -
am-1)(K-am 1 -

am-1), 0
+

(2R -

[am+am-1])(am-1 -
am) >, 0
K range (A, BJ

Kmax, kmin as
If Kmax <kmin snetum-1;

acrol I 2

S 2 10
|a b1 1b a)
110-R1>, (2-k)
=
- -

12-181 >, (K-21


6.1
k = K >, 3.S
(K-10), (k 2)2 =

G ↓

(K-10)-(K-2), 0 k6 =
k>, Y

(k-10 +
k -
27(k -10 k 2) - +
>,0

12k -

12)/ 8)-
>, 0 => 2k-12 = 0

L d
12
=
- ve (R
=>

- Ve
I k = 6
S Y 10

k4
=

RS
=

I & 6X
S ~
⑧ I

k 8
=

k6 =

3 Y 24
1 2 en

k7
=

2 3 3 W
Kor
10 k Y
=

S 2

~x
2

s
=

3 6 22

-
x 6
=

k9
=

Y I 1
k 7 =

k3
=

2 S 3
X 2 1 7 x
5 - 30
8
S?6 E
& A

k
,513E E
> 4 x

kmin

max(kmin, it
kmin= 3 -
6 = 0

x
36- kmax

Kmax=min(kmax,
Aisit1))
6 -
8 c0

xc
08 xe
=

Kmax
S 3 10
K max = 8. I

kY
=

1 1 6 v ↓ L
Kmax=8

R 6.S
=

3.5 3.5 w
1.5
3 6 S-37,0
S
10 ↑

kmin
S
=

=
[Kmin, Knax] (8,6.5)
=

3 -
10 < 0

Kmax
310 6.S
=
=

10 -
67,8

knin= -
5 -

37,0
S 3 7
-
-

kmin
5 4
=
=

3 - <
+ 0

Knax
713 S
=
=
6 2 3-37,0
S 3
~ /

kmax=6.8
kmin=
StB Y
=

k 6.8
= -
(int
I ↓

6 3 -
6 - 0

6.S
kmin
=

2
1 knax
613 4.5
=

cixt) +

K >, 6.5

C
↓ 6-2>, 0

but
K is integer 4
knin=
612
=

K7,7
U kmin= 4.6
kmin=

1

X

y k 7,5

(int) (4.6) + 1

u 1
=>
+
VVVIMP
Binary Search 3
for
Interview

THANK YOU

You might also like