Implement algorithms A1 and A2 in C/C++ Write a C/C++ program that makes use of the above implementations as a Your program should input the array from the user. After implementation edit the code that shows after how many number of iteration searched value is found

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 29SA
icon
Related questions
Question

OBJECT

 

Lab Session 03

 

Application of the Linear and binary search on a list of elements stored in an array.

 

THEORY

 

Linear Search

A nonempty array DATA with N numerical values is given. This algorithm finds the location LOC and required value of elements of DATA. The variable K is used as a counter.

ALGORITHM

 

1. Set k := 1 & loc : = 0

  1. Repeat step 3 & 4 while loc : = 0 &k < = n
  2. If (item = data[k]) loc : = k

Else

K = k + 1

  1. If loc > = 0 or If (item = data[k]) then

Print “loc is the location of item”

Else

Print “no. not found”

  1. Exit

 

Binary Search

Suppose DATA is an array that is sorted in increasing (or decreasing) numerical order or, equivalently, alphabetically. Then there is an extremely efficient searching algorithm, called binary search, which can be used to find the location LOC of a given ITEM of information in DATA.

 

The binary search algorithm applied to our array DATA works as follows: During each stage of our algorithm, our search for ITEM is reduced to a segment of elements of DATA: DATA[BEG], DATA[BEG+1], DATA[BEG+2], . . ., DATA[END]

 

Note that the variables BEG and END denote, respectively, the beginning and end locations of the segment under consideration. The algorithm compares ITEM with the middle element DATA[MID] of the segment, where MID is computed as:

MID = INT((BEG + END) / 2) (INT(A) refers to the integer value of A.)

If DATA[MID] = = ITEM, then the search is successful and we set LOC = MID.

 

Otherwise a new segment of DATA is obtained as follows:

 

  1. If ITEM < DATA[MID], then ITEM can appear only in the left half of the segment: DATA[BEG], DATA[BEG+1], . . . , DATA[MID-1]

So we reset END = MID –1 and begin searching again.

  1. If ITEM > DATA[MID], then ITEM can appear only in the right half of the segment: DATA[MID+1], DATA[MID+2], . . . , DATA[END]

So we reset BEG = MID +1 and begin searching again.

 

Initially, we begin with the entire array DATA; i.e., we begin with BEG = 0 and END = N-1. If ITEM is not in DATA, then eventually we obtain BEG > END.

This condition signals that the search is unsuccessful, and in such a case we assign LOC = NULL. Here NULL is a value that lies outside the set of indices of DATA.

 

ALGORITHM

Algorithm: BINSEARCH(DATA, ITEM)

  1. Set BEG = 0, END = N-1 and MID = INT((BEG + END) / 2)
  2. Repeat steps 3 and 4 while BEG <= END AND DATA[MID] != ITEM
  3. If ITEM < DATA[MID], then Set END = MID – 1, Else Set BEG = MID + 1
  4. MID = INT((BEG + END) / 2)
  5. If DATA[MID] = ITEM, then Set LOC = MID Else Set LOC = NULL
  6. Exit

 

LAB TASKS

  1. Implement algorithms A1 and A2 in C/C++
  2. Write a C/C++ program that makes use of the above implementations as a Your program should input the array from the user.
  3. After implementation edit the code that shows after how many number of iteration searched value is found

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning