0% found this document useful (0 votes)
2 views5 pages

Data Structure Lab Assignment

The document contains six C++ programs demonstrating different search algorithms: linear search (both iterative and class-based) and binary search (both iterative and class-based). Each program initializes an array, searches for a specified element, and outputs the index of the element or a message if not found. The linear search programs handle unsorted arrays, while the binary search programs require sorted arrays.

Uploaded by

zayan aamir
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)
2 views5 pages

Data Structure Lab Assignment

The document contains six C++ programs demonstrating different search algorithms: linear search (both iterative and class-based) and binary search (both iterative and class-based). Each program initializes an array, searches for a specified element, and outputs the index of the element or a message if not found. The linear search programs handle unsorted arrays, while the binary search programs require sorted arrays.

Uploaded by

zayan aamir
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

Program:1

#include <iostream>
using namespace std;

int main() {
int arr[] = { 5, 7, 1, 3, 9, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
int index = -1;
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
index = i;
break;
}
}
if (index == -1) {
cout << "Element not found" << endl;
}
else {
cout << "Element found at index " << index << endl;
}
return 0;
}

Program:2
#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int x);

int main() {
int arr[] = { 5, 7, 1, 3, 9, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
int index = linearSearch(arr, n, x);
if (index == -1) {
cout << "Element not found" << endl;
}
else {
cout << "Element found at index " << index << endl;
}
return 0;
}

int linearSearch(int arr[], int n, int x) {


for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}

Program:3
#include <iostream>
using namespace std;

class LinearSearch {
private:
int* arr;
int n;
public:
LinearSearch(int a[], int s) {
arr = new int[s];
n = s;
for (int i = 0; i < n; i++) {
arr[i] = a[i];
}
}
int search(int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
~LinearSearch() {
delete[] arr;
}
};

int main() {
int arr[] = { 5, 7, 1, 3, 9, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
LinearSearch ls(arr, n);
int index = [Link](x);
if (index == -1) {
cout << "Element not found" << endl;
}
else {
cout << "Element found at index " << index << endl;
}
return 0;
}
Program:4
#include <iostream>
using namespace std;

int main() {
int arr[] = { 1, 3, 4, 5, 7, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 5;
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
cout << "Element found at index " << mid << endl;
return 0;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
cout << "Element not found" << endl;
return 0;
}

Program:5
#include <iostream>
using namespace std;

int binarySearch(int arr[], int n, int x) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}

int main() {
int arr[] = { 1, 3, 4, 5, 7, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 5;
int index = binarySearch(arr, n, x);
if (index == -1) {
cout << "Element not found" << endl;
}
else {
cout << "Element found at index " << index << endl;
}
return 0;
}

Program:6
#include <iostream>
using namespace std;

class BinarySearch {
private:
int* arr;
int n;
public:
BinarySearch(int size) {
n = size;
arr = new int[n];
}
void input() {
cout << "Enter " << n << " elements in sorted order:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
}
int search(int x) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
~BinarySearch() {
delete[] arr;
}
};

int main() {
int n = 6;
BinarySearch bs(n);
[Link]();
int x = 5;
int index = [Link](x);
if (index == -1) {
cout << "Element not found" << endl;
}
else {
cout << "Element found at index " << index << endl;
}
return 0;
}

You might also like