0% found this document useful (0 votes)
13 views22 pages

HMR Institute of Technology and Management: Experiment 1

Uploaded by

Saurav Singhal
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)
13 views22 pages

HMR Institute of Technology and Management: Experiment 1

Uploaded by

Saurav Singhal
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/ 22

HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Hamidpur , Delhi – 110036


( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 1
Aim
To implement linear and binary search and analysis its time complexity.

Linear Search
#include <iostream>
using namespace std;

int search(int arr[], int n, int x)


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

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, x);
(result == -1)? cout<<"Element is not present in array"
: cout<<"Element is present at index " <<result;
return 0;
}

Binary Search
#include <iostream>
using namespace std;

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


while (l <= r) {
int m = l + (r - l) / 2;

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

// Check if x is present at mid


if (arr[m] == x)
return m;

// If x greater, ignore left half


if (arr[m] < x)
l = m + 1;

// If x is smaller, ignore right half


else
r = m - 1;
}
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);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

Output:

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 2A
Aim
To implement merge sort algorithms using arrays as a data structure.

Merge Sort

#include <iostream>
void merge(int arr[], int left, int middle, int right) {
int n1 = middle - left + 1;
int n2 = right - middle;
int leftArr[n1];
int rightArr[n2];
for (int i = 0; i < n1; i++) {
leftArr[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
rightArr[j] = arr[middle + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int middle = left + (right - left) / 2;

mergeSort(arr, left, middle);


mergeSort(arr, middle + 1, right);
merge(arr, left, middle, right);
}
}
int main() {

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

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


int n = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
mergeSort(arr, 0, n - 1);
std::cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}

Output:

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 2B
Aim
To implement quick sort algorithms using arrays as a data structure.

Quick Sort
#include <iostream>
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++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return i + 1; // Return the index of the pivot element
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
quickSort(arr, 0, n - 1);
std::cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

Output:

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 2C
Aim
To implement bubble sort algorithms using arrays as a data structure.

Bubble Sort

#include <iostream>
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++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

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


std::cout << "Original array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
bubbleSort(arr, n);
std::cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}

Output:

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 2D
Aim
To implement selection sort algorithms using arrays as a data structure.

Selection Sort

#include <iostream>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
std::swap(arr[i], arr[minIndex]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

}
std::cout << std::endl;
selectionSort(arr, n);
std::cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}

Output:

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 2E
Aim
To implement heap sort algorithms using arrays as a data structure.

Heap Sort

#include <iostream>
void maxHeapify(int arr[], int n, int i) {
int largest = i; // Initialize the largest as the root
int left = 2 * i + 1; // Left child
int right = 2 * i + 2; // Right child
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
std::swap(arr[i], arr[largest]);
maxHeapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

maxHeapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
std::swap(arr[0], arr[i]);
maxHeapify(arr, i, 0);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
heapSort(arr, n);
std::cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

Output:

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 3
Aim
To implement matrix multiplication and analyse its time complexity.

Sparce Matrix Multiplication


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

vector<vector <int>> sparceMultiplication(int a[2][2],int b[2][2]){


int p=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
int q=(b[0][0])*(a[1][0]+a[1][1]);
int r=(a[0][0])*(b[0][1]-b[1][1]);
int s=(a[1][1])*(b[1][0]-b[0][0]);
int t=(b[1][1])*(a[0][0]+a[0][1]);
int u=(b[0][0]+b[0][1])*(a[1][0]-a[0][0]);
int v=(b[1][0]+b[1][1])*(a[0][1]-a[1][1]);
vector<vector<int>> ans;
ans.push_back({(p+s-t+v),(r+t)});
ans.push_back({(q+s),(p+r-q+u)});
return ans;
}

int main(){
cout<<"!!! Sparce Matrix Multiplication !!!"<<endl;
vector<vector<int>> ans;
int a[2][2]={{1,3},{7,5}};
int b[2][2]={{6,7},{3,8}};
ans=sparceMultiplication(a,b);
for(auto i:ans){
for(auto j:i){
cout<<j<<" ";
}
cout<<endl;
}
return 0;

}
AASHISH THAKUR 0853302721
HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

Output:

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

EXPERIMENT 4
Aim
To implement Huffman coding and analyse its time complexity.

Huffman coding
#include <iostream>
#include <queue>
#include <unordered_map>
using namespace std;

struct Node {
char data;
int frequency;
Node* left;
Node* right;
};

Node* createNode(char data, int frequency) {


Node* newNode = new Node();
newNode->data = data;
newNode->frequency = frequency;
newNode->left = newNode->right = nullptr;
return newNode;
}
struct Compare {
bool operator()(Node* left, Node* right) {
return left->frequency > right->frequency;
}
};
Node* buildHuffmanTree(string text) {
unordered_map<char, int> freq;
for (char ch : text) {
freq[ch]++;
}
priority_queue<Node*, vector<Node*>, Compare> pq;
for (auto pair : freq) {
pq.push(createNode(pair.first, pair.second));
}
AASHISH THAKUR 0853302721
HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

while (pq.size() != 1) {
Node* left = pq.top();
pq.pop();
Node* right = pq.top();
pq.pop();
Node* internalNode = createNode('\0', left->frequency + right->frequency);
internalNode->left = left;
internalNode->right = right;
pq.push(internalNode);
}

return pq.top();
}

void encode(Node* root, string str, unordered_map<char, string>& huffmanCodes) {


if (root == nullptr) {
return;
}

if (root->data != '\0') {
huffmanCodes[root->data] = str;
}
encode(root->left, str + "0", huffmanCodes);
encode(root->right, str + "1", huffmanCodes);
}
void decode(Node* root, int& index, string encodedText) {
if (root == nullptr) {
return;
}
if (root->data != '\0') {
cout << root->data;
return;
}

index++;
if (encodedText[index] == '0') {
decode(root->left, index, encodedText);
} else {
decode(root->right, index, encodedText);
}
}
int main() {
AASHISH THAKUR 0853302721
HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

string text = "this is an example for huffman encoding";


Node* root = buildHuffmanTree(text);
unordered_map<char, string> huffmanCodes;
encode(root, "", huffmanCodes);
cout << "Huffman Codes:\n";
for (auto pair : huffmanCodes) {
cout << pair.first << ": " << pair.second << endl;
}
string encodedText = "";
for (char ch : text) {
encodedText += huffmanCodes[ch];
}
cout << "Encoded Text: " << encodedText << endl;
cout << "Decoded Text: ";
int index = -1;
while (index < static_cast<int>(encodedText.size()) - 2) {
decode(root, index, encodedText);
}
cout << endl;
return 0;
}

AASHISH THAKUR 0853302721


HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Hamidpur , Delhi – 110036
( An ISO 9001:2008 certified , AICTE approved and GGSIP University affiliated
institute )
Email: [email protected], Phone: 8130643674,8130643690,8287461931

Output:

AASHISH THAKUR 0853302721

You might also like