0% found this document useful (0 votes)
1 views58 pages

DSA

data structure

Uploaded by

Akansha S
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)
1 views58 pages

DSA

data structure

Uploaded by

Akansha S
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/ 58

[04/07, 23:38] Ashutosh K: 1. To insert and delete elements from appropriate position in an array.

Ans –

#include <iostream>

#define MAX_SIZE 100

Void insertElement(int arr[], int& size, int element, int position) {

If (position < 0 || position > size) {

Std::cout << “Invalid position!” << std::endl;

Return;

If (size >= MAX_SIZE) {

Std::cout << “Array is full, cannot insert element!” << std::endl;

Return;

For (int i = size; i > position; i--) {

Arr[i] = arr[i – 1];

Arr[position] = element;

Size++;

Std::cout << “Element inserted successfully!” << std::endl;

Void deleteElement(int arr[], int& size, int position) {

If (position < 0 || position >= size) {

Std::cout << “Invalid position!” << std::endl;

Return;

}
For (int i = position; i < size – 1; i++) {

Arr[i] = arr[i + 1];

Size--;

Std::cout << “Element deleted successfully!” << std::endl;

Int main() {

Int arr[MAX_SIZE];

Int size = 0;

// Inserting elements

insertElement(arr, size, 10, 0);

insertElement(arr, size, 20, 1);

insertElement(arr, size, 30, 2);

// Deleting an element

deleteElement(arr, size, 1);

// Displaying the array

Std::cout << “Array elements: “;

For (int i = 0; i < size; i++) {

Std::cout << arr[i] << “ “;

Std::cout << std::endl;

Return 0;

}
[04/07, 23:39] Ashutosh K: 2. To search an element and print the total time of occurrence in the
array.

Ans –

#include <iostream>

#define MAX_SIZE 100

Int searchElement(int arr[], int size, int element) {

Int count = 0;

For (int i = 0; i < size; i++) {

If (arr[i] == element) {

Count++;

Return count;

Int main() {

Int arr[MAX_SIZE] = { 10, 20, 30, 20, 40, 20, 50 };

Int size = 7;

Int element;

Std::cout << “Enter the element to search: “;

Std::cin >> element;

Int count = searchElement(arr, size, element);

If (count > 0) {

Std::cout << “Element found “ << count << “ time(s) in the array.” << std::endl;

} else {
Std::cout << “Element not found in the array.” << std::endl;

Return 0;

[04/07, 23:40] Ashutosh K: 3. To delete all occurrence of an element in an array.

Ans –

#include <iostream>

#define MAX_SIZE 100

Void deleteOccurrences(int arr[], int& size, int element) {

Int newSize = 0;

For (int i = 0; i < size; i++) {

If (arr[i] != element) {

Arr[newSize] = arr[i];

newSize++;

Size = newSize;

Std::cout << “Occurrences of element deleted successfully!” << std::endl;

Int main() {

Int arr[MAX_SIZE

// ...

Int size = 7;

Int element = 20;


// Displaying the original array

Std::cout << “Original array: “;

For (int i = 0; i < size; i++) {

Std::cout << arr[i] << “ “;

Std::cout << std::endl;

// Deleting occurrences of element

deleteOccurrences(arr, size, element);

// Displaying the modified array

Std::cout << “Modified array: “;

For (int i = 0; i < size; i++) {

Std::cout << arr[i] << “ “;

Std::cout << std::endl;

Return 0;

[04/07, 23:41] Ashutosh K: 4. Array implementation of Stack.

Ans –

#include <iostream>

#define MAX_SIZE 100

Class Stack {

Private:

Int top;

Int arr[MAX_SIZE];

Public:
Stack() {

Top = -1;

Void push(int element) {

If (top == MAX_SIZE – 1) {

Std::cout << “Stack Overflow! Cannot push element.” << std::endl;

Return;

Arr[++top] = element;

Std::cout << “Element pushed successfully!” << std::endl;

Void pop() {

If (top == -1) {

Std::cout << “Stack Underflow! Cannot pop element.” << std::endl;

Return;

Top--;

Std::cout << “Element popped successfully!” << std::endl;

Int peek() {

If (top == -1) {

Std::cout << “Stack is empty! No element to peek.” << std::endl;

Return -1;

Return arr[top];
}

Bool isEmpty() {

Return (top == -1);

Bool isFull() {

Return (top == MAX_SIZE – 1);

};

Int main() {

Stack stack;

// Pushing elements

Stack.push(10);

Stack.push(20);

Stack.push(30);

// Popping elements

Stack.pop();

// Peeking element

Int topElement = stack.peek();

If (topElement != -1) {

Std::cout << “Top element: “ << topElement << std::endl;

Return 0;

[04/07, 23:42] Ashutosh K: 5. Array implementation of Linear Queue.


Ans –

#include <iostream>

#define MAX_SIZE 100

Class Queue {

Private:

Int front, rear;

Int arr[MAX_SIZE];

Public:

Queue() {

Front = -1;

Rear = -1;

Void enqueue(int element) {

If (rear == MAX_SIZE – 1) {

Std::cout << “Queue Overflow! Cannot enqueue element.” << std::endl;

Return;

Arr[++rear] = element;

If (front == -1) {

Front = 0;

Std::cout << “Element enqueued successfully!” << std::endl;

Void dequeue() {
If (front == -1 || front > rear) {

Std::cout << “Queue Underflow! Cannot dequeue element.” << std::endl;

Return;

Front++;

If (front > rear) {

Front = -1;

Rear = -1;

Std::cout << “Element dequeued successfully!” << std::endl;

Int peek() {

If (front == -1 || front > rear) {

Std::cout << “Queue is empty! No element to peek.” << std::endl;

Return -1;

Return arr[front];

Bool isEmpty() {

Return (front == -1 || front > rear);

Bool isFull() {

Return (rear == MAX_SIZE – 1);

}
};

Int main() {

Queue queue;

// Enqueuing elements

Queue.enqueue(10);

Queue.enqueue(20);

Queue.enqueue(30);

// Dequeuing elements

Queue.dequeue();

// Peeking element

Int frontElement = queue.peek();

If (frontElement != -1) {

Std::cout << “Front element: “ << frontElement << std::endl;

Return 0;

[04/07, 23:44] Ashutosh K: 6. Array implementation of Circular Queue.

Ans –

#include <iostream>

#define MAX_SIZE 100

Class CircularQueue {

Private:

Int front, rear;

Int arr[MAX_SIZE];
Public:

CircularQueue() {

Front = -1;

Rear = -1;

Void enqueue(int element) {

If ((front == 0 && rear == MAX_SIZE – 1) || (rear + 1 == front)) {

Std::cout << “Circular Queue Overflow! Cannot enqueue element.” << std::endl;

Return;

If (front == -1) {

Front = 0;

Rear = 0;

} else if (rear == MAX_SIZE – 1) {

Rear = 0;

} else {

Rear++;

Arr[rear] = element;

Std::cout << “Element enqueued successfully!” << std::endl;

Void dequeue() {

If (front == -1) {

Std::cout << “Circular Queue Underflow! Cannot dequeue element.” << std::endl;

Return;

}
If (front == rear) {

Front = -1;

Rear = -1;

} else if (front == MAX_SIZE – 1) {

Front = 0;

} else {

Front++;

Std::cout << “Element dequeued successfully!” << std::endl;

Int peek() {

If (front == -1) {

Std::cout << “Circular Queue is empty! No element to peek.” << std::endl;

Return -1;

Return

// ...

Int peekElement = queue.peek();

If (peekElement != -1) {

Std::cout << “Front element: “ << peekElement << std::endl;

Return 0;

[04/07, 23:45] Ashutosh K: 7. To implement linear linked list and perform different operation such as
node insert and delete, search of an item, reverse the list.

Ans –
#include <iostream>

Struct Node {

Int data;

Node* next;

};

Class LinkedList {

Private:

Node* head;

Public:

LinkedList() {

Head = nullptr;

Void insertNode(int data) {

Node* newNode = new Node;

newNode->data = data;

newNode->next = nullptr;

if (head == nullptr) {

head = newNode;

} else {

Node* temp = head;

While (temp->next != nullptr) {

Temp = temp->next;

Temp->next = newNode;

}
Std::cout << “Node inserted successfully!” << std::endl;

Void deleteNode(int data) {

If (head == nullptr) {

Std::cout << “List is empty! Cannot delete node.” << std::endl;

Return;

Node* temp = head;

Node* prev = nullptr;

If (temp != nullptr && temp->data == data) {

Head = temp->next;

Delete temp;

Std::cout << “Node deleted successfully!” << std::endl;

Return;

While (temp != nullptr && temp->data != data) {

Prev = temp;

Temp = temp->next;

If (temp == nullptr) {

Std::cout << “Node with data “ << data << “ not found! Cannot delete node.” << std::endl;

Return;

Prev->next = temp->next;

Delete temp;
Std::cout << “Node deleted successfully!” << std::endl;

Bool searchNode(int data) {

Node* temp = head;

While (temp != nullptr) {

If (temp->data == data) {

Return true;

Temp = temp->next;

Return false;

Void reverseList() {

Node* current = head;

Node* prev = nullptr;

Node* next = nullptr;

While (current != nullptr) {

Next = current->next;

Current->next = prev;

Prev = current;

Current = next;

Head = prev;

Std::cout << “List reversed successfully!” << std::endl;


}

Void displayList() {

If (head == nullptr) {

Std::cout << “List is empty!” << std::endl;

Return;

Node* temp = head;

Std::cout << “List: “;

While (temp != nullptr) {

Std::cout << temp->data << “ “;

Temp = temp->next;

Std::cout << std::endl;

};

Int main() {

LinkedList list;

// Inserting nodes

List.insertNode(10);

List.insertNode(20);

List.insertNode(30);

// Deleting a node

List.deleteNode(20);

// Searching a node

Bool found = list.searchNode(30);


If (found) {

Std::cout << “Node found in the list.” << std::endl;

} else {

Std::cout << “Node not found in the list.” << std::

Std::endl;

// Reversing the list

List.reverseList();

// Displaying the list

List.displayList();

Return 0;

[04/07, 23:46] Ashutosh K: 8. To implement circular linked list and perform different operation such
as node insert and delete.

Ans –

#include <iostream>

Struct Node {

Int data;

Node* next;

};

Class CircularLinkedList {

Private:

Node* head;

Public:

CircularLinkedList() {
Head = nullptr;

Void insertNode(int data) {

Node* newNode = new Node;

newNode->data = data;

newNode->next = nullptr;

if (head == nullptr) {

head = newNode;

head->next = head;

} else {

Node* temp = head;

While (temp->next != head) {

Temp = temp->next;

Temp->next = newNode;

newNode->next = head;

Std::cout << “Node inserted successfully!” << std::endl;

Void deleteNode(int data) {

If (head == nullptr) {

Std::cout << “List is empty! Cannot delete node.” << std::endl;

Return;

Node* temp = head;

Node* prev = nullptr;


If (temp != nullptr && temp->data == data) {

While (temp->next != head) {

Temp = temp->next;

Temp->next = head->next;

Delete head;

Head = temp->next;

Std::cout << “Node deleted successfully!” << std::endl;

Return;

While (temp->next != head && temp->data != data) {

Prev = temp;

Temp = temp->next;

If (temp == head) {

Std::cout << “Node with data “ << data << “ not found! Cannot delete node.” << std::endl;

Return;

Prev->next = temp->next;

Delete temp;

Std::cout << “Node deleted successfully!” << std::endl;

Void displayList() {

If (head == nullptr) {

Std::cout << “List is empty!” << std::endl;

Return;
}

Node* temp = head;

Std::cout << “List: “;

Do {

Std::cout << temp->data << “ “;

Temp = temp->next;

} while (temp != head);

Std::cout << std::endl;

};

Int main() {

CircularLinkedList list;

// Inserting nodes

List.insertNode(10);

List.insertNode(20);

List.insertNode(30);

// Deleting a node

List.deleteNode(20);

// Displaying the list

List.displayList();

Return 0;

[04/07, 23:47] Ashutosh K: 9. To implement double linked list and perform different operation such
as node insert and delete.

Ans –
#include <iostream>

Struct Node {

Int data;

Node* prev;

Node* next;

};

Class DoublyLinkedList {

Private:

Node* head;

Public:

DoublyLinkedList() {

Head = nullptr;

Void insertNode(int data) {

Node* newNode = new Node;

newNode->data = data;

newNode->prev = nullptr;

newNode->next = nullptr;

if (head == nullptr) {

head = newNode;

} else {

Node*

Node* temp = head;

While (temp->next != nullptr) {

Temp = temp->next;

}
Temp->next = newNode;

newNode->prev = temp;

Void deleteNode(int data) {

If (head == nullptr) {

Std::cout << “List is empty! Cannot delete node.” << std::endl;

Return;

Node* temp = head;

While (temp != nullptr && temp->data != data) {

Temp = temp->next;

If (temp == nullptr) {

Std::cout << “Node with data “ << data << “ not found! Cannot delete node.” << std::endl;

Return;

If (temp == head) {

Head = temp->next;

If (head != nullptr) {

Head->prev = nullptr;

} else {

Temp->prev->next = temp->next;

If (temp->next != nullptr) {

Temp->next->prev = temp->prev;

}
}

Delete temp;

Std::cout << “Node deleted successfully!” << std::endl;

Void displayList() {

If (head == nullptr) {

Std::cout << “List is empty!” << std::endl;

Return;

Node* temp = head;

Std::cout << “List: “;

While (temp != nullptr) {

Std::cout << temp->data << “ “;

Temp = temp->next;

Std::cout << std::endl;

};

Int main() {

DoublyLinkedList list;

// Inserting nodes

List.insertNode(10);

List.insertNode(20);

List.insertNode(30);

// Deleting a node
List.deleteNode(20);

// Displaying the list

List.displayList();

Return 0;

[04/07, 23:47] Ashutosh K: 10. Linked list implementation of Stack.

Ans –

#include <iostream>

Struct Node {

Int data;

Node* next;

};

Class Stack {

Private:

Node* top;

Public:

Stack() {

Top = nullptr;

Void push(int data) {

Node* newNode = new Node;

newNode->data = data;

newNode->next = top;

top = newNode;

std::cout << “Element pushed successfully!” << std::endl;


}

Void pop() {

If (top == nullptr) {

Std::cout << “Stack Underflow! Cannot pop element.” << std::endl;

Return;

Node* temp = top;

Top = top->next;

Delete temp;

Std::cout << “Element popped successfully!” << std::endl;

Int peek() {

If (top == nullptr) {

Std::cout << “Stack is empty! No element to peek.” << std::endl;

Return -1;

Return top->data;

Bool isEmpty() {

Return (top == nullptr);

};

Int main() {

Stack stack;
// Pushing elements

Stack.push(10);

Stack.push(20);

Stack.push(30);

// Popping an element

Stack.pop();

// Peeking element

Int topElement = stack.peek();

If (topElement != -1) {

Std::cout << “Top element: “ << topElement << std::endl;

Return 0;

[04/07, 23:49] Ashutosh K: 11. Linked list implementation of Queue.

Ans –

#include <iostream>

Struct Node {

Int data;

Node* next;

};

Class Queue {

Private:

Node* front;

Node* rear;

Public:
Queue() {

Front = nullptr;

Rear = nullptr;

Void enqueue(int data) {

Node* newNode = new Node;

newNode->data = data;

newNode->next = nullptr;

if (rear == nullptr) {

front = newNode;

rear = newNode;

} else {

Rear->next = newNode;

Rear = newNode;

Std::cout << “Element enqueued successfully!” << std::endl;

Void dequeue() {

If (front == nullptr) {

Std::cout << “Queue Underflow! Cannot dequeue element.” << std::endl;

Return;

Node* temp = front;

Front = front->next;

If (front == nullptr) {
Rear = nullptr;

Delete temp;

Std::cout << “Element dequeued successfully!” << std::endl;

Int peek() {

If (front == nullptr) {

Std::cout << “Queue is empty! No element to peek.” << std::endl;

Return -1;

Return front->data;

Bool isEmpty() {

Return (front == nullptr);

};

Int main() {

Queue queue;

// Enqueuing elements

Queue.enqueue(10);

Queue.enqueue(20);

Queue.enqueue(30);

// Dequeuing an element

Queue.dequeue();
// Peeking element

Int frontElement = queue.peek();

If (frontElement != -1) {

Std::cout << “Front element: “ << frontElement << std::endl;

Return 0;

[04/07, 23:53] Ashutosh K: 12. Polynomial representation using linked list.

Ans –

#include <iostream>

Struct Node {

Int coefficient;

Int exponent;

Node* next;

};

Class Polynomial {

Private:

Node* head;

Public:

Polynomial() {

Head = nullptr;

Void addTerm(int coefficient, int exponent) {

Node* newNode = new Node;

newNode->coefficient = coefficient;
newNode->exponent = exponent;

newNode->next = nullptr;

if (head == nullptr) {

head = newNode;

} else {

Node* temp = head;

While (temp->next != nullptr) {

Temp = temp->next;

Temp->next = newNode;

Std::cout << “Term added successfully!” << std::endl;

Void displayPolynomial() {

If (head == nullptr) {

Std::cout << “Polynomial is empty!” << std::endl;

Return;

Node* temp = head;

Std::cout << “Polynomial: “;

While (temp != nullptr) {

Std::cout << temp->coefficient << “x^” << temp->exponent << “ “;

Temp = temp->next;

Std::cout << std::endl;

};
Int main() {

Polynomial polynomial;

// Adding terms to the polynomial

Polynomial.addTerm(3, 2);

Polynomial.addTerm(-2, 1);

Polynomial.addTerm(5, 0);

// Displaying the polynomial

Polynomial.displayPolynomial();

Return 0;

[04/07, 23:55] Ashutosh K: 13. To implement a Binary Search Tree.

Ans –

#include <iostream>

Struct Node {

Int data;

Node* left;

Node* right;

};

Class BinarySearchTree {

Private:

Node* root;

Public:

BinarySearchTree() {

Root = nullptr;
}

Node* createNode(int data) {

Node* newNode = new Node;

newNode->data = data;

newNode->left = nullptr;

newNode->right = nullptr;

return newNode;

Node* insertNode(Node* node, int data) {

If (node == nullptr) {

Return createNode(data);

If (data < node->data) {

Node->left = insertNode(node->left, data);

} else if (data > node->data) {

Node->right = insertNode(node->right, data);

Return node;

Void insert(int data) {

Root = insertNode(root, data);

Std::cout << “Node inserted successfully!” << std::endl;

Node* searchNode(Node* node, int data) {

If (node == nullptr || node->data == data) {


Return node;

If (data < node->data) {

Return searchNode(node->left, data);

} else {

Return searchNode(node->right, data);

Void search(int data) {

Node* result = searchNode(root, data);

If (result != nullptr) {

Std::cout << “Node with data “ << data << “ found in the tree.” << std::endl;

} else {

Std::cout << “Node with data “ << data << “ not found in the tree.” << std::endl;

Void inorderTraversal(Node* node) {

If (node == nullptr) {

Return;

inorderTraversal(node->left);

std::cout << node->data << “ “;

inorderTraversal(node->right);

Void displayInorder() {

If (root == nullptr) {
Std::cout << “Tree is empty!” << std::endl;

Return;

Std::cout << “Inorder Traversal: “;

inorderTraversal(root);

std::cout << std::endl;

Node* findMinimumNode(Node* node) {

While (node->left != nullptr) {

Node = node->left;

Return node;

Node* deleteNode(Node* node, int data) {

If (node == nullptr) {

Return node;

If (data < node->data) {

Node->left = deleteNode(node->left, data);

} else if (data > node->data) {

Node->right = deleteNode(node->right, data);

} else {

If (node->left == nullptr) {

Node* temp = node->right;

Delete node;

Return temp;

} else if (node->right == nullptr) {


Node* temp = node->left;

Delete node;

Return temp;

Node* temp = findMinimumNode(node->right);

Node->data = temp->data;

Node->right = deleteNode(node->right, temp->data);

Return node;

Void remove(int data) {

Root = deleteNode(root, data);

Std::cout << “Node deleted successfully!” << std::endl;

};

Int main() {

BinarySearchTree bst;

// Inserting nodes

Bst.insert(50);

Bst.insert(30);

Bst.insert(70);

Bst.insert(20);

Bst.insert(40);

// Searching nodes

Bst.search(40);
Bst.search(60);

// Displaying tree

Bst.displayInorder();

// Removing a node

Bst.remove(30);

// Displaying updated tree

Bst.displayInorder();

Return 0;

[04/07, 23:56] Ashutosh K: 14. To represent a Sparse Matrix.

Ans –

#include <iostream>

Using namespace std;

Const int MAX_SIZE = 100;

Class SparseMatrix {

Private:

Int matrix[MAX_SIZE][3];

Int rows, cols, numTerms;

Public:

SparseMatrix(int r, int c) : rows(r), cols(c), numTerms(0) {}

Void readMatrix() {

Cout << “Enter the elements of the matrix:” << endl;

For (int i = 0; i < rows; i++) {


For (int j = 0; j < cols; j++) {

Int element;

Cin >> element;

If (element != 0) {

Matrix[numTerms][0] = i;

Matrix[numTerms][1] = j;

Matrix[numTerms][2] = element;

numTerms++;

Void displayMatrix() {

Cout << “Sparse Matrix Representation:” << endl;

Cout << “Row\tColumn\tValue” << endl;

For (int i = 0; i < numTerms; i++) {

Cout << matrix[i][0] << “\t” << matrix[i][1] << “\t” << matrix[i][2] << endl;

};

Int main() {

Int rows, cols;

Cout << “Enter the number of rows and columns of the matrix: “;

Cin >> rows >> cols;

SparseMatrix sparseMatrix(rows, cols);

sparseMatrix.readMatrix();

sparseMatrix.displayMatrix();
return 0;

[04/07, 23:57] Ashutosh K: 15. To perform binary search operation.

Ans –

#include <iostream>

Using namespace std;

Int binarySearch(int arr[], int size, int target) {

Int left = 0;

Int right = size – 1;

While (left <= right) {

Int mid = left + (right – left) / 2;

If (arr[mid] == target) {

Return mid;

} else if (arr[mid] < target) {

Left = mid + 1;

} else {

Right = mid – 1;

Return -1;

Int main() {

Int arr[] = {2, 5, 10, 15, 20, 25, 30};

Int size = sizeof(arr) / sizeof(arr[0]);


Int target;

Cout << “Enter the element to search: “;

Cin >> target;

Int index = binarySearch(arr, size, target);

If (index != -1) {

Cout << “Element found at index “ << index << endl;

} else {

Cout << “Element not found in the array” << endl;

Return 0;

[05/07, 00:04] Ashutosh K: 16. To perform Bubble sort.

Ans –

#include <iostream>

Using namespace std;

Const int MAX_SIZE = 100;

Class Queue {

Private:

Int front, rear;

Int arr[MAX_SIZE];

Public:

Queue() {

Front = -1;

Rear = -1;

}
Bool isEmpty() {

Return (front == -1 && rear == -1);

Bool isFull() {

Return (rear == MAX_SIZE – 1);

Void enqueue(int element) {

If (isFull()) {

Cout << “Queue is full. Cannot enqueue element.” << endl;

Return;

If (isEmpty()) {

Front = 0;

Rear++;

Arr[rear] = element;

Int dequeue() {

If (isEmpty()) {

Cout << “Queue is empty. Cannot dequeue element.” << endl;

Return -1;

Int dequeuedElement = arr[front];


If (front == rear) {

Front = -1;

Rear = -1;

} else {

Front++;

Return dequeuedElement;

Void displayQueue() {

If (isEmpty()) {

Cout << “Queue is empty.” << endl;

Return;

Cout << “Queue elements: “;

For (int i = front; i <= rear; i++) {

Cout << arr[i] << “ “;

Cout << endl;

Void bubbleSort() {

If (isEmpty()) {

Cout << “Queue is empty. Cannot perform Bubble Sort.” << endl;

Return;

Int n = rear – front + 1;


For (int i = 0; i < n – 1; i++) {

For (int j = 0; j < n – i – 1; j++) {

If (arr[j] > arr[j + 1]) {

// Swapping the elements

Int temp = arr[j];

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

Arr[j + 1] = temp;

Cout << “Bubble Sort has been performed on the queue.” << endl;

};

Int main() {

Queue queue;

// Enqueuing elements

Queue.enqueue(5);

Queue.enqueue(10);

Queue.enqueue(3);

Queue.enqueue(8);

Cout << “Before sorting:” << endl;

Queue.displayQueue();

// Performing Bubble Sort

Queue.bubbleSort();

Cout << “After sorting using Bubble Sort:” << endl;


Queue.displayQueue();

Return 0;

[05/07, 00:06] Ashutosh K: 17. To perform Selection sort.

Ans –

#include <iostream>

Using namespace std;

Const int MAX_SIZE = 100;

Class Stack {

Private:

Int top;

Int arr[MAX_SIZE];

Public:

Stack() {

Top = -1;

Bool isEmpty() {

Return (top == -1);

Bool isFull() {

Return (top == MAX_SIZE – 1);

Void push(int element) {

If (isFull()) {
Cout << “Stack is full. Cannot push element.” << endl;

Return;

Top++;

Arr[top] = element;

Int pop() {

If (isEmpty()) {

Cout << “Stack is empty. Cannot pop element.” << endl;

Return -1;

Int poppedElement = arr[top];

Top--;

Return poppedElement;

Void displayStack() {

If (isEmpty()) {

Cout << “Stack is empty.” << endl;

Return;

Cout << “Stack elements: “;

For (int i = top; i >= 0; i--) {

Cout << arr[i] << “ “;

Cout << endl;


}

Void selectionSort() {

If (isEmpty()) {

Cout << “Stack is empty. Cannot perform Selection Sort.” << endl;

Return;

Int n = top + 1;

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;

// Swapping the elements

Int temp = arr[i];

Arr[i] = arr[minIndex];

Arr[minIndex] = temp;

Cout << “Selection Sort has been performed on the stack.” << endl;

};

Int main() {

Stack stack;
// Pushing elements

Stack.push(5);

Stack.push(10);

Stack.push(3);

Stack.push(8);

Cout << “Before sorting:” << endl;

Stack.displayStack();

// Performing Selection Sort

Stack.selectionSort();

Cout << “After sorting using Selection Sort:” << endl;

Stack.displayStack();

Return 0;

[05/07, 00:07] Ashutosh K: 18. To perform Insertion sort.

Ans –

#include <iostream>

Using namespace std;

Const int MAX_SIZE = 100;

Class CircularQueue {

Private:

Int front, rear;

Int arr[MAX_SIZE];

Public:
CircularQueue() {

Front = -1;

Rear = -1;

Bool isEmpty() {

Return (front == -1 && rear == -1);

Bool isFull() {

Return ((rear + 1) % MAX_SIZE == front);

Void enqueue(int element) {

If (isFull()) {

Cout << “Queue is full. Cannot enqueue element.” << endl;

Return;

If (isEmpty()) {

Front = 0;

Rear = (rear + 1) % MAX_SIZE;

Arr[rear] = element;

Int dequeue() {

If (isEmpty()) {

Cout << “Queue is empty. Cannot dequeue element.” << endl;

Return -1;
}

Int dequeuedElement = arr[front];

If (front == rear) {

Front = -1;

Rear = -1;

} else {

Front = (front + 1) % MAX_SIZE;

Return dequeuedElement;

Void displayQueue() {

If (isEmpty()) {

Cout << “Queue is empty.” << endl;

Return;

Cout << “Queue elements: “;

Int i = front;

While (i != rear) {

Cout << arr[i] << “ “;

I = (i + 1) % MAX_SIZE;

Cout << arr[rear] << endl;

Void insertionSort() {

If (isEmpty()) {
Cout << “Queue is empty. Cannot perform Insertion Sort.” << endl;

Return;

Int n = (rear – front + MAX_SIZE + 1) % MAX_SIZE;

For (int i = 1; i < n; i++) {

Int key = arr[i];

Int j = i – 1;

While (j >= 0 && arr[j] > key) {

Arr[(j + 1) % MAX_SIZE] = arr[j];

J = (j – 1 + MAX_SIZE) % MAX_SIZE;

Arr[(j + 1) % MAX_SIZE] = key;

Cout << “Insertion Sort has been performed on the circular queue.” << endl;

};

Int main() {

CircularQueue queue;

// Enqueuing elements

Queue.enqueue(5);

Queue.enqueue(10);

Queue.enqueue(3);

Queue.enqueue(8);
Cout << “Before sorting:” << endl;

Queue.displayQueue();

// Performing Insertion Sort

Queue.insertionSort();

Cout << “After sorting using Insertion Sort:” << endl;

Queue.displayQueue();

Return 0;

[05/07, 00:08] Ashutosh K: 19. To perform Quick sort.

Ans –

#include <iostream>

Using namespace std;

Class Node {

Public:

Int data;

Node* next;

Node(int value) {

Data = value;

Next = nullptr;

};

Class LinkedList {

Private:

Node* head;
Public:

LinkedList() {

Head = nullptr;

Void insertNode(int value) {

Node* newNode = new Node(value);

If (head == nullptr) {

Head = newNode;

} else {

Node* temp = head;

While (temp->next != nullptr) {

Temp = temp->next;

Temp->next = newNode;

Void displayList() {

If (head == nullptr) {

Cout << “List is empty.” << endl;

Return;

Cout << “List elements: “;

Node* temp = head;

While (temp != nullptr) {

Cout << temp->data << “ “;

Temp = temp->next;

}
Cout << endl;

Node* getTailNode() {

If (head == nullptr) {

Return nullptr;

Node* temp = head;

While (temp->next != nullptr) {

Temp = temp->next;

Return temp;

Node* partition(Node* low, Node* high) {

Int pivot = high->data;

Node* i = low->next;

For (Node* j = low; j != high; j = j->next) {

If (j->data < pivot) {

Swap(i->data, j->data);

I = i->next;

Swap(i->data, high->data);

Return i;

Void quickSort(Node* low, Node* high) {


If (low != nullptr && high != nullptr && low != high && low != high->next) {

Node* pivot = partition(low, high);

Node* prev = low;

While (prev->next != pivot) {

Prev = prev->next;

quickSort(low, prev);

quickSort(pivot->next, high);

Void performQuickSort() {

Node* tail = getTailNode();

quickSort(head, tail);

cout << “Quick Sort has been performed on the linked list.” << endl;

};

Int main() {

LinkedList list;

// Inserting elements into the linked list

List.insertNode(5);

List.insertNode(10);

List.insertNode(3);

List.insertNode(8);

Cout << “Before sorting:” << endl;

List.displayList();
// Performing Quick Sort

List.performQuickSort();

Cout << “After sorting using Quick Sort:” << endl;

List.displayList();

Return 0;

[05/07, 00:09] Ashutosh K: 20. To perform Merge sort.

Ans –

#include <iostream>

Using namespace std;

Const int MAX_SIZE = 100;

Class Stack {

Private:

Int top;

Int arr[MAX_SIZE];

Public:

Stack() {

Top = -1;

Bool isEmpty() {

Return (top == -1);

Bool isFull() {
Return (top == MAX_SIZE – 1);

Void push(int element) {

If (isFull()) {

Cout << “Stack is full. Cannot push element.” << endl;

Return;

Arr[++top] = element;

Int pop() {

If (isEmpty()) {

Cout << “Stack is empty. Cannot pop element.” << endl;

Return -1;

Return arr[top--];

Int peek() {

If (isEmpty()) {

Cout << “Stack is empty.” << endl;

Return -1;

Return arr[top];

Void displayStack() {
If (isEmpty()) {

Cout << “Stack is empty.” << endl;

Return;

Cout << “Stack elements: “;

For (int i = top; i >= 0; i--) {

Cout << arr[i] << “ “;

Cout << endl;

Void merge(int left[], int leftSize, int right[], int rightSize) {

Int i = 0, j = 0, k = 0;

While (i < leftSize && j < rightSize) {

If (left[i] <= right[j]) {

Arr[k++] = left[i++];

} else {

Arr[k++] = right[j++];

While (i < leftSize) {

Arr[k++] = left[i++];

While (j < rightSize) {

Arr[k++] = right[j++];

}
Void mergeSort(int input[], int size) {

If (size < 2) {

Return;

Int mid = size / 2;

Int left[mid];

Int right[size – mid];

For (int i = 0; i < mid; i++) {

Left[i] = input[i];

For (int i = mid; i < size; i++) {

Right[i – mid] = input[i];

mergeSort(left, mid);

mergeSort(right, size – mid);

merge(left, mid, right, size – mid);

Void performMergeSort() {

Int size;

Cout << “Enter the size of the array: “;

Cin >> size;

Int input[size];
Cout << “Enter the elements of the array: “;

For (int i = 0; i < size; i++) {

Cin >> input[i];

mergeSort(input, size);

cout << “Merge Sort has been performed on the array.” << endl;

cout << “Sorted array elements: “;

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

cout << input[i] << “ “;

Cout << endl;

};

Int main() {

Stack stack;

// Performing Merge Sort

Stack.performMergeSort();

Return 0;

You might also like