0% found this document useful (0 votes)
4 views32 pages

DSA Questions

The document contains various C++ programming exercises covering algorithms and data structures, including recursive and iterative binary search, linear search, bubble sort, and the Towers of Hanoi. It also includes implementations of stacks and queues using arrays, lists, and linked lists, as well as functions for manipulating arrays and counting occurrences. Each section provides code examples and main functions to demonstrate the functionality of the algorithms and data structures.

Uploaded by

jadonrajat25
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)
4 views32 pages

DSA Questions

The document contains various C++ programming exercises covering algorithms and data structures, including recursive and iterative binary search, linear search, bubble sort, and the Towers of Hanoi. It also includes implementations of stacks and queues using arrays, lists, and linked lists, as well as functions for manipulating arrays and counting occurrences. Each section provides code examples and main functions to demonstrate the functionality of the algorithms and data structures.

Uploaded by

jadonrajat25
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/ 32

Practical Class-1

1. Q1) Recursive Binary Search Function

#include <iostream>
using namespace std;

int binarySearchRecursive(int arr[], int low, int high,


int key) {
if (low > high)
return -1; // Base case: element not found

int mid = low + (high - low) / 2;

if (arr[mid] == key)
return mid; // Element found at mid

if (arr[mid] > key)


return binarySearchRecursive(arr, low, mid - 1,
key);

return binarySearchRecursive(arr, mid + 1, high,


key);
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = binarySearchRecursive(arr, 0, n - 1,
key);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result <<
endl;
return 0;
}

2. Q2) Iterative Binary Search


#include <iostream>
using namespace std;

int binarySearchIterative(int arr[], int n, int


key) {
int low = 0, high = n - 1; while (low <=
high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid; // Element found
else if (arr[mid] < key)
low = mid + 1; // Search in right half
else
high = mid - 1; // Search in left half
}
return -1; // Element not found
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = binarySearchIterative(arr, n,
key);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result
<< endl;
return 0;
}

3. Q3) Iterative Linear Search


#include <iostream>
using namespace std;

int linearSearchIterative(int arr[], int n, int


key) {
for (int i = 0; i < n; ++i) {
if (arr[i] == key)
return i; // Element found
}
return -1; // Element not found
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = linearSearchIterative(arr, n,
key);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result
<< endl;
return 0;
}

4. Q4) Recursive Code for Linear Search


#include <iostream>
using namespace std;

int linearSearchRecursive(int arr[], int n, int


key, int index) {
if (index == n)
return -1; // Element not found
if (arr[index] == key)
return index; // Element found
return linearSearchRecursive(arr, n, key, index
+ 1);
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 6;
int result = linearSearchRecursive(arr, n, key,
0);
if (result == -1)
cout << "Element not found\n";
else
cout << "Element found at index " << result
<< endl;
return 0;
}

5. Q5) Bubble Sort


#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
}
}

int main() {
int arr[] = {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}

6. Q6) Towers of Hanoi


7. #include <iostream>
8. using namespace std;
9.
10. void towersOfHanoi(int n, char source, char
auxiliary, char destination) {
11. if (n == 1) {
12. cout << "Move disk 1 from " << source
<< " to " << destination << endl;
13. return;
14. }
15. towersOfHanoi(n - 1, source, destination,
auxiliary);
16. cout << "Move disk " << n << " from " <<
source << " to " << destination << endl;
17. towersOfHanoi(n - 1, auxiliary, source,
destination);
18. }
19.
20. int main() {
21. int n = 3; // Number of disks
22. towersOfHanoi(n, 'A', 'B', 'C'); // A, B,
and C are the names of the rods
23. return 0;
}
Practical Class-2
7. Q1) Stack implementation using Array
#include <iostream>
using namespace std;

#define MAX_SIZE 100

class Stack {
private:
int arr[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}

void push(int value) {


if (top >= MAX_SIZE - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = value;
}

int pop() {
if (top < 0) {
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}

int peek() {
if (top < 0) {
cout << "Stack is empty\n";
return -1;
}
return arr[top];
}

bool isEmpty() {
return (top < 0);
}
};

int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "Top element: " << s.peek() << endl;
cout << "Popped element: " << s.pop() << endl;
cout << "Top element: " << s.peek() << endl;
return 0;
}
8. Q2) Stack using List
#include <iostream>
#include <list>
using namespace std;

class Stack {
private:
list<int> data;
public:
void push(int value) {
data.push_back(value);
}
int pop() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
int value = data.back();
data.pop_back();
return value;
}

int peek() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
return data.back();
}

bool isEmpty() {
return data.empty();
}
};

int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "Top element: " << s.peek() << endl;
cout << "Popped element: " << s.pop() << endl;
cout << "Top element: " << s.peek() << endl;
return 0;
}
9. Q3) Stack using Linked List
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

class Stack {
private:
Node* top;
public:
Stack() {
top = nullptr;
}

void push(int value) {


Node* newNode = new Node(value);
newNode->next = top;
top = newNode;
}

int pop() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
int value = top->data;
Node* temp = top;
top = top->next;
delete temp;
return value;
}

int peek() {
if (isEmpty()) {
cout << "Stack is empty\n";
return -1;
}
return top->data;
}
bool isEmpty() {
return (top == nullptr);
}
};

int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "Top element: " << s.peek() << endl;
cout << "Popped element: " << s.pop() << endl;
cout << "Top element: " << s.peek() << endl;
return 0;
}
10. Q4) Queue implementation using Array
#include <iostream>
using namespace std;

#define MAX_SIZE 100

class Queue {
private:
int arr[MAX_SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}

void enqueue(int value) {


if (rear >= MAX_SIZE - 1) {
cout << "Queue Overflow\n";
return;
}
arr[++rear] = value;
if (front == -1)
front = 0;
}
int dequeue() {
if (isEmpty()) {
cout << "Queue Underflow\n";
return -1;
}
int value = arr[front];
if (front == rear)
front = rear = -1;
else
front++;
return value;
}

int peek() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
return arr[front];
}

bool isEmpty() {
return (front == -1);
}
};

int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << "Front element: " << q.peek() << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
cout << "Front element: " << q.peek() << endl;
return 0;
}
11. Q5) Queue using List
#include <iostream>
#include <list>
using namespace std;
class Queue {
private:
list<int> data;
public:
void enqueue(int value) {
data.push_back(value);
}

int dequeue() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
int value = data.front();
data.pop_front();
return value;
}

int peek() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
return data.front();
}

bool isEmpty() {
return data.empty();
}
};

int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << "Front element: " << q.peek() << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
cout << "Front element: " << q.peek() << endl;
return 0;
}
12. Q6) Queue using Linked List
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = nullptr;
rear = nullptr;
}

void enqueue(int value) {


Node* newNode = new Node(value);
if (rear == nullptr) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}

int dequeue() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
int value = front->data;
Node* temp = front;
if (front == rear)
front = rear = nullptr;
else
front = front->next;
delete temp;
return value;
}

int peek() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
}
return front->data;
}

bool isEmpty() {
return (front == nullptr);
}
};

int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << "Front element: " << q.peek() << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
cout << "Front element: " << q.peek() << endl;
return 0;
}
Practical Class-3
13. Q1) Write a C++ program to separate 0s and 1s from a given
array of values 0 and 1.
#include <iostream>
using namespace std;

void separateZerosOnes(int arr[], int n) {


int left = 0, right = n - 1;
while (left < right) {
while (arr[left] == 0 && left < right)
left++;
while (arr[right] == 1 && left < right)
right--;
if (left < right) {
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}

int main() {
int arr[] = {0, 1, 0, 1, 1, 0, 0, 1};
int n = sizeof(arr) / sizeof(arr[0]);
separateZerosOnes(arr, n);
cout << "Array after separating 0s and 1s: ";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}
14. Q2) Function to count occurrences of a specific number 'x' in
an array 'arr' of size 'n'
#include <iostream>
using namespace std;

int countOccurrences(int arr[], int n, int x) {


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

int main() {
int arr[] = {1, 2, 3, 4, 2, 2, 5, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 2;
cout << "Number of occurrences of " << x << ": " <<
countOccurrences(arr, n, x) << endl;
return 0;
}
15. Q3) Indirect recursion
#include <iostream>
using namespace std;

void funB(int n);

void funA(int n) {
if (n > 0) {
cout << n << " ";
funB(n - 1);
}
}

void funB(int n) {
if (n > 1) {
cout << n << " ";
funA(n / 2);
}
}

int main() {
funA(20);
return 0;
}
Practical Class-4
16. Q1) Circular Linked list
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

class CircularLinkedList {
private:
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}

void insert(int value) {


Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
}

void display() {
if (head == nullptr) {
cout << "List is empty\n";
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};

int main() {
CircularLinkedList list;
list.insert(1);
list.insert(2);
list.insert(3);
list.display();
return 0;
}
17. Q2) Singly Linked list
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

class SinglyLinkedList {
private:
Node* head;
public:
SinglyLinkedList() {
head = nullptr;
}

void insert(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 display() {
if (head == nullptr) {
cout << "List is empty\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
SinglyLinkedList list;
list.insert(1);
list.insert(2);
list.insert(3);
list.display();
return 0;
}
18. Q3) Doubly Linked List using Class
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* prev;
Node* next;

Node(int value) {
data = value;
prev = nullptr;
next = nullptr;
}
};

class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}

void insert(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;
newNode->prev = temp;
}
}

