0% found this document useful (0 votes)
6 views

CPP Programs

Cpp program

Uploaded by

ayushwagh1st
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)
6 views

CPP Programs

Cpp program

Uploaded by

ayushwagh1st
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/ 4

1) Merge Sort

#include <iostream>

using namespace std;

void merge(int a[], int l, int m, int r) {

int i = l, j = m + 1, k = 0, temp[r - l + 1];

while (i <= m && j <= r) temp[k++] = (a[i] < a[j]) ? a[i++] : a[j++];

while (i <= m) temp[k++] = a[i++];

while (j <= r) temp[k++] = a[j++];

for (i = l, k = 0; i <= r; i++) a[i] = temp[k++];

void mergeSort(int a[], int l, int r) {

if (l < r) {

int m = (l + r) / 2;

mergeSort(a, l, m);

mergeSort(a, m + 1, r);

merge(a, l, m, r);

int main() {

int a[] = {5, 3, 8, 6, 2};

mergeSort(a, 0, 4);

for (int i : a) cout << i << ' ';

2) Single Linked List Reverse Traversal


#include <iostream>

using namespace std;

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

void reversePrint(Node* head) {

if (!head) return;

reversePrint(head->next);

cout << head->data << ' ';

void push(Node*& head, int data) {

Node* newNode = new Node{data, head};

head = newNode;

int main() {

Node* head = nullptr;

push(head, 1); push(head, 2); push(head, 3);

reversePrint(head);

3) Priority Queue using Linked List

#include <iostream>

using namespace std;

struct Node { int data, priority; Node* next; };

void push(Node*& head, int d, int p) {

Node* newNode = new Node{d, p, nullptr};

if (!head || head->priority > p) { newNode->next = head; head = newNode; return; }

Node* temp = head;


while (temp->next && temp->next->priority <= p) temp = temp->next;

newNode->next = temp->next; temp->next = newNode;

int pop(Node*& head) { int d = head->data; head = head->next; return d; }

int main() {

Node* pq = nullptr;

push(pq, 10, 2); push(pq, 5, 1); push(pq, 20, 3);

while (pq) cout << pop(pq) << ' ';

4) BST Pre-order and Post-order Traversal

#include <iostream>

using namespace std;

struct Node { int data; Node *left, *right; };

void preorder(Node* root) { if (!root) return; cout << root->data << ' ';

preorder(root->left); preorder(root->right); }

void postorder(Node* root) { if (!root) return; postorder(root->left);

postorder(root->right); cout << root->data << ' '; }

Node* newNode(int data) { return new Node{data, nullptr, nullptr}; }

int main() {

Node* root = newNode(1);

root->left = newNode(2); root->right = newNode(3);

preorder(root); cout << endl; postorder(root);

5) Selection Sort for Array of Strings


#include <iostream>

#include <string>

using namespace std;

void selectionSort(string arr[], int n) {

for (int i = 0; i < n - 1; i++) {

int minIdx = i;

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

if (arr[j] < arr[minIdx]) minIdx = j;

swap(arr[i], arr[minIdx]);

int main() {

string arr[] = {"apple", "orange", "banana"};

selectionSort(arr, 3);

for (string s : arr) cout << s << ' ';

You might also like