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

CORE - 14 - Algorithm Design Techiniques

The document contains C++ implementations of various algorithms including Linear Search, Binary Search, Merge Sort, Quick Sort, Heap Sort, Breadth-First Search (BFS), Depth-First Search (DFS), Bubble Sort, Insertion Sort, and Selection Sort. Each algorithm is presented with code snippets, example outputs, and explanations of their functionality. The document serves as a comprehensive guide for understanding and implementing these fundamental algorithms in C++.

Uploaded by

subhampallai7
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)
3 views

CORE - 14 - Algorithm Design Techiniques

The document contains C++ implementations of various algorithms including Linear Search, Binary Search, Merge Sort, Quick Sort, Heap Sort, Breadth-First Search (BFS), Depth-First Search (DFS), Bubble Sort, Insertion Sort, and Selection Sort. Each algorithm is presented with code snippets, example outputs, and explanations of their functionality. The document serves as a comprehensive guide for understanding and implementing these fundamental algorithms in C++.

Uploaded by

subhampallai7
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/ 14

1.

Write a Linear Search Algorithm in C++

#include <iostream>​
using namespace std;​


int linearSearch(int arr[], int size, int key) {​
for(int i = 0; i < size; i++) {​
if(arr[i] == key) {​
return i; }​
}​
return -1; ​
}​

int main() {​
int arr[] = {5, 3, 8, 6, 7};​
int size = sizeof(arr) / sizeof(arr[0]);​
int key;​

cout << "Enter the number to search: ";​
cin >> key;​

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

if(result != -1) {​
cout << "Element found at index " << result << endl;​
} else {​
cout << "Element not found in the array." << endl;​
}​

return 0;​
}

OUTPUT :

Enter the number to search: 8​


Element found at index 2​

Enter the number to search: 10​
Element not found in the array.
2.Write a Binary Search Algorithm in C++

#include <iostream>​
using namespace std;​


int binarySearch(int arr[], int size, int key) {​
int low = 0, high = size - 1;​

while(low <= high) {​
int mid = (low + high) / 2;​

if(arr[mid] == key)​
return mid;
else if(arr[mid] < key)​
low = mid + 1;
else​
high = mid - 1;
}​

return -1;
}​

int main() {​
int arr[] = {2, 4, 6, 8, 10, 12, 14};
int size = sizeof(arr) / sizeof(arr[0]);​
int key;​

cout << "Enter the number to search: ";​
cin >> key;​

int result = binarySearch(arr, size, key);​

if(result != -1)​
cout << "Element found at index " << result << endl;​
else​
cout << "Element not found in the array." << endl;​

return 0;​
}​

OUTPUT :

Enter the number to search: 10​


Element found at index 4​

Enter the number to search: 5​
Element not found in the array.

3.Write a Merge Sort Algorithm in C++

#include <iostream>​
using namespace std;​

// Merge two halves​
void merge(int arr[], int left, int mid, int right) {​
int n1 = mid - left + 1; // size of left half​
int n2 = right - mid; // size of right half​

// Create temporary arrays​
int L[n1], R[n2];​

// Copy data to temp arrays​
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];​

// Merge the temp arrays back into arr[left..right]​
int i = 0, j = 0, k = left;​
while(i < n1 && j < n2) {​
if(L[i] <= R[j]) {​
arr[k] = L[i];​
i++;​
} else {​
arr[k] = R[j];​
j++;​
}​
k++;​
}​

// Copy remaining elements of L[], if any​
while(i < n1) {​
arr[k] = L[i];​
i++;​
k++;​
}​

// Copy remaining elements of R[], if any​
while(j < n2) {​
arr[k] = R[j];​
j++;​
k++;​
}​
}​

// Merge sort function​
void mergeSort(int arr[], int left, int right) {​
if(left < right) {​
int mid = (left + right) / 2;​

// Sort first and second halves​
mergeSort(arr, left, mid);​
mergeSort(arr, mid + 1, right);​

// Merge the sorted halves​
merge(arr, left, mid, right);​
}​
}​

// Print array​
void printArray(int arr[], int size) {​
for(int i = 0; i < size; i++)​
cout << arr[i] << " ";​
cout << endl;​
}​

int main() {​
int arr[] = {9, 4, 6, 2, 10, 1};​
int size = sizeof(arr) / sizeof(arr[0]);​

cout << "Original array: ";​
printArray(arr, size);​

mergeSort(arr, 0, size - 1);​

cout << "Sorted array: ";​
printArray(arr, size);​

return 0;​
}
OUTPUT :
Original array: 9 4 6 2 10 1 ​
Sorted array: 1 2 4 6 9 10

