Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Information
Technology
Department of
Artificial Intelligence and Data
Science
Student Name: Aditya
Sirsat
Class: F.Y. B. Tech Division: B (B2) Roll No.: 107235
Semester: 2nd Semester Academic Year: 2023-24
Subject Name & Code: IDSA
Assignment No: 5
Program to store roll numbers of student in array who attended
training program on Data Structure in random order. Write
function to perform following operations
1. Search roll no is present or not using Linear Search
2. Search roll no is present or not using Binary Search
3. Sort stored roll no using Bubble Sort
Date of Performance: Date of Submission:
Code:
#include <iostream>
#include <algorithm>
using namespace std;
bool linearSearch(int arr[], int n, int key) { // Function to perform linear search
for (int i = 0; i < n; ++i) {
if (arr[i] == key) {
return true;
}}
return false;
bool binarySearch(int arr[], int n, int key) { // Function to perform binary search (assumes array is sorted)
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return true;}
if (arr[mid] < key) {
left = mid + 1;}
else {
right = mid - 1; }
} return false;
void bubbleSort(int arr[], int n) { // Function to perform bubble sort
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}}}}
int main() {
const int maxSize = 100;
int rollNumbers[maxSize];
int n;
cout << "Enter the number of students: ";
cin >> n;
cout << "Enter roll numbers of students:\n";
for (int i = 0; i < n; ++i) {
cin >> rollNumbers[i];
int key;
cout << "Enter roll number to search: ";
cin >> key;
if (linearSearch(rollNumbers, n, key)) { // Linear search
cout << "Roll number " << key << " found using linear search.\n";
} else {
cout << "Roll number " << key << " not found using linear search.\n";
bubbleSort(rollNumbers, n); // Sorting before binary search
if (binarySearch(rollNumbers, n, key)) {
cout << "Roll number " << key << " found using binary search.\n";
} else {
cout << "Roll number " << key << " not found using binary search.\n";
bubbleSort(rollNumbers, n); // Sorting roll numbers using bubble sort
cout << "Sorted roll numbers:\n";
for (int i = 0; i < n; ++i) {
cout << rollNumbers[i] << " ";
}cout << endl;
return 0;
}
Output: