Sorting C 11
Sorting C 11
Today’s Checklist
● Time Complexity
● 2 Pointer approach
● Bubble sort
● Selection sort
● Insertion sort
Ques: Given an array of integers with 1 to n elements
and the size of the array if<3n+1. One element is
occurring more than once i.e duplicate number is
present. Find the duplicate element.
d ↑ 2 3 Y 56 -8910
M I
-
-
· forlint i 0
=
;i<n 1;i +)
- + Efficient
-> on terms of
space
I!
for (int j i 1i
=
jcn;j 1
+
if (avr(i] avr[i]) (
==
I printf));
break;
3
3
w
2373;
d ↑ 9 10
arr 6 910
- 1
↓
- 53
operations
-
10 =
M 2
-
d ↑ 23 756-8910
-
boz 01 1 I 11 1 110
↓ visited
array
In terms
Efficient -
of time.
nficient.
In terms
of space-O(n) extra space
We are using extra
space
M 3
-
= w
61 234 56 -8910
sum =
06 A IIA 19242/8 3/ US SM 64
from 1
=55
numbers
sum of 1 20
to +
Sn n(n 1)
=
-
+ 64 - 55
=
2
Efficient
in terms
of time
spaceboth
3rd gen is 3udgen is
· #AN
Time complexity
space complexity
TLE -
time limit
exceeded
Ox for (ini 0 ;i <u;i +1 Big 0 Notation'
+
=
I printf( "Hello");
0(n a)
+
X O(n)
3 ↓
constant
↓
0(n)
n
operations ->
u;i 1
i
I printf( "Hello");
3
n 3
+ +
0(n 3) v0(n) +
< 3 n;i +
i
as for lint i 1;
=
+
0(n)
0(u*n)
=
I printf( "Hello");
k -> constant
3
0(3*n) O(n)
1;
i
n i +
Ox for (in i < n +
=
;
I printf( "Hello");
O(n*n) 0(n))
=
as for lint i 1 =
i
;
<n ;i +
+
1 i =
1 +j 1 ton=
j(n;j +
1
for (intj 10;
+
=
I printf( "Hello");
3
3 ⑩(n2)
O,for lint i 1;i<
= n ; i +
+
0(n(n
1)) =
0(n )
for (int i 1;j<i;j +
1
+
+
0(tn n)
=
I printf("a");
3 ~ O(n n) +
0(X 8) + = O(m)
0(n3k n
+
1)
+
= 0(nk)
↓
0(1)
what
is
sorting: sort -
putin ascending
order
9 1 286 u
C
sort in
I
put in
12 4689 decreasing
order
descending
order
*2-pointers "algorithms
-
->
+ =
1/found
-
3
int target = 8
0.23 y 56 7
X X i i i i j comparisons
inti =0; target)
if (av(i] avr[i]
+ = =
int j n 1; Ifound
= -
if (aur(i] a(r)5]
+
< target) <
i+ +; // to
increase
3
inti 0;
0(n)
=
int i n
=
-
1;
while (i <j)k
target)
1
if (auv(i) av(j)
=
+
=
I printf / (
break;
3
else if (aur(i] aur(5]> target) j -;
-
+
else i +
+
;
3
Bubble Soto:
9/34105 original
I 3 456 9 10 Souted
1) Technique 1) Complexities
2) explanation
3) Optimization
arr
9 /3 41056 original
1934105
1394105
1349105
Epaereel, are +13
*
1349105
1399 5106
13 495610
Pass-2
-
- 13 495610
13495610
13495610
13495010
13459010
1345 6910
Sorted &
I 3456 910.
already
3 2db ass
13456910
-
-
1st pass 2nd pass ⑭
-
- -
=>
v
5 u 3 21 u 3 2 I S
N
4532 I 34n I S
X
43521 324 I S
~ ⑤
432 3 I 3 2 1 Y S
v
43 2 I S
pass
3
-
yth
-
-
pass
~ ⑤ X -v
3 2 1 Y S 2 1 3 us
-v / vvv
23 1 Y S I 2345
X -v
2 1 3 Y S
away-in-1 passes
S
· n' elements in the
·
After every pass, we need to
apply bubble sort on the
unsorted elements
only we do not need to
check the
last
Coding implementation of bubble sort
Nested Loops
6 lookwill stand of passes
Outer for no
↳
will do
Inner loop the
swapping
i
j + 0ton -
1 -
Time complexity Outer Loop-oxien -
2 n -1
bear chalega
↳
Inner Loop
1 0 -
=
n-1 bao
i 1 =
h
->
-
2 baar
i 2
= + n -
3 boar
i 7
=
-n -
ybaar
1 2 3 4 2 1
OPs
.....
+
n n n n
- + +
+
no
- + - -
=
inn sO(1-2)
=
= oIn:Oh
Maximum no of swaps in worst case in Bubble Sort
↓
descending
If size of is in
away
1 ... 3 1
nt)
->
n -
x
+ - -
2
+ +
=
How to optimize the bubble sort in the case of
nearly sorted arrays?
↓
check
if asway after every pass is already sorted of not.
↓
with the help of a checkmark.
- >
Icomplexityo f
Bubt
in best : case
flag=true; n + n -
1
2 3uS ↓
123uS
123uS
1 2 345
flag=true,
complexity
Time spcomplexity
-
o(n) 0(1)
Best Case
O(n2) 0(1)
Case
Arg.
0(n) 0(1)
Worst case
Is Bubble Sort Stable? Yes!
7
2 S 2 Y
6.
↓ stable sout
al 2 2 u Stable sort
Y
/
2 2 S
Is Bubble Sort Stable?
25 2 Y
25 2 Y
25 2 Y
2 21 S Y
22u 5
Ques : What is the best case time and space
complexity of bubble sort:
a) 21 ↓
b)
/ 15 n 1
-
n
+ -
2 n 3
+
-
..3
+ z
+
1
+
c) 10
5 4 3 2 1 15
d) 28
+
+ + +
=
Bubble Sort - Unsorted
array ->
ascending order
325/4354321
in descending order.
1,Sort
Selection sort
② mink element
past
Coding implementation of selection sort
0.2 3 Y
⑪servations:
pass-1 5432 1
· For in dements we
pass y 123u S ⑧
After every pass the
-
unsorted reduces
away
1 2345 by 1
length,
⑮Run:i0 =
to 3 0 I 2
3 Y
1 234 5
i 01=
min Max
=
SMRDMax 2
minidx 10 & B
=
M y13
Time complexity
of ops- 1 ...3 2
nn+1) e
-
no n n
+
-
n
+ -
2 + + =
2
-
/
2 3 4 3 O
if
&
(minav(5])
01
o
2
1
3/
2
34
3 3/
instable
stable
<min=arr(s)
3
Ques : What will the array look like after the first
iteration of selection sort [2,3,1,6,4] ?
a) [1,2,3,6,4]
b) [1,3,2,4,6]
c)
v [1,3,2,6,4]
d) [2,3,1,4,6]
Ques : Which of the following is an advantage of
selection sort over bubble sort:
1st 5 132 1
End
Swapping from
·
nd
finds
tiel the element
2 4 5321
position.
its
rd
·
swap happens 3 3 4521
when the
only th
smaller u 2 3451
element is
12345
Insertion Sort:
for e
B
0 12 3 Y
1 2 3 & S
n -n 1
-
operations -
O(n)
Coding implementation of insertion sort
01234
of passes-n-1 passes
⑭2
No
Is Is
·
1;i +
+
( i
j -1j
int j ij
=
swap(aur (i],aur(i-1]);
j- -;
3
3
Time complexity
Worst Case -
O(n'
Arg. Case -
O(n2)
Best case -
O(n)
Is Insertion Sort Stable? ->
yes!!
⑭
10(4/2
Ques : Which of the following examples represent
the worst case input for an insertion sort?
a) 1
b) Depends on order of elements
v
c) 4
d) 5
A
Ques : Given an integer array arr, move all 0's to the
end of it while maintaining the relative order of the
non-zero elements. idx 0/R3M5
=
are
0
10(4)/1310
and(s),
1s
↳Tc 0(n)
=
S.c. 0(n)
=
Ques : Given an integer array arr, move all 0's to the
end of it while maintaining the relative order of the
non-zero elements.
Note that you must do this in-place without making a
copy of the array.
;
-
1;i
+
+
i;i +1
1!
for (it j 0;j<n 1
+
= - -
if (av (j) =
=
0)
(i+1]);
3 swap law (i), aur
3
Homework
Ques : Given an integer array and an integer k
-
k 3 =
0(k)
Ques : Given an array of digits (values are from 0 to
9), the task is to find the minimum possible sum of
two numbers formed from digits of the array.
Please note that all digits of the given array must
be used to form the two numbers. Step-1->Sort
↳31112/M5 12355
12535
12395 min no.
sework:If last
two indexelements are same,
then?