0% found this document useful (0 votes)
18 views4 pages

IDSA Assignment 5

The document outlines an assignment for a student named Aditya Sirsat from Vishwakarma Institute of Information Technology, focusing on programming tasks related to student roll numbers. It includes functions for linear search, binary search, and bubble sort to manage and search through an array of roll numbers. The provided C++ code implements these functionalities, allowing users to input roll numbers and search for a specific roll number using both search methods.

Uploaded by

adityasirsat1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

IDSA Assignment 5

The document outlines an assignment for a student named Aditya Sirsat from Vishwakarma Institute of Information Technology, focusing on programming tasks related to student roll numbers. It includes functions for linear search, binary search, and bubble sort to manage and search through an array of roll numbers. The provided C++ code implements these functionalities, allowing users to input roll numbers and search for a specific roll number using both search methods.

Uploaded by

adityasirsat1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

You might also like