Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sarthak To Marda A File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Harcourt Butler Technical

University
Nawabganj , Kanpur

Lab File

Design & Analysis of Algorithm


(ECS-355)
Session : 2022 – 2023

Submitted To : Submitted By :
Dr. Imran Khan Sarthak Tomar
Assistant Professor 200108053
CSE Department Information Technology (III)
INDEX
Serial
No.
Content Page No.

1 Merge Sort Implementation using C++ 1


2 Quick Sort Implementation using C++ 3
3 0/1 Knapsack Problem Implementation using C++ 5
4 Travelling Salesman Problem implementation using C++ 6
5 N – Queen problem implementation using C++ 8
6 Insertion Sort implementation using C++ 10
7 Heap Sort implementation using C++ 11
8 Bubble Sort implementation using C++ 13
9 Binary Search Implementation using C++ 14
10 Implement kth smallest element problem using C++ 15
1: Merge Sort Implementation using C++.

void merge(int arr[], int p, int q, int r) {

int n1 = q - p + 1;

int n2 = r - q;

int L[n1], M[n2];

for (int i = 0; i < n1; i++){

L[i] = arr[p + i];

for (int j = 0; j < n2; j++){

M[j] = arr[q + 1 + j];

// Maintain current index of sub-arrays and main array

int i, j, k;

i = 0;

j = 0;

k = p;

while (i < n1 && j < n2) {

if (L[i] <= M[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = M[j];

j++;

k++;

// When we run out of elements in either L or M,


// pick up the remaining elements and put in A[p..r]

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = M[j];

j++;

k++;

void mergeSort(int arr[],int low,int high){

if(low < high){

int mid = (low + high)/2;

mergeSort(arr,low,mid);

mergeSort(arr,mid+1,high);

merge(arr,low,mid,high);

void printArray(int A[], int size)

for (auto i = 0; i < size; i++)

cout << A[i] << " ";

int main()

int arr[] = { 12, 11, 13, 5, 6, 7 };


int arr_size = sizeof(arr) / sizeof(arr[0]);

cout << "Given array is \n";

printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

cout << "\nSorted array is \n";

printArray(arr, arr_size);

return 0;

2: Quick Sort Implementation using C++ .

#include <bits/stdc++.h>

using namespace std;

void swap(int *a, int *b)

int t = *a;

*a = *b;

*b = t;

int partition(int arr[], int low, int high)

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high - 1; j++)

{
if (arr[j] < pivot)

i++; // increment index of smaller element

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)

int i;

for (i = 0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

int main()

int arr[] = {10, 7, 8, 9, 1, 5};


int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

cout << "Sorted array: \n";

printArray(arr, n);

return 0;

3: 0/1 Knapsack Problem Implementation using C++.

#include <bits/stdc++.h>

using namespace std;

int knapSack(int W, int wt[], int val[], int n)

int i, w;

vector<vector<int> > K(n + 1, vector<int>(W + 1));

for (i = 0; i <= n; i++) {

for (w = 0; w <= W; w++) {

if (i == 0 || w == 0)

K[i][w] = 0;

else if (wt[i - 1] <= w)

K[i][w] = max(val[i - 1]+ K[i - 1][w - wt[i - 1]], K[i - 1][w]);

else

K[i][w] = K[i - 1][w];

return K[n][W];
}

int main()

int val[] = { 60, 100, 120 };

int wt[] = { 10, 20, 30 };

int W = 50;

int n = sizeof(val) / sizeof(val[0]);

cout << knapSack(W, wt, val, n)<<endl;

return 0;

4: Travelling Salesman Problem implementation using C++.

#include<bits/stdc++.h>

using namespace std;

int tsp(const vector<vector<int>>& cities, int pos, int visited, vector<vector<int>>& state)

if(visited == ((1 << cities.size()) - 1))

return cities[pos][0]; // return to starting city

if(state[pos][visited] != INT_MAX)

return state[pos][visited];

for(int i = 0; i < cities.size(); ++i)

// can't visit ourselves unless we're ending & skip if already visited

if(i == pos || (visited & (1 << i)))

continue;
int distance = cities[pos][i] + tsp(cities, i, visited | (1 << i), state);

if(distance < state[pos][visited])

state[pos][visited] = distance;

return state[pos][visited];

int main()

vector<vector<int>> cities = { { 0, 20, 42, 35 },

{ 20, 0, 30, 34 },

{ 42, 30, 0, 12 },

{ 35, 34, 12, 0 }

};

vector<vector<int>> state(cities.size());

for(auto& neighbors : state)

neighbors = vector<int>((1 << cities.size()) - 1, INT_MAX);

cout << "minimum: " << tsp(cities, 0, 1, state) << endl;

return 0;

}
5: N – Queen problem implementation using C++.

#include <bits/stdc++.h>

#define N 4

using namespace std;

void printSolution(int board[N][N])

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++)

if(board[i][j])

cout << "Q ";

else cout<<". ";

printf("\n");

bool isSafe(int board[N][N], int row, int col)

int i, j;

/* Check this row on left side */

for (i = 0; i < col; i++)

if (board[row][i])

return false;

/* Check upper diagonal on left side */

for (i = row, j = col; i >= 0 && j >= 0; i--, j--)

if (board[i][j])

return false;

/* Check lower diagonal on left side */


for (i = row, j = col; j >= 0 && i < N; i++, j--)

if (board[i][j]) return false;

return true;

bool nQueens(int board[N][N], int col)

if (col >= N)

return true;

for (int i = 0; i < N; i++) {

if (isSafe(board, i, col)) {

board[i][col] = 1;

if (nQueens(board, col + 1))

return true;

board[i][col] = 0;

return false;

bool solveNQ()

int board[N][N] = { { 0, 0, 0, 0 },{ 0, 0, 0, 0 },{ 0, 0, 0, 0 }, { 0, 0, 0, 0 } };

if (nQueens(board, 0) == false) {

cout << "Solution does not exist";

return false;

printSolution(board);

return true;

}
int main()

solveNQ();

return 0;

6: Insertion Sort implementation using C++.

#include <bits/stdc++.h>

using namespace std;

void insertionSort(int arr[], int n)

int i, key, j;

for (i = 1; i < n; i++)

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key)

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

}
void printArray(int arr[], int n)

int i;

for (i = 0; i < n; i++)

cout << arr[i] << " ";

cout << endl;

int main()

int arr[] = { 12, 11, 13, 5, 6 };

int N = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, N);

printArray(arr, N);

return 0;

7: Heap Sort implementation using C++.

#include <iostream>

using namespace std;

void heapify(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(int arr[], int N)

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(int arr[], int N)

for (int i = 0; i < N; ++i)

cout << arr[i] << " ";

cout << "\n";

int main()

int arr[] = { 12, 11, 13, 5, 6, 7 };

int N = sizeof(arr) / sizeof(arr[0]);


heapSort(arr, N);

cout << "Sorted array is \n";

printArray(arr, N);

8: Bubble Sort implementation using C++.


#include <bits/stdc++.h>
using namespace std;

void bubbleSort(int arr[], int n)


{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = { 5, 1, 4, 2, 8};
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}
9: Binary Search Implementation using C++.
#include <bits/stdc++.h>
using namespace std;

int binarySearch(int arr[], int l, int r, int x)


{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;

if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);


}
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
if(result == -1)
cout << "Element is not present in array"<<endl;
else
cout << "Element is present at index " << result <<endl;
return 0;
}
10: Implement kth smallest element problem using C++.

#include <bits/stdc++.h>
using namespace std;

int kthSmallest(int arr[], int N, int K)


{
priority_queue<int> pq;

for (int i = 0; i < K; i++) {


pq.push(arr[i]);
}
for (int i = K; i < N; i++) {
if (arr[i] < pq.top()) {
pq.pop();
pq.push(arr[i]);
}
}
return pq.top();
}

int main()
{
int N = 10;
int arr[N] = { 10, 5, 4, 3, 48, 6, 2, 33, 53, 10 };
int K = 4;

cout << "Kth Smallest Element is: "<< kthSmallest(arr, N, K);


}

You might also like