0% found this document useful (0 votes)
2 views

DAA Lab File

The document contains implementations of four different search and sorting algorithms in C++: linear search, binary search, heap sort, and merge sort. Each algorithm is presented with its respective code, demonstrating how to find elements in an array or sort an array. The main function for each algorithm showcases example usage and outputs the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DAA Lab File

The document contains implementations of four different search and sorting algorithms in C++: linear search, binary search, heap sort, and merge sort. Each algorithm is presented with its respective code, demonstrating how to find elements in an array or sort an array. The main function for each algorithm showcases example usage and outputs the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LINEAR SEARCH

#include <iostream>
using namespace std;

int linearSearch(int arr[], int size, int key, int index = 0) {


if (index == size)
return -1;
if (arr[index] == key)
return index;
return linearSearch(arr, size, key, index + 1);
}

int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 30;

int result = linearSearch(arr, size, key);


if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found" << endl;

return 0;
}
BINARY SEARCH

#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] > x) {
return binarySearch(arr, low, mid - 1, x);
}
return binarySearch(arr, mid + 1, high, x);
return -1;
}

int main() {
int arr[] = { 2, 3, 4, 10, 40 };
int query = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, query);
if (result == -1)
cout << "Element is not present in array";
else
cout << "Element is present at index " << result;
return 0;
}
HEAP SORT

#include <bits/stdc++.h>
using namespace std;
void heapify(vector<int>& arr, int n, int i){
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(vector<int>& arr) {


int n = arr.size();
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void printArray(vector<int>& arr){
for (int i = 0; i < arr.size(); ++i)
cout << arr[i] << " ";
cout << "\n";
}

int main(){
vector<int> arr = {9, 4, 3, 8, 10, 2, 5};
cout << "Array Before Sorting : \n";
printArray(arr);
heapSort(arr);
cout << "Sorted array is \n";
printArray(arr);
}
MERGE SORT

#include <bits/stdc++.h>
using namespace std;
void merge(vector<int>& arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
void mergeSort(vector<int>& arr, int left, int right) {
if (left >= right)
return;
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}

void printVector(vector<int>& arr) {


for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
cout << endl;
}

int main() {
vector<int> arr = {12, 11, 13, 5, 6, 7};
int n = arr.size();
cout << "Given vector is \n";
printVector(arr);
mergeSort(arr, 0, n - 1);
cout << "\nSorted vector is \n";
printVector(arr);
return 0;
}

You might also like