void display() {
if (head == nullptr) {
cout << "List is empty\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
DoublyLinkedList list;
list.insert(1);
list.insert(2);
list.insert(3);
list.display();
return 0;
}
Practical Class-5
19. Q1) Doubly Linked List
#include <iostream>
using namespace std;

struct Node {
int data;
Node* prev;
Node* next;
};

Node* createNode(int value) {


Node* newNode = new Node;
newNode->data = value;
newNode->prev = nullptr;
newNode->next = nullptr;
return newNode;
}

void insert(Node** head, int value) {


Node* newNode = createNode(value);
if (*head == nullptr) {
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}

void display(Node* head) {


if (head == nullptr) {
cout << "List is empty\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

int main() {
Node* head = nullptr;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
display(head);
return 0;
}
20. Q2) Tree Traversal
#include <iostream>
using namespace std;

class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;

TreeNode(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};

void inorderTraversal(TreeNode* root) {


if (root == nullptr)
return;
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}

void preorderTraversal(TreeNode* root) {


if (root == nullptr)
return;
cout << root->data << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}

void postorderTraversal(TreeNode* root) {


if (root == nullptr)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->data << " ";
}

int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);

cout << "Inorder Traversal: ";


inorderTraversal(root);
cout << endl;

cout << "Preorder Traversal: ";


preorderTraversal(root);
cout << endl;

cout << "Postorder Traversal: ";


postorderTraversal(root);
cout << endl;

return 0;
}
Practical Class-6
21. Q1) Binary Search Tree operations
#include <iostream>
using namespace std;

class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;

TreeNode(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};

class BST {
private:
TreeNode* root;

TreeNode* insertRecursive(TreeNode* root, int value)


{
if (root == nullptr)
return new TreeNode(value);
if (value < root->data)
root->left = insertRecursive(root->left,
value);
else if (value > root->data)
root->right = insertRecursive(root->right,
value);
return root;
}

void inorderTraversal(TreeNode* root) {


if (root == nullptr)
return;
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}

public:
BST() {
root = nullptr;
}

void insert(int value) {


root = insertRecursive(root, value);
}
void inorder() {
inorderTraversal(root);
cout << endl;
}
};

int main() {
BST bst;
bst.insert(4);
bst.insert(2);
bst.insert(6);
bst.insert(1);
bst.insert(3);

cout << "Inorder Traversal: ";


bst.inorder();

return 0;
}
22. Q2) Tree Traversal using C style
#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};

