Fae reverdins for finding specific text or word(s) in the documents such as
] inding text in MS-Word or information downloaded in the web browser window
¢ Finding records or information stored in a sequential file
The sequential search method is not recommended for a large amount of data because
ome more efficient search methods are available for large and complex searches. These search
lnethods are binary search and hash table. \ .. ¢ come le nit y oly
gal4is
an algorithm that searches a value from an array ‘XYZ’ having N elements,
1 START
2 SET LOC =-1
3 INPUT N values into an array XYZ
| INPUT VAL [Enter value that is to be searched)
4 REPEAT Step-5 FOR |= 1 TON
5. IFVAL=XYZ{l] THEN a
- Loc=! RANA Feee
PRINT "Value found at location", LOC.
EXIT
END IF
[End of step-4 Loop]
5. IF LOC =-1 THEN
PRINT "Value not found"
END IF :
7. EXIT
// Program to search a value in an array using sequential search |, - .
| #include
| #include
class seq_search
private; ©
int xyz[5);
public: ;
void input(void); .
; void search(int), ~ yd .
Fis main(void) 1 LeyWrite an algorithm that searches a value in an array XYZ, having N elements and
replaces the searched value with a new one. Use a sequential method to search
START
SET LOC =-1
INPUT N values into array XYZ
INPUT VAL1 {Enter a value to be searched into variable VAL1)
Pens
f
‘array.
| :
“INPUT VAL2 {Enter a value to be replaced with VAL1 inte variable VAL2}
REPEAT Step-6 FOR != 1 TON
Bp. 5.
| 6. IFVAL1=XYZ{l] THEN
| _XYZ{l] = VAL2
Loc=!
PRINT “Value is modified at location’, Loc
: EXIT
= END IF
[End of Step-5 loop]
7. IF LOC =-1 THEN:
PRINT "Value not Found"
EXIT !
END IF we : : 5
Wt Program to seat a value in an array and replace it with another one.
#include'
#include : \
class seq_search 3 :
{ Y = ‘
Bie Private:
i intxyz[5;;
public: .
Void input(vold); ‘
Ro Vold search replaca(int, int);
- » :. 2 a
(malo
\fe STK ‘he largest value]
Write an algorithm that finds the largest value in an array ‘XYZ’ consisting of §
| elements.
4, START
2. INPUT five values into array XYZ
3. [Assigns value of first element of array to MAX, and 1 to C}
MAX = XYZ[0], C = 1
4. [Loop that searches maximum value in an array]
REPEAT Step-5 WHILE C<=4
5. IF MAX < XYZ[C] THEN
MAX = XYZ[C]
c=C+1
END IF
[End of loop of step- 4]
6. PRINT MAX
EXIT
// Program to find the largest value in‘an say using speathach method
#include EW ¢
#include > 5
class max_value "* 55
{ eet A re
private: pele k
int xyz[5];-
publi; Fe ;
void input(void); ’ AS
int search(int&); .
} ‘ Z i any
void main(void) oh ee te 3
{ ’ i a esha §
max_value obj;
int pos, max; j
elrscr(), a ot
| 0bj.Input(); 4 ab
“Max = obj.search(pos);
~, Coutk<"The lataest valud'is: "<»
20 pages First, we open the book from the middle. If the current page is 'e5%%4
= Swe apen ai a page to the right; otherwise, we open at a page to the left.
page 6 is found. In this example, we have a sorted list of sz
mpanison is with the middle page number 10. This eliminates the \ast
pages. as page 6 is less than 10. The second comparison is from page | to page
eliminates page | to page 5, as page 6 is greater than page 5. This 2
continues until page 6 is found. .
+ Searching a specific word in the ica: For searching a particular word im
dictionary, we usually start from the middle in the dictionary. If the word that wea
searching for comes before words on the current page, it shows that the word shoul
te before this page. So we look at the first half. Otherwise, we search for the word
the second half of the dictionary. Suppose the word is in the first half of
dictionary, we consider the first half for looking at the word. We do not need to loot
nto the second half of the dictionary. Thus the data to be searched is divided into hail
of the process. Now we divide this portion into two halves and resume search for the
aad Here we again have to find whether. the word is in the first half or the second
half of this portion. The same step is repeated with the part that contains the required,
word. Finally, we come to the page where the required word exists. We see that in the)
binary search, the search process speeds up becatas of dividing the target list into)
two halves repeatedly. t
se
|
|
ESE |
\
Write a algorithm t that finds a value in an array ‘ABC! consisting of 10 elements, |
sorted in ascending order, Assume-that § represents the first element of the array
and E represents the last element of the array. {
START |
S=1,E=10 {Assign values to variables S & E)
SETLOC=-14 |
INPUT values in array ABC, in ascending order.
Ob }
Snes#5 LY
267
5, ° INPUT VAL [Enter value to be searched in variable VAL]
[Loop that searches value in an array using Binary search}
REPEAT Step-7 and Step-8 WHILE S <= E
MID = (S + E)/2
IF VAL = ABC[MID] THEN
LOC =MID +1
EXIT
ELSE IF VAL < ABC[MID]
E=MID-1
es
on
ELSE
S=MID+1
END IF
[End of Step-6 loop]
IF LOC = -1 THEN
PRINT "Value not found "
ry
ELSE
PRINT. "Value found’at locatio
END IF sot ee imag
1. EXT Sie
‘rogram to search arequired value from aray .y with binary search method
include -
- i
ee 2) haga ota’ lo atp
privet sor soa he ba
int abc[10};
Public; 4
'. ‘void input(void);
i vold,search(int);
id main(void)
bin_search obj; 5 :
intval; f ree eee oss |
clrser();.” ‘ : oa iad aAhatt
ANI innit/\42% 20
4% 20
12% 20
[37 37% 20= 17
—
MU RECUR
Write an algorithm that inserts N integer data values into a hash table and then
searches a specific value from the hash table. Take an array ARR of size N.
START
Initialize ARR array to NULL
INPUT N values into array ARR
REPEAT Step-5 to Step-7 FOR = 1 TON
INPUT data value in VAL
{Compute the index to-store the value into.hash table]
INDEX=VAL/N :
¥. ff the calculated | ‘index hag, empty space, store the value, otherwise display “Collisicn”
message)
IF ARR[INDEX] = NULL THEN
ARRIINDEX] = VAL
ELSE
“PRINT "Collision Occurs"
ENDIF |
{End of Step-4 loop] i
8. INPUT data value to search in X
Oo hwo
9. {Search the value from hash table by re pamputiog its index.in hash ee
“INDEX =X/N
10. “IF ARR[INDEX] = NULL THEN
| PRINT "Value not found”,
| ~ ELSE
| oe PRINT "Data value is” MARINE
| ENDIF.
11. EXIT
PrnEe : ; rite
// program to input 10 i integer values into a hash table and search a specific value from
| -I/ the hash table
| #include yoid hash_integers::search(int n) amas
index = n % 10;
if(hash_tablefindex) = n || hash_tablefindex} == NULL)
cout<<"Value not found”.
else
cout<<"Value found:” <=1) [Start of upper loop]
4 SETI=1
5. REPEAT STEP-6 TO 7 WHILE (I<=U) {Start of inner loop]
6. _IF ABC[I] > ABC[I+!] THEN [Interchange values]
TEMP = ABC{I] :
ABCTI] = ABCI+1]
ABC[I+41] = TEMP
END IF :
7.5 VSV# AS © * [End of inner loop) 3 >
. USU-4 {End of Upper loop] =
9 EXT ok ‘ :
Mt Progra to sort an array with 5 elements using bubble sort method.
#include ;
#include
class bubble
{ ‘
Private: © * Pry tek as
int abofhj, es Pt Ss
public: * parece nie RE
bs void input(void);). °° os oot
7+... Wold sort(void); pees
; vold print(vold);. " }
h y Se ey S04 fe
void main(void) PSs
fee4 og
*~ bubble obj;
clrser(); wed :
f° objinput(), yay
obj.son(),
obj. print();
getch(),
Y Pca eget143 fot 7 | 2 a a
Before Swapping Aner Swapping
Pass -4:
The array is searched starting from the fourth element to the last element i.e. ely
and S™. The clement that has the smallest value is selected. The smallest val,
position 4, Since 7 is a smaller value and it is already located at location 4, y
exchange the values, The array after this pass is shown below. We get the sq
after the last pass. F
Ceri) Llslel7] 2!
Before Swapping Re After Swapping. ©
Write an algorithm to sort an array.ABC in: ascending order using the sel
method. The array consists of N elements. Puta e ae
START a .
“SET U=0 ~ .. [Urepresents the control variable Used. for upper loop}
REPEAT STEPS 4 T0.10 WHILE (U ABC[I] THEN » ~ : abo ee
“MINI = ABC. § ISS as OS
Loc =| shes gees fest :
NO Ponp >
ns ENDIF . ~
18 La bean: eaNS :
+ [End of Step-6 loop innier Loop} 524
9. ° [interchange values) res
f TEMP = ABCILOC]
“ABCILOC] = ABCIU]
zl *ABC[U] = TEMP
10. USUFT.. se
a [End of Step-3 loop -—- upper Loop] ©."
Me EAM sy es
(ERIE Me ain yy Uae ae ue aay
‘.f Program to sort array with 5 elements using selection sort method."
ib egg . A aesvalue of A[O] So shift the values of clemenis » . 7 TB
jnsent the value of A[2] into AO] The array, before and alles paooré 15 Shown belo!
I . . |
CTT] U[sJe2fs)
Compare the value of the fourth element of the array with the values of elements by,
and insert it in the proper position. In this case, the value of A[3] is smaller thy,
value of A[2]. So shift the value of element from A(2] one position to the right ang
the value of A(3] into A[2]. The array, before and after pass-3 is shown below:
Py dy
ET Te lol EEE
Pass-4: : :
Compare the value of the fifth element of the array with the values of elements be
and insert it in the proper position: In this case, the value of A[4] is smaller
value of A[3]. So shift the value of element from A[3]} one Position to the right and
the value of A[4] into A[3]. The array, before and after pass-4 is shown below
Write an algorithm to sort an'array ‘ABC in, ascending order using the. int
sort method, The array consists of N elements. Sa
1. START. paces : :
2 INPUT data into array ABC
3. [ Start of upper Loop ] (
REPEAT. Step-4 TO 8 FOR.U 1-TO Neter FOF Oe
a
4. VAL = ABC[U]
5 teu
6. _ [Start of inner Loop ]
REPEAT Step-7 WHILE(I>=0 AND VAL boos :
foot: ; Sa ‘4 :
_ #include :
quicksori(intQ, int int);
r int partition(int 4, int, ‘int y
“void main(void)
Wa ce:change their order
PE) EE) ©
36 41 | 45
2 In the second step, we compare lists of two data values and merge them into another
of data Values placing all in sorted order
19°) 45 | 29 | 34 17 ] 36 | a1 45
3. In the third step, we compare lists of four data values, and merge them into another lis
date values We get the sorted list of data values.
15/17 | 29 | 34 | 36 | 41 | 45
A os RS LULL ‘
Write an algorithm that sorts an array ARR which consists of N elememes
merge sort.
+ START rank
2 \F array ARR has only one element, itis already sorted, THEN
EXIT : ‘. *
ELSE ' ee
Dividé the array recursively into two halves until it cannot be *
divided ’
END IF a
3. Merge the smaller sub-arrays into a new array in sorted order :
4 EXIT ey, :
When an array of an odd number of elements is divided into two sub-arrays. them
second sub-array will have one more element than the first sub-array. For example, the array
clements 8 is divided into two sub-arrays of equal lengths, i.e, each having 4 elements. Howes
f the length of an array is 9, ‘then the first sub-array will have 4 elements and the second 2
vill have 5 elements,
//Program to sort array with 6 elements by using merge sorting methog
#include > *
#include
void MergeSort(int (, int, int);
void Merge(int (}, int, int, int); c