4. Write a Quick Sort Algorithm in C++

#include <iostream>​
using namespace std;​


int partition(int arr[], int low, int high) {​
int pivot = arr[high]; ​
int i = low - 1; ​

for(int j = low; j < high; j++) {​
if(arr[j] < pivot) {​
i++;​

swap(arr[i], arr[j]);​
}​
}​


swap(arr[i + 1], arr[high]);​

return i + 1; ​
}​


void quickSort(int arr[], int low, int high) {​
if(low < high) {​

int pi = partition(arr, low, high);​


quickSort(arr, low, pi - 1);​
quickSort(arr, pi + 1, high);​
}​
}​


void printArray(int arr[], int size) {​
for(int i = 0; i < size; i++)​
cout << arr[i] << " ";​
cout << endl;​
}​

int main() {​
int arr[] = {7, 2, 1, 6, 8, 5, 3, 4};​
int size = sizeof(arr) / sizeof(arr[0]);​

cout << "Original array: ";​
printArray(arr, size);​

quickSort(arr, 0, size - 1);​

cout << "Sorted array: ";​
printArray(arr, size);​

return 0;​
}

OUTPUT :

Original array: 7 2 1 6 8 5 3 4 ​
Sorted array: 1 2 3 4 5 6 7 8

5. Write a Heap Sort Algorithm in C++

#include <iostream>​
using namespace std;​

// Function to heapify a subtree rooted at index i​
void heapify(int arr[], int n, int i) {​
int largest = i; // Initialize largest as root​
int left = 2 * i + 1; // left child index​
int right = 2 * i + 2;// right child index​

// If left child is larger than root​
if(left < n && arr[left] > arr[largest])​
largest = left;​

// If right child is larger than largest so far​
if(right < n && arr[right] > arr[largest])​
largest = right;​

// If largest is not root​
if(largest != i) {​
swap(arr[i], arr[largest]); // Swap root with largest​
heapify(arr, n, largest); // Recursively heapify the affected
sub-tree​
}​
}​

// Heap sort function​
void heapSort(int arr[], int n) {​
// Build max heap​
for(int i = n / 2 - 1; i >= 0; i--)​
heapify(arr, n, i);​

// Extract elements from heap one by one​
for(int i = n - 1; i > 0; i--) {​
swap(arr[0], arr[i]); // Move current root to end​
heapify(arr, i, 0); // Heapify the reduced heap​
}​
}​

// Print the array​
void printArray(int arr[], int size) {​
for(int i = 0; i < size; i++)​
cout << arr[i] << " ";​
cout << endl;​
}​

int main() {​
int arr[] = {12, 11, 13, 5, 6, 7};​
int size = sizeof(arr) / sizeof(arr[0]);​

cout << "Original array: ";​
printArray(arr, size);​

heapSort(arr, size);​

cout << "Sorted array: ";​
printArray(arr, size);​

return 0;​
}
OUTPUT :

Original array: 12 11 13 5 6 7 ​
Sorted array: 5 6 7 11 12 13

6. Write a Program to implement BFS in C++


#include <iostream>​
#include <vector>​
#include <queue>​
using namespace std;​

class Graph {​
int V; // number of vertices​
vector<vector<int>> adj;​

public:​
Graph(int v) {​
V = v;​
adj.resize(V);​
}​

void addEdge(int u, int v) {​
adj[u].push_back(v); // Add v to u's list​
adj[v].push_back(u); // For undirected graph​
}​

void BFS(int start) {​
vector<bool> visited(V, false);​
queue<int> q;​

visited[start] = true;​
q.push(start);​

cout << "BFS Traversal starting from node " << start << ": ";​

while (!q.empty()) {​
int node = q.front();​
q.pop();​
cout << node << " ";​

for (int neighbor : adj[node]) {​
if (!visited[neighbor]) {​
visited[neighbor] = true;​
q.push(neighbor);​
}​
}​
}​

cout << endl;​
}​
};​

int main() {​
Graph g(6);​

g.addEdge(0, 1);​
g.addEdge(0, 2);​
g.addEdge(1, 3);​
g.addEdge(2, 4);​
g.addEdge(3, 5);​
g.addEdge(4, 5);​

g.BFS(0);​

return 0;​
}​

OUTPUT :

BFS Traversal starting from node 0: 0 1 2 3 4 5

7. Write a Program to implement DFS in C++

#include <iostream>​
#include <vector>​
using namespace std;​

