0% found this document useful (0 votes)
3 views1 page

Random Search Algorithm in Sorted Array

The document outlines a tutorial with three exercises focused on algorithms and data structures. Exercise 1 involves developing a random search algorithm for a value in a sorted array, while Exercise 2 requires displaying file contents in a fixed format. Exercise 3 discusses redistributing records evenly across blocks in a file.

Uploaded by

bouchramerah91
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Random Search Algorithm in Sorted Array

The document outlines a tutorial with three exercises focused on algorithms and data structures. Exercise 1 involves developing a random search algorithm for a value in a sorted array, while Exercise 2 requires displaying file contents in a fixed format. Exercise 3 discusses redistributing records evenly across blocks in a file.

Uploaded by

bouchramerah91
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sidi Bel Abbes on 16/10 /2024

FILE STRUCTURE AND DATA STRUCTURE

Tutorial : 3

Exercise 1
Develop an algorithm to find a value V in a sorted array T[1..N] using the following
steps:
 a = 1 and b = N initialize the search range.
 In each iteration, a random index i is generated between a and b using the Rand
(n) function.
 If T[i] equals V, the search ends successfully by returning the index .
 If V is greater than T[i], the search continues in the range [i+1,b].
 If V is less than T[i], the search continues in the range [a,i−1].
 The process stops either when V is found or when a exceeds b, in which case V
is not present in the array.

You can call this function with an ordered array T, the size of the array N, and the value
V you want to search for. If the value is found, the function will return its position
(based index), otherwise, it will return -1.

Exercise 2
We have a buffer in main memory (buf). Display the contents of the file (F) arranged in
a fixed format (TOF), knowing that all the records are stored consecutively.

Exercise 3
Spread the records evenly across consecutive blocks i, i+1, and i+2 of a TOF file (a
ordered array of blocks with a fixed record format). For example, if the blocks initially
hold 10, 4, and 8 records, after redistribution, they will contain 8, 7, and 7 records,
respectively.

Page 1

You might also like