0% found this document useful (0 votes)
23 views4 pages

C++ DataStructures Sorting

The document provides C++ implementations for data structures and algorithms, including a singly linked list, a binary search tree (BST), and various sorting algorithms such as bubble sort, insertion sort, selection sort, merge sort, and quick sort. Each section includes code snippets demonstrating how to create and manipulate these data structures and perform sorting. The document concludes with a sample usage of the sorting algorithms.

Uploaded by

kumarshivamprof
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)
23 views4 pages

C++ DataStructures Sorting

The document provides C++ implementations for data structures and algorithms, including a singly linked list, a binary search tree (BST), and various sorting algorithms such as bubble sort, insertion sort, selection sort, merge sort, and quick sort. Each section includes code snippets demonstrating how to create and manipulate these data structures and perform sorting. The document concludes with a sample usage of the sorting algorithms.

Uploaded by

kumarshivamprof
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

Linked List, Tree, and Sorting in C++

1. Singly Linked List in C++

#include <iostream>
using namespace std;

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

void append(Node*& head, int value) {


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

void printList(Node* head) {


while (head) {
cout << head->data << " -> ";
head = head->next;
}
cout << "NULL\n";
}

int main() {
Node* head = nullptr;
append(head, 10);
append(head, 20);
append(head, 30);
printList(head);
return 0;
}

2. Binary Search Tree (BST) in C++

#include <iostream>
using namespace std;

struct TreeNode {
int val;
TreeNode *left, *right;
Linked List, Tree, and Sorting in C++

};

TreeNode* insert(TreeNode* root, int val) {


if (!root) return new TreeNode{val, nullptr, nullptr};
if (val < root->val)
root->left = insert(root->left, val);
else
root->right = insert(root->right, val);
return root;
}

void inorder(TreeNode* root) {


if (!root) return;
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}

int main() {
TreeNode* root = nullptr;
root = insert(root, 15);
insert(root, 10);
insert(root, 20);
insert(root, 8);
insert(root, 12);
cout << "Inorder Traversal: ";
inorder(root);
return 0;
}

3a. Bubble Sort

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]);
}

3b. Insertion Sort

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


for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
arr[j + 1] = arr[j--];
Linked List, Tree, and Sorting in C++

arr[j + 1] = key;
}
}

3c. Selection Sort

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


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

3d. Merge Sort

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


int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; ++i) L[i] = arr[l + i];
for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
arr[k++] = (L[i] < R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

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


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

3e. Quick Sort

int partition(int arr[], int low, int high) {


int pivot = arr[high], i = low - 1;
for (int j = low; j < high; ++j)
if (arr[j] < pivot)
swap(arr[++i], arr[j]);
Linked List, Tree, and Sorting in C++

swap(arr[i + 1], arr[high]);


return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

Sample Sorting Usage

#include <iostream>
using namespace std;

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

// bubbleSort(arr, n);
// insertionSort(arr, n);
// selectionSort(arr, n);
// mergeSort(arr, 0, n - 1);
quickSort(arr, 0, n - 1);

cout << "Sorted array: ";


for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}

You might also like