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

PCA

The document outlines various algorithms for searching and sorting, including binary search, merge sort, and quicksort, along with their implementations in C. It provides code snippets and explanations for each algorithm, detailing the steps involved in their execution. Additionally, it includes a method for finding the minimum and maximum values in an array using divide and conquer techniques.

Uploaded by

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

PCA

The document outlines various algorithms for searching and sorting, including binary search, merge sort, and quicksort, along with their implementations in C. It provides code snippets and explanations for each algorithm, detailing the steps involved in their execution. Additionally, it includes a method for finding the minimum and maximum values in an array using divide and conquer techniques.

Uploaded by

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

I. Binary Seanh ying divide and Conquer.

Algethm:
Binary.Search (Arr, La, high, Key)
Sltll: start retum-1

Set mid (louhigk) l2


IF Arr[mia] ke)retm mi4
Binarysearch ArrC], low,mid-,ky
TF Anr mid] > Ke, Call
Search (Aw C], idd, high, Keg)
Ele. Binarg-
ke3} End.
Co de
#include <stdio. h hish, int key)
anrCl, int lou int
mE binay-searchlat
retm -l;
int midoQos+kigt)2
it (arrimia]= 2keg)
retumm mid
tlse it (oarrImi] key)
binaryseorch(am, law. mid4, koy:
rehum
elee key);
return bi-s ereh (am md tl, kigh

int main)
lements?;
PhtEnter the num be r ot

PinstlEwker eleato: ) :
for (int iz0, i4n it*)
Stant | 7.A,4a [);
print4 Enter ele mnt to search1
Stant (". , heg)i
int result binorj- Starch (orY, 0, nt, keg);
if (resutt- - )
Printt (" Element ot foun d );
else fund at inder 7.A", result);
prit41 Elemat

Output. numler
Enter elette
3 4 S
Enter elements! L 2
elemnt to Search
Ehtor
t index 9
Elemet fond
2
Sort Diride and Conduer

Arr, orprEal ltt, ight


Mee-sert((Ans,
SlagS telt Lright
/2
Sat mid-(eia rigt)imia)
Merg-sort (Arr, left vigkt)
Merg-Sort(Ar mida\,
id, gt)
Merge (Am, lett.

Merge (Ar, lett, mid, ig ht) ?, n2 night -mi d


m dlett +1
Step : et nl=
[n]
Shey 2,Set L[ni],,a
to
Cke3! for i= 0

n2
ar midal)
RG]
K left
l
Shp5: Set iz0, j-0, ka
and
while iZ n|

L[i]
ar[ktJ-
else

LNDoFLE]
IxooF
[ENOOF
Stp wLle atktn]- L[i++]

tep 9: £ND.
Lode
include <st dio.h>
Void merge (int ant], int lett, int mid, int Yight)

int nJ mid-lett +1 n2 = right -mid;


int Lfal], R3;
tor(i20 knl'; i4*)

=0,i 2 0, Ka left j
while (ieni j<n2){

Jkile Cien)
ar [K+=L[i+t];
hile ('jcn2)

lett nt nut) /
sort (int ar[], int
Vaid me Tg
a(left*right) /2
nt id
sort (omr, lett, midl;
merge
sort Car idtl,right);
mege mid,right
merge (aYr,le tt,
3
int matnl)f
1nt n
printf ("Enter he nvmkr otelerunts: "),

int an nl
Print4(Enter elements: " ;
Scaat("Gar[iJ);
hergesort (ar, o, n-0,
printt ("n),
for(int i=0; i<n; jt)

Output
S
Enter number f eleeats :
emt:

1 3
3! Quiekcort using divide and contuer
|Algonthm
ShShel Parhthn (arC1, beg end)

sterS St ng t end
Shepl! whle lett <ri
repent 'orig] >pvot
rigut --
vepet ar[iett] <epivot 99 lettLright
leftt

temp arr[lett]
anlett} anrIist
Arr [rigut- temp
END OF Lsop

Stee ? retm right


Step 8: End.

Ruicksort(arr, bag end)


Stepl Start
Step2: it bex <end
loc paritm(ar, begena)
&uiksor+(am, beg, en loc-)
Quickss n ( ar, loc41, Qnd).
loc-); begi (a,qwcksart
end); beg (am,parthon loc int
nd)l itnt beg inant],
t (int qu'krteo veid
ightj retrn
tempiArrgut]=
tem tnt
temparrighl
on[righid;
an[lettl; te=mp int
rig)1 (lett i4
<nght) letH (arfteft]<=
99
pivot wile
vot(arLvigkt]
) pi while
end right- beg lett- tnt
or[brg: pivet it
ena)L int beg tnt 0, arporihn
(int tnt
ineladestdto.h
lode
int malnl)
it n )
Pwintf|Enter the hunber f dements! ");

PrittEnter elements! ";


frlint iz 0; i<n} 1+)

Print4(\m");
fr(int ia 0; i<n; i++)
printt"%A" ara);

Enter lemnt? S
S 3

1 3 S
4. Maximy m and
Prach
Miimum ot Asy of ingers vi dini and co ey

Minmax (Avr, bej, end, result t1)


StepJ: Stort
-=end

Yesultfo]- Ar (bes]
result []- Avr [be
rturn
Ske3! it end best)
if ArTbe;] <Arlend]
result]= tr[beg]
result [1]= Ar[and
eise
result[o]> Ar end]

Yetm
Sd mt tep4: Set mid= lle_t end)/2
StpSTnitiali2e lett Res pJ, right ResJ
Step s: on Min Maz (Arr bes id, tett Re s)
Minnar (Ar, mid +!, end, ngttRes)
Stp 3: lett Res lo] & iht ResCoJ
Yesult[o3 - let+Res[o]
else resultlo3-igtRs(o]
if letf Res[l > rigLtResli]
Yesuit 13 left Res[]
result T3" rist Ru(3
Ste to: End,
rigtres
(0; Di3> result
l5 result[J-
Lettyes
[i
rgtresfol; resulH[o]
lse
lettres[J; Yeyulttol2
es
o] r(1ett
[< +
Zo]) res ltrig
riltres); end, t, mid (am
lettres); nid, bey. (ar minmex
rgttres[] [], leftes Ixt
tend)/2 (bes mid> }nt
rem)
erfbsli l+[:3> Yes
arlend]; resulto
ehei
fendJj YsultJ>
omenid3) (arbeg]< H
tuin re
YosulE]2
am
ar[bgl; result
o]
(bgend){ if
(stdio.h inelade
t main(
int n
prntf( Enttr he num ber f elenents: "):

printt ('Entr elents ");


fr (int i-0; icnit)

3
-§ int result[2I ) res t
minma*am, 0, n-l, esfott
Minimvm: n") , result[oJ);
Pritt
"Masimm! Kd\n"?, resuE[iJ);
pintt

6wtput
Enter humbeY ot elment S
Enter emnt: 3 I9 72

Marimnum

You might also like