Title | Linearybinary - Perform linear and binary search in c++ |
---|---|
Author | Mukund Choudhary |
Course | Master of computer applications |
Institution | Chandigarh University |
Pages | 6 |
File Size | 350.7 KB |
File Type | |
Total Downloads | 66 |
Total Views | 170 |
Perform linear and binary search in c++...
Linear And Binary Search
Student Name: Mukund Kumar Choudhary Branch: MCA Semester: 1st Subject Name: Design & Analysis Of Algorithms Lab Subject Code: 21CAP606
UID: 21MCA2147 Section/Group: Section 1 Group B Date of Performance: 08/09/2021
1. Aim/Overview of the practical:
Perform linear and binary search. Should have option to put the element of user’s choice. Should have option to choose which search method user wants to perform. Should have output.
2. Task to be done: Demonstrate the functioning of Linear Search and Binary Search with the help of a program.
3. Algorithm/Flowchart : For Linear Search: Linear Search ( Array A, KeyElement x)
Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit
For Binary Search: Compare keyElement with the middle element. If keyElement matches with the middle element,we return the mid index. Else If keyElement is greater than the mid element,then keyElement can only in the right half subarray after the mid element. So we recur for the right half. Else (keyElement is smaller) recur for the left half.
4. Dataset: For Linear: [5,6,10,80,64,75,78,15], Key element: 16 For Binary: [7,6,80,45,64,83,45,65] Key element: 66
5. Code for experiment/practical: #include #include using namespace std; int linearsearch(int a[20],int key,int n); int binarysearch(int a[20],int key,int n); int main() { int a[20],k,n,ch,ans,i; coutn; while(1){ cout...