C++ Program for Linear and Binary Search in an Array, Assignments of Data Structures and Algorithms

A C++ program for implementing linear and binary search algorithms to find a given number in an array. The user can input the array size and values, and then use the menu to choose between linear and binary search. The program displays the location of the number if found, or a message indicating that it was not found.

Typology: Assignments

2020/2021

Uploaded on 04/06/2021

riaz-ahmad-shigri
riaz-ahmad-shigri 🇵🇰

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include<iostream>
int menuss() {
int choice;
std::cout << " ----------------------------- " << std::endl;
std::cout << " MENU " << std::endl;
std::cout << " ----------------------------- " << std::endl;
std::cout << " press 1 for Input array values " << std::endl;
std::cout << " press 2 for linear Search " << std::endl;
std::cout << " press 3 for binary Search " << std::endl;
std::cin >> choice;
return choice;
}
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C++ Program for Linear and Binary Search in an Array and more Assignments Data Structures and Algorithms in PDF only on Docsity!

#include int menuss() { int choice; std::cout << " ----------------------------- " << std::endl; std::cout << " MENU " << std::endl; std::cout << " ----------------------------- " << std::endl; std::cout << " press 1 for Input array values " << std::endl; std::cout << " press 2 for linear Search " << std::endl; std::cout << " press 3 for binary Search " << std::endl; std::cin >> choice; return choice; }

int linearSearch(int a[], int size, int search_num) { for (int i = 0; i < size; i++) { if (a[i] == search_num) { return i; } else return -1; } }

int binarySearch(int a[], int first, int last, int search_num) { int middle; if (last >= first) { middle = (first + last) / 2; if (a[middle] == search_num) { return middle + 1; } else if (a[middle] < search_num) { return binarySearch(a, middle + 1, last, search_num); } else { return binarySearch(a, first, middle - 1, search_num); } } return -1;

case 1: { system("cls"); std::cout << " ----------------------------- " << std::endl; std::cout << " sTORE ARRAY VALUES " << std::endl; std::cout << " ----------------------------- " << std::endl; for (int i = 0; i < size; i++) { std::cout << "Please enter value for location " << i << std::endl; std::cin >> num[i]; } break; } case 2: { system("cls"); std::cout << " ----------------------------- " << std::endl; std::cout << " LINEAR SEARCH " << std::endl; std::cout << " ----------------------------- " << std::endl; int search_num, loc = -1; std::cout << "Enter the number that you want to search: linear search " << std::endl; std::cin >> search_num;

loc = linearSearch(num, size, search_num); if (loc != -1) { std::cout << search_num << " found in the array at the location: " << loc << std::endl; } else { std::cout << " not found through linear search" << std::endl;; } break; } case 3: { system("cls"); std::cout << " ----------------------------- " << std::endl; std::cout << " BINARY SEARCH " << std::endl; std::cout << " ----------------------------- " << std::endl; int search_num, loc = -1; std::cout << "Enter the number that you want to search: through binary " << std::endl; std::cin >> search_num; loc = binarySearch(num, 0, size, search_num);