Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

DS-Linear search and binary search

The document outlines algorithms for sequential and binary search methods to find an item X in an array. It includes step-by-step instructions for both algorithms and provides corresponding C++ code implementations. The sequential search operates on an unsorted array, while the binary search requires the array to be sorted in ascending order.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DS-Linear search and binary search

The document outlines algorithms for sequential and binary search methods to find an item X in an array. It includes step-by-step instructions for both algorithms and provides corresponding C++ code implementations. The sequential search operates on an unsorted array, while the binary search requires the array to be sorted in ascending order.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Algorithm - Sequential Search

Write an algorithm to find an item X in an array A


consisting of N elements.
1. SET LOC = -1
2. INPUT X
3. REPEAT STEP 4 FOR I = 1 TO N
4. IF X == A[I] THEN
o LOC = I
o PRINT "Data found at location: LOC"
o EXIT
5. IF LOC == -1 THEN
o PRINT "Data not found."
o EXIT
C++ Code for Sequential Search
#include <iostream>
int main()
using namespace std;
class SeqSearch { {
private: SeqSearch obj;
int arr[10]; int item;
public:
void input() { obj.input();
cout << "Enter required value to search: ";
cout << "Enter 10 values: ";
cin >> item;
for (int i = 0; i < 10; i++)
cin >> arr[i]; obj.search(item);
} return 0;}
void search(int item) {
int loc = -1;
for (int i = 0; i < 10; i++) {
if (arr[i] == item) {
loc = i + 1;
cout << "Data found at
location: " << loc << endl;
return;
}
}
cout << "Data not found" << endl
;
}
};
BINARY SEARCH Algorithm
Write an algorithm to find an item X in an ascending sorted array A of size N
.
1. SET LOC = -1
2. INPUT X
3. SET LOW = 1, HIGH = N
4. REPEAT WHILE LOW <= HIGH
o MID = (LOW + HIGH) / 2
o IF X == A[MID]
LOC = MID
PRINT "Item found at location: LOC"
EXIT
o ELSE IF X > A[MID]
LOW = MID + 1
o ELSE
HIGH = MID - 1
5. IF LOC == -1 THEN
o PRINT "Item not found."
o EXIT
C++ Code for Binary Search cout << "Required value found at location: " << loc << endl;
#include <iostream> return;
using namespace std; } else if (item > arr[mid]) {
low = mid + 1;
class BinSearch {
} else {
private:
high = mid - 1;
int arr[10];
}
public: }
void input() { cout << "Data not found" << endl;
cout << "Enter 10 sorted values: "; }
for (int i = 0; i < 10; i++) };
cin >> arr[i]; int main() {
} BinSearch obj;
int item;
void search(int item) {
int low = 0, high = 9, mid, loc = -1; obj.input();

while (low <= high) { cout << "Enter value to search: ";
mid = (low + high) / 2; cin >> item;
if (arr[mid] == item) {
obj.search(item);
loc = mid + 1;
return 0;
}

You might also like