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

Sorting C 11

The document discusses various sorting algorithms and their time and space complexities, including bubble sort, selection sort, insertion sort. It also provides an example of using two pointers to find two numbers in a sorted array that add up to a specific target number, with time complexity of O(n2). The document analyzes time and space efficiencies of different sorting and searching algorithms.

Uploaded by

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

Sorting C 11

The document discusses various sorting algorithms and their time and space complexities, including bubble sort, selection sort, insertion sort. It also provides an example of using two pointers to find two numbers in a sorted array that add up to a specific target number, with time complexity of O(n2). The document analyzes time and space efficiencies of different sorting and searching algorithms.

Uploaded by

Divyanshu Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Sorting Algorithms

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

arr 617 325489910

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

arr 6 /7325489 910

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

Ox for (ini ; < +


2 +
=

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) +

printf(" in"); nnt's operations


3
:
0(3n3 2n 8n)
+ +
= 0(n3 n
+
n)
+
0(n3)
=

0(X 8) + = O(m)

0(n3k n
+
1)
+
= 0(nk)

in size size away, size


space:
Ea away, I
5 size X
array
-> -


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
-

Ques : Given an array of integers numbers that is


already sorted in non-decreasing order, find two
numbers such that they add up to a specific target
number.
int target = 8
0 1234567

int aur[] = 123458910

for lint i 0;i <n 1;i + ))


O(n2)
= - +

->

for (intj= i 1; j<n; j +)


+
+

if (avr(i] aur(j) target) <


=

+ =

1/found
-
3
int target = 8
0.23 y 56 7

int aur[] = I 2 3458910 -> max + 2

X X i i i i j comparisons
inti =0; target)
if (av(i] avr[i]
+ = =

int j n 1; Ifound
= -

if (awv (i) avoli]>


+
target) (
j--;/1 to decrease

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 ↓

O(n -1) = 0(n)


1 234 S

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) O(1) & O(1)


/
b) O(n) & O(1)
c) O(n) & O(n)
d) O(logn) & O(1)
Ques : Given an array of⑧
6 elements, what is the
max number of swaps we need to sort the array:
6 S 432/

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

red sorted part


blue unsorted

past
Coding implementation of selection sort
0.2 3 Y
⑪servations:
pass-1 5432 1
· For in dements we

need in-1 passes.


1432 S
pass-2
· In each pass we find

1 23u S out the mint element in


pass -3
the unsorted part.

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

Worst Case ->


O(n)

of ops- 1 ...3 2
nn+1) e
-
no n n
+
-

n
+ -
2 + + =

2
-

Avg. Case -> O(n) n -n


Best Case - 0 (n))
Is selection sort stable ? No.

/
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:

a) It has a worst case complexity which is better


than that of bubble sort.
b) It takes O(N) swaps while the other techniques
/
take O(N^2) swaps.
c) The cost of swapping is an issue.
d) All of these.
-

section Sort: 0123 Y

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

than its left element

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
·

for (it i 1;=


i =n -

1;i +
+

( i
j -1j
int j ij
=

while (i>1&& arr(i] <av(i-1])

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) array in sorted order


b) large array
c) normal unsorted array
/
d) array sorted in reverse order
Ques : How many passes would be required during
insertion sort to sort an array of 5 elements?

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.

Hint:Bubble Soot, sort mat socho


are
(310101010
for (it i 0 ish
=

;
-
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
-

where k <= size of array, We need to return the kth


smallest element of the array.
S 2 134 n 5
=

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.

12354 See. min no.

sework:If last
two indexelements are same,
then?

You might also like