struct TreeNode* createNode(int value) {


struct TreeNode* newNode = (struct
TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

void inorderTraversal(struct TreeNode* root) {


if (root == NULL)
return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

void preorderTraversal(struct TreeNode* root) {


if (root == NULL)
return;
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

void postorderTraversal(struct TreeNode* root) {


if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}

int main() {
struct TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

printf("Inorder Traversal: ");


inorderTraversal(root);
printf("\n");

printf("Preorder Traversal: ");


preorderTraversal(root);
printf("\n");

printf("Postorder Traversal: ");


postorderTraversal(root);
printf("\n");

return 0;
}
23. Q3) AVL tree
24. #include <iostream>
25. using namespace std;
26.
27. struct TreeNode {
28. int data;
29. TreeNode* left;
30. TreeNode* right;
31. int height;
32. };
33.
34. int max(int a, int b) {
35. return (a > b) ? a : b;
36. }
37.
38. int getHeight(TreeNode* node) {
39. if (node == nullptr)
40. return 0;
41. return node->height;
42. }
43.
44. int getBalance(TreeNode* node) {
45. if (node == nullptr)
46. return 0;
47. return getHeight(node->left) -
getHeight(node->right);
48. }
49.
50. TreeNode* rightRotate(TreeNode* y) {
51. TreeNode* x = y->left;
52. TreeNode* T2 = x->right;
53.
54. x->right = y;
55. y->left = T2;
56.
57. y->height = 1 + max(getHeight(y->left),
getHeight(y->right));
58. x->height = 1 + max(getHeight(x->left),
getHeight(x->right));
59.
60. return x;
61. }
62.
63. TreeNode* leftRotate(TreeNode* x) {
64. TreeNode* y = x->right;
65. TreeNode* T2 = y->left;
66.
67. y->left = x;
68. x->right = T2;
69.
70. x->height = 1 + max(getHeight(x->left),
getHeight(x->right));
71. y->height = 1 + max(getHeight(y->left),
getHeight(y->right));
72.
73. return y;
74. }
75.
76. TreeNode* insert(TreeNode* node, int value) {
77. if (node == nullptr)
78. return new TreeNode{value, nullptr,
nullptr, 1};
79.
80. if (value < node->data)
81. node->left = insert(node->left, value);
82. else if (value > node->data)
83. node->right = insert(node->right,
value);
84. else
85. return node;
86.
87. node->height = 1 + max(getHeight(node-
>left), getHeight(node->right));
88.
89. int balance = getBalance(node);
90.
if (balance > 1 && value < node->left->data)
return rightRotate(node);
91. if (balance < -1 && value > node->right-
>data)
return leftRotate(node);
if (balance > 1 && value > node->left->data) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && value < node->right->data)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}

void inorderTraversal(TreeNode* root) {


if (root == nullptr)
return;
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}

int main() {
TreeNode* root = nullptr;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);

cout << "Inorder Traversal: ";


inorderTraversal(root);
cout << endl;

return 0;
}
Practical Class-7
24. Q1) Max Heap
25. #include <iostream>
26. using namespace std;
27.
28. class MaxHeap {
29. private:
30. int* heap;
31. int capacity;
32. int size;
33.
34. int parent(int i) {
35. return (i - 1) / 2;
36. }
37.
38. int leftChild(int i) {
39. return 2 * i + 1;
40. }
41.
42. int rightChild(int i) {
43. return 2 * i + 2;
44. }
45.
46. void heapifyUp(int i) {
47. while (i > 0 && heap[i] >
heap[parent(i)]) {
48. swap(heap[i], heap[parent(i)]);
49. i = parent(i);
50. }
51. }
52.
53. void heapifyDown(int i) {
54. int maxIndex = i;
55. int left = leftChild(i);
56. int right = rightChild(i);
57.
58. if (left < size && heap[left] >
heap[maxIndex])
59. maxIndex = left;
60.
61. if (right < size && heap[right] >
heap[maxIndex])
62. maxIndex = right;
63.
64. if (i != maxIndex) {
65. swap(heap[i], heap[maxIndex]);
66. heapifyDown(maxIndex);
67. }
68. }
69.
70. public:
71. MaxHeap(int capacity) {
72. this->capacity = capacity;
73. heap = new int[capacity];
74. size = 0;
75. }
76.
77. ~MaxHeap() {
78. delete[] heap;
79. }
80.
81. void insert(int value) {
82. if (size >= capacity) {
83. cout << "Heap overflow\n";
84. return;
85. }
86. heap[size++] = value;
87. heapifyUp(size - 1);
}
88.
int extractMax() {
if (size <= 0) {
cout << "Heap underflow\n";
return -1;
}
int max = heap[0];
heap[0] = heap[--size];
heapifyDown(0);
return max;
}

void printHeap() {
for (int i = 0; i < size; ++i)
cout << heap[i] << " ";
cout << endl;
}
};

int main() {
MaxHeap maxHeap(10);
maxHeap.insert(10);
maxHeap.insert(20);
maxHeap.insert(15);
maxHeap.insert(30);
maxHeap.insert(25);

cout << "Max Heap: ";


maxHeap.printHeap();

cout << "Extracted Max: " <<


maxHeap.extractMax() << endl;

cout << "Heap after extraction: ";


maxHeap.printHeap();

return 0;
}
Practical Class-8
25. Q1) Heap Sort
#include <iostream>
using namespace std;

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

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 << endl;
}

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);

heapSort(arr, n);

cout << "Sorted array: ";


printArray(arr, n);

return 0;
}

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