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

unit 5 pps

The document provides an overview of searching and sorting algorithms, specifically focusing on linear search and binary search methods for finding elements in an array. It also details sorting algorithms such as bubble sort, selection sort, and insertion sort, explaining their procedures and implementations in C programming. Key concepts include the importance of sorted arrays for binary search and the step-by-step processes for each sorting technique.

Uploaded by

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

unit 5 pps

The document provides an overview of searching and sorting algorithms, specifically focusing on linear search and binary search methods for finding elements in an array. It also details sorting algorithms such as bubble sort, selection sort, and insertion sort, explaining their procedures and implementations in C programming. Key concepts include the importance of sorted arrays for binary search and the step-by-step processes for each sorting technique.

Uploaded by

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

UNIT-V ST SeA RCH ING -AND SORTING,

# SEARCHING Seach an element n 3ay of elemenl


(tind) (ejven) Ce)ven)

4 G

Key Sngut AINeÐR eARCH


BINARY
(sm
7YPEs DF SEARCHHNG!

AvYay tound
I/p
key
Seorehig (or)
Not found

)NEAR SEARH :

23 4

Key: 51ey
int Ale], key,!
tor(i=o;i<io;i*+)
it (a(] =z key)?
break;

(is=e) rint (* Key s not tourd );


else printf (%ol is found at inder hdó key,D;
# 4ioctude <stdioh
int main c)$
int Atol, key,i ;
Hox(io; iero;i+){
scant (^ nd, n[);

seanf (d , key);
tov (fwo;ic t;i+){

breát;

prutf " tey iu not fond ");


elsei
priatt ("rdx tound a nd; key .,);
+ BiNARY SEARCH
an element en
(key)
it uith sequenual approch
(array)
’ ony wwoYks wheD aray u in torted ordey,

# Key POINTS Mid = high 2-+ lou

Ley = A[md] key is tound.

St key c A[midkey may 'present in fist pat

Eey may present n port

LDwE MID +1}

ow
high
wigh id 1
mid
2 334'6
4

32 A(id]
46
vey
) 45
hioh rd-1

step > lowD 32 A


4

high 3)< 14 *
32 > 14

3) =A2]
step nid = 2 -a 20
=3
high 32 P 20x

Step4
Srep4
Sn case of e 31 =key
loo -3
bigh3 check
mid = 3+3 -3 Step 4
low-3

TRUE
high:3
id =3+3 3

(ser} 31 =- Al3]
incwde <staio.h >
int main C{
int Alo), (ow=0, high 4, key: mid ;
toy (iso; icio;i+){

ohile (ow e= high)i


màl =tow+2 hìgh ;

prott ( tey is found ";


break

elte-it (ey e Alnid high nud-1 :


else loo ud +1;

prottl tey sis not


not --foud);
A/B|ea|n21|3»)4sl4Zl4a/s1
89

ohile (te ezhigh){


d (loot high);

priott('FoIND),;
break rey 48)

elte -it (tey< A(oid))


high =mid-t;

else oo =md + 1,

48 -AJx
48 ==Ak]
48==A* 46
(aa)
48c 46Y
low=& ohile
sort bubble wing anay kovted pvntt
i"
n)bubblesort
;(arr,
(otoJ) (av)/kine
of oftiee n= nt
qo} 25
,li,,/2,22 =b4,34, nra
c)i man int
('n"); pritt
awti); (vd;!priatt
icn;it( iz0j (int for
,ntn)t pintaway
on(int void
tenpi avrlj]=
lj+]; ar anj]
amlj; temp= int
arlitJ arljJ> suap
Cj+): (arli
a> t
;++){ n-i j<iz0) (int for
;+)5 n-t ici=0, (int tor
bubblesort
(int vojd
t
)í arC],
stdion> fnctude
< t
SORT
# BUBBAE t
# SeLec+iDN SDRTING

AhGORITHM
) Stast wth the -fst elemenr in the asay
>) Iterate the array to fnd the smalest element
-hvough
) Suoap the smallest element wh fvst element
4 move O-The
Ttho net poiaon and vepeat steps Tuo and 3
tor Yenaning elemens

sDconine ntii the asay s torted

# ncude cstdio h
Vod seleotonSort ( int ar [J, inti
in i minlda,temp ;
for (izo:ien;i++)1
V Fnd the mis element n rem ain ing asay
milda i;

it (as j]e ai (inld )1


1 minlda =j*
Ali]
huap the tound nin elem ent wth tre
element ArCÜ

anlauintda];
aY minld] a ;
arY[iT temp
41 44 Passt
R
5/49
Pas2
element Smallust
valu
-1,1. inden
,
-s
4
+)
o); (arh,
Pvnthrvay
aray:) Sovted pròtt
/"
(avp, SortSelecton
(ar,n Arva print
:); aray ("ovina printt
(arto]) ; size tar)/
ot oftize = nint
2 , 12, 25,
", anC]j6u, int
()
mar tnt
l"yo; pviatt
(J);: arr "d" prutt
Pas4

/-3

Pass

Pass
&

4| S 4|9
fmalles

Fnal torad fray?


A2l-l45|H
A plainal en for pas i
a+|3lals+|14

ACjJ <A[adinder) 3< 4


nüdinde =

<3
núd index =4
3
¿=3 ’ACj]e A[mininde] 5<2

1=S’ ACj] <'ACidSnde


nic Snader=j
ALGORITHM:
tor (i=0--)

midinder i
tor (jti’n-di
minindenot
it (rininderl=)E
tenp -A[

A[in Ihdei] tenp

ndex ot the smalest elemenl and toap


each pas nd the
u th AJ

rriov
INSeR TION SORT AL GORITHM int main C1
int awl]-in,1, 13,5, 6}
) start uth the lecond element as the key int n = ize ot (a) / ie ot (an o);
> CopaYe the key ith elemenzs befoe it. pvintt ("oniginal array :);
3) s i t all elemens reater than the key one pontion
prùthrray (an);
inserion Sovt (arr,);
the nghr.
4) Tnyert correet Dosun
the key at is
priott (" Sorted trYay );
Repeat toY alU elemens n aray.

include <itdio hs
void insertionsort ( int ang, int )i
Dnseruon SoYt

tey= a i]; Avay


3
nove elements of avr o..-J, thar ave greater than key
u o one poiuon of ahead ot cussent po shon key =23 keg'- Ar (i]
PASS 1
tey c arr[J
aw[jJ = aY(;]: 23c2&

anfjl zkey:

vold pvry(int avt0, iotn)f


for (int iz0; icn; it)i
| printt (zd, avl3);
printf ao");
Pas
key26

Move q o net a3/24/34


-fas
nove key
key to
to Arlitü
(222e/24

Pas key .2,


las (261 99| 21
key =&)

a3

J=-1’ Afu ulie toop


Moue beyo ngly] S6]29)

a s sy
key a
i=4 MOve a lo net
1=3,a,I,o
(ail23| 26J24/29)

lcey
a|a3|26] a>129

a1|23|36|}24[28)
J=493,2,1,0

You might also like