class Graph {​
int V; // Number of vertices​
vector<vector<int>> adj;​

public:​
Graph(int v) {​
V = v;​
adj.resize(V);​
}​

void addEdge(int u, int v) {​
adj[u].push_back(v); // Add v to u's list​
adj[v].push_back(u); // For undirected graph​
}​

void DFSUtil(int node, vector<bool>& visited) {​
visited[node] = true;​
cout << node << " ";​

for (int neighbor : adj[node]) {​
if (!visited[neighbor]) {​
DFSUtil(neighbor, visited);​
}​
}​
}​

void DFS(int start) {​
vector<bool> visited(V, false);​
cout << "DFS Traversal starting from node " << start << ": ";​
DFSUtil(start, visited);​
cout << endl;​
}​
};​

int main() {​
Graph g(6);​

g.addEdge(0, 1);​
g.addEdge(0, 2);​
g.addEdge(1, 3);​
g.addEdge(2, 4);​
g.addEdge(3, 5);​
g.addEdge(4, 5);​

g.DFS(0);​

return 0;​
}​

OUTPUT :
DFS Traversal starting from node 0: 0 1 3 5 4 2
8. Write a Program to implement Bubble Sort in C++

#include <iostream>​
using namespace std;​

void bubbleSort(int arr[], int n) {​
for (int i = 0; i < n - 1; i++) {​
bool swapped = false;​

for (int j = 0; j < n - i - 1; j++) {​
// Swap if the element is greater than the next one​
if (arr[j] > arr[j + 1]) {​
swap(arr[j], arr[j + 1]);​
swapped = true;​
}​
}​

// If no two elements were swapped, the array is already sorted​
if (!swapped)​
break;​
}​
}​

void printArray(int arr[], int n) {​
for (int i = 0; i < n; i++)​
cout << arr[i] << " ";​
cout << endl;​
}​

int main() {​
int arr[] = {64, 34, 25, 12, 22, 11, 90};​
int n = sizeof(arr)/sizeof(arr[0]);​

cout << "Original array: ";​
printArray(arr, n);​

bubbleSort(arr, n);​

cout << "Sorted array: ";​
printArray(arr, n);​

return 0;​
}​
OUTPUT :
Original array: 64 34 25 12 22 11 90 ​
Sorted array: 11 12 22 25 34 64 90

9. Write a Program to implement Insertion Sort in C++

#include <iostream>​
using namespace std;​

void insertionSort(int arr[], int n) {​
for (int i = 1; i < n; i++) {​
int key = arr[i]; // Current element to be inserted​
int j = i - 1;​

// Shift elements of arr[0..i-1] that are greater than key​
while (j >= 0 && arr[j] > key) {​
arr[j + 1] = arr[j]; // Move element one position ahead​
j--;​
}​

arr[j + 1] = key; // Insert key into the correct position​
}​
}​

void printArray(int arr[], int n) {​
for (int i = 0; i < n; i++)​
cout << arr[i] << " ";​
cout << endl;​
}​

int main() {​
int arr[] = {29, 10, 14, 37, 13};​
int n = sizeof(arr)/sizeof(arr[0]);​

cout << "Original array: ";​
printArray(arr, n);​

insertionSort(arr, n);​

cout << "Sorted array: ";​
printArray(arr, n);​

return 0;​
}

OUTPUT :
Original array: 29 10 14 37 13 ​
Sorted array: 10 13 14 29 37

10. Write a Program to implement Selection Sort in C++


#include <iostream>​
using namespace std;​

void selectionSort(int arr[], int n) {​
for (int i = 0; i < n - 1; i++) {​
int minIndex = i;​

// Find the minimum element in the remaining unsorted array​
for (int j = i + 1; j < n; j++) {​
if (arr[j] < arr[minIndex])​
minIndex = j;​
}​

// Swap the found minimum with the first element of unsorted
part​
if (minIndex != i)​
swap(arr[i], arr[minIndex]);​
}​
}​

void printArray(int arr[], int n) {​
for (int i = 0; i < n; i++)​
cout << arr[i] << " ";​
cout << endl;​
}​

int main() {​
int arr[] = {64, 25, 12, 22, 11};​
int n = sizeof(arr) / sizeof(arr[0]);​

cout << "Original array: ";​
printArray(arr, n);​

selectionSort(arr, n);​

cout << "Sorted array: ";​
printArray(arr, n);​

return 0;​
}​

OUTPUT :
Original array: 64 25 12 22 11 ​
Sorted array: 11 12 22 25 64​

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy