Dsa Lab Final Prep
Dsa Lab Final Prep
BUBBLE SORT
void sort(int arr[], int size)
{
}
int main()
{
char a[5][5] = { 'o','f','o','o','t','v','o','q','u','o','e','o','i','h','o','r','t','g','h','f' };
int r=s(0, 0, a);
cout << "\n" << r << " ";
}
SINGLY LINKEDLIST SIMPLE FUNCTIONS
#include<iostream>
using namespace std;
class Node
{
public:
int data;
int key;
Node* next;
Node()
{
data = 0;
key = 0;
next = NULL;
}
Node(int data)
{
this->data = data;
key = 0;
next = NULL;
}
Node(int data, int key)
{
this->data = data;
this->key = key;
next = NULL;
}
};
class ll
{
public:
Node* head;
Node* temp;
Node* tail;
ll()
{
head = NULL;
temp = NULL;
tail = NULL;
}
void append(int data)
{
Node* newn = new Node(data);
if (head == NULL)
{
head = newn;
temp = newn;
tail = newn;
}
else
{
temp->next = newn;
temp = temp->next;
}
}
void prepend(int data)
{
Node* newn = new Node(data);
if (head == NULL)
{
head = newn;
temp = newn;
tail = newn;
}
else
{
newn->next = head;
head = newn;
}
}
void insert(int data, int key)
{
int count = 0;
Node* newn = new Node(data, key);
if (key < 0)
{
cout << "\nKey is not correct\n";
}
else if (head == NULL && key > 0)
{
cout << "\n Can be inserted at head but not on your designated position";
}
else if (head == NULL)
{
head = newn;
temp = newn;
}
else if (key == 0)
{
newn->next=head;
head = newn;
}
else
{
temp = head;
while (count < key)
{
tail = temp;
temp = temp->next;
count++;
}
tail->next = newn;
newn->next = temp;
}
}
void Delete(int key)
{
int count = 0;
if (key < 0)
{
cout << "\nKey is not correct\n";
}
else if (head == NULL && key > 0)
{
cout << "\nlist is empty\n";
}
else if (head == NULL)
{
cout << "\nhead is empty\n";
}
else if (key == 0)
{
temp = head->next;
delete head;
head = temp;
}
else
{
temp = head;
while (count < key)
{
tail = temp;
temp = temp->next;
count++;
}
tail->next = temp->next;
delete temp;
}
}
void print()
{
temp = head;
while (temp != NULL)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL\n";
}
};
int main()
{
ll ob;
ob.append(2);
ob.append(1);
ob.append(7);
cout << "Append: ";
ob.print();
ob.prepend(8);
ob.prepend(9);
cout << "prepend: ";
ob.print();
ob.insert(5, 2);
ob.insert(8, 4);
cout << "Insert: ";
ob.print();
ob.Delete(2);
cout << "Delete: ";
ob.print();
}
QUICK SORT
#include<iostream>
using namespace std;
int partition(int arr[], int low, int high);
void sort(int arr[], int low, int high)
{
if (high <= low)
{
return;
}
else {
int z = partition(arr, low, high);
sort(arr, low, z - 1);
sort(arr, z + 1,high);
}
}
int partition(int arr[], int low,int high)
{
int x = low;
int j = high + 1;
int a = arr[x];
while (true)
{
while (arr[++x] < a)
{
if (x == high)
{
break;
}
}
while (a < arr[--j])
{
if (j == low)
{
break;
}
}
if (x >= j)
{
break;
}
swap(arr[x], arr[j]);
}
swap(arr[low], arr[j]);
return j;
}
void printing(int arr[], int low, int high)
{
for (int i = 0; i <= high; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int arr[] = { 35,33,42,10,14,19,27,44,26,31};
cout << "UNSORTED ARRAY: ";
int low = 0;
int high = sizeof(arr) / sizeof(int) - 1;
for (int i = 0; i <= high; i++)
{
cout << arr[i] << " ";
}
sort(arr, low, high);
cout << "\nSORTED ARRAY: ";
for (int i = 0; i <= high; i++)
{
cout << arr[i] << " ";
}
}
MERGE SORT
#include<iostream>
using namespace std;
void merge(int arr[], int low, int mid, int high) {
int n1 = mid - low + 1;
int n2 = high - mid;
int leftArray['n1'];
int rightArray['n2'];
int i = 0, j = 0, k = low;
}
int main()
{
int arr[] = { 3, 1, 6, 8, 4, 5, 7, 2 };
int high = sizeof(arr) / sizeof(int) -1;
int low = 0;
cout << "Unsorted array: ";
for (int i = 0; i < high; i++)
{
cout << arr[i] << " ";
}
mergesort(arr, low, high);
cout << "\nsorted array: ";
for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
{
cout << arr[i] << " ";
}
}
RADIX SORT
#include<iostream>
using namespace std;
void radixsort(int arr[], int n,int max,int temp[])
{
int Newarr['n'] = { 0 };
for (int i = 0; i < n; i++)
{
Newarr[i] = arr[i];
}
while (max > 0)
{
for (int j = 0; j < n; j++)
{
temp[j] = Newarr[j] % 10;
Newarr[j] = Newarr[j] / 10;
}
for (int i = 0; i < n - 1; i++)
{
for (int j = i; j < n; j++)
{
if (temp[i] > temp[j])
{
swap(temp[i], temp[j]);
swap(Newarr[i], Newarr[j]);
swap(arr[i], arr[j]);
}
}
}
}
BINARY SEARCH
#include<iostream>
using namespace std;
void binarysearch(int arr[], int low, int high)
{
INTERPOLATION SEARCH
#include<iostream>
using namespace std;
void interpolationsearch(int arr[], int low, int high, int target)
{
int mid = low + ((high - low) / (arr[high] - arr[low])) * (target -
arr[low]);
if (arr[mid] == target)
{
cout << "\n" << target << " FOUND AT " << mid << "
position\n";
return;
}
else if (arr[mid] < target)
{
interpolationsearch(arr, mid + 1, high, target);
}
else if (arr[mid] > target)
{
interpolationsearch(arr, low, mid - 1, target);
}
else
{
cout << target << " NOT FOUND.";
}
}
int main()
{
int arr[] = { 1,6,8,10,13,18,58,92,100};
int i = 0;
int low = 0;
int target = 0;
int high = sizeof(arr) / sizeof(int)-1;
cout << "enter the target: ";
cin >> target;
interpolationsearch(arr, low, high,target);
}
class AVL
{
public:
newn->left = node;
node->right = new2;
newn->right = node;
node->left = new2;
return node;
}
void preorder(Node* root)
{
if (root != NULL)
{
cout << root->key << "->";
preorder(root->left);
preorder(root->right);
}
}
};
int main()
{
Node* root = NULL;
AVL avl;
root = avl.insert(root, 1);
root = avl.insert(root, 2);
root = avl.insert(root, 3);
root = avl.insert(root, 4);
root = avl.insert(root, 5);
root = avl.insert(root, 6);
root = avl.insert(root, 7);
cout << "AVL TREE: ";
avl.preorder(root);
}
class AVL
{
public:
newn->left = node;
node->right = new2;
newn->right = node;
node->left = new2;
return node;
}
Node* minValueNode(Node* node)
{
Node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
else
{
if ((root->left == NULL) || (root->right == NULL))
{
Node* temp;
if (root->left != NULL)
{
temp = root->left;
}
else
{
temp = root->right;
}
if (temp == NULL)
{
temp = root;
root = NULL;
}
else
*root = *temp;
delete temp;
}
else
{
Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == NULL)
return root;
return root;
}
void preorder(Node* root)
{
if (root != NULL)
{
cout << root->key << "->";
preorder(root->left);
preorder(root->right);
}
}
};
int main()
{
Node* root = NULL;
AVL avl;
root = avl.insert(root, 1);
root = avl.insert(root, 2);
root = avl.insert(root, 3);
root = avl.insert(root, 4);
root = avl.insert(root, 5);
root = avl.insert(root, 6);
root = avl.insert(root, 7);
cout << "AVL TREE: ";
avl.preorder(root);
avl.deleteNode(root, 3);
cout << "\nAVL TREE AFTER DELETING 3: ";
avl.preorder(root);
MAX HEAP
#include <iostream>
using namespace std;
class MaxHeap {
private:
int* array;
int capacity;
int size;
void heapify(int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (largest != i) {
swap(array[i], array[largest]);
heapify(largest);
}
}
public:
MaxHeap(int cap) : capacity(cap), size(0) {
array = new int[cap];
}
int i = size;
size = size + 1;
array[i] = value;
int extractMax() {
if (size == 0) {
cout << "Heap is empty. Cannot extract maximum element." <<
endl;
return -1;
}
int deleteMax() {
return extractMax();
}
void printHeap() {
cout << "Heap elements: ";
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
~MaxHeap() {
delete[] array;
}
};
int main() {
MaxHeap heap(10);
heap.insert(35);
heap.insert(33);
heap.insert(42);
heap.insert(10);
heap.insert(14);
heap.insert(19);
heap.insert(27);
heap.insert(44);
heap.insert(26);
heap.insert(31);
heap.printHeap();
return 0;
}
MIN HEAP
#include <iostream>
using namespace std;
class MaxHeap {
private:
int* array;
int capacity;
int size;
void heapify(int i) {
int lowest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (lowest != i) {
swap(array[i], array[lowest]);
heapify(lowest);
}
}
public:
MaxHeap(int cap) : capacity(cap), size(0) {
array = new int[cap];
}
void printHeap() {
cout << "Heap elements: ";
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
~MaxHeap() {
delete[] array;
}
};
int main() {
MaxHeap heap(10);
heap.insert(35);
heap.insert(33);
heap.insert(42);
heap.insert(10);
heap.insert(14);
heap.insert(19);
heap.insert(27);
heap.insert(44);
heap.insert(26);
heap.insert(31);
heap.printHeap();
return 0;
}
ADJACENCY LIST
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class Graph {
private:
int V;
Node** adj;
public:
Graph(int vertices) {
V = vertices;
adj = new Node * [V];
for (int i = 0; i < V; i++) {
adj[i] = nullptr;
}
}
void printAdjacencyList() {
for (int i = 0; i < V; ++i) {
cout << "Adjacency list of vertex " << i << ": ";
Node* temp = adj[i];
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
}
};
int main() {
Graph g(4);
g.addEdge(0, 2);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.printAdjacencyList();
return 0;
}
class queue
{
public:
int front;
int rear;
int arr[20];
queue()
{
front = 0;
rear = 0;
for (int i = 0; i < 20; i++)
{
arr[i] = 0;
}
}
bool isempty()
{
if (rear == 0 || front == rear)
{
return true;
}
return false;
}
bool isfull()
{
if (rear == 20)
{
return true;
}
return false;
}
void enqueue(int val)
{
if (isempty())
{
arr[front] = val;
rear++;
}
else if (isfull()) {
cout << "\nQUEUE IS FULL\n";
}
else
{
arr[rear] = val;
rear++;
}
}
int dequeue()
{
int a=0;
if (isempty())
{
cout << "\nUEUE IS EMPTY\n";
}
else
{
a = arr[front];
front++;
}
return a;
}
};
class Graph
{
public:
int v;
int adj['v']['v'];
Graph(int v)
{
this->v = v;
for (int i = 0; i < v; i++)
{
for (int j = 0; j < v; j++)
{
adj[i][j] = 0;
}
}
}
void addedge(int source, int dest)
{
adj[source][dest] = 1;
adj[dest][source] = 1;
}
void printadj()
{
for (int i = 0; i < v; i++)
{
for (int j = 0; j < v; j++)
{
cout << adj[i][j] << " ";
}
cout << endl;
}
}
void bfs(int v,int n)
{
bool visited[7] = { false };
cout << n << " ";
visited[n] = true;
queue obj;
obj.enqueue(n);
while (!obj.isempty())
{
int x = obj.dequeue();
for (int i = 0; i < v; i++)
{
if (adj[x][i] == 1 && visited[i] == false)
{
cout << i << " ";
visited[i] = true;
obj.enqueue(i);
}
}
}
};
int main()
{
Graph ob(7);
int node=0;
ob.addedge(0, 1);
ob.addedge(0, 2);
ob.addedge(0, 3);
ob.addedge(1, 3);
ob.addedge(2, 3);
ob.addedge(2, 4);
ob.addedge(3, 4);
ob.addedge(4, 5);
ob.addedge(4, 6);
ob.bfs(7, 0);
}
}
};
DIJKSTRA’s ALGORITHM
#include <iostream>;
using namespace std;
#include <limits.h>
#define V 9
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
dijkstra(graph, 0);
return 0;
}
PRIMS ALGORITHM
#include <iostream>
#include <climits>
#define V 5
class Graph {
int graph[V][V];
public:
Graph(int g[][V]) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
graph[i][j] = g[i][j];
}
}
}
return min_index;
}
void primMST() {
int parent[V];
int key[V];
bool mstSet[V];
for (int i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
printMST(parent);
}
int main() {
/* Let us create the following graph
2 3
(0)---(1)---(2)
| / \ |
6| 8/ \5 |7
|/ \ |
(3)---------(4)
9 */
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
Graph g(graph);
g.primMST();
return 0;
}