Ass Gnment
Ass Gnment
---
Advanced sorting algorithms are used for efficient sorting, especially on large datasets. Examples
include:
Merge Sort
Quick Sort
Heap Sort
Shell Sort
These algorithms provide better time complexity than simple ones like Bubble Sort or Insertion Sort.
Each is explained below.
---
Code in C++:
#include <iostream>
int j;
arr[j] = temp;
int main() {
shellSort(arr, n);
cout << "Sorted array: ";
return 0;
Explanation:
---
Quick Sort uses the divide-and-conquer technique. It partitions the array around a pivot and recursively
sorts the subarrays.
Code in C++:
#include <iostream>
int i = low - 1;
i++;
swap(arr[i], arr[j]);
return i + 1;
quickSort(arr, pi + 1, high);
}
}
int main() {
quickSort(arr, 0, n - 1);
return 0;
Explanation:
2. Elements smaller than the pivot are moved to the left, and larger ones to the right.
Heap Sort builds a max heap and repeatedly extracts the maximum element, placing it at the end of the
array.
Code in C++:
#include <iostream>
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
heapify(arr, n, i);
swap(arr[0], arr[i]);
heapify(arr, i, 0);
int main() {
heapSort(arr, n);
return 0;
Explanation:
1. A max heap is created from the array.
2. The largest element is swapped with the last element, and the heap size is reduced.
---
Merge Sort is a divide-and-conquer algorithm that divides the array, sorts the subarrays, and merges
them.
Code in C++:
#include <iostream>
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
int i = 0, j = 0, k = l;
arr[k++] = L[i++];
else
arr[k++] = R[j++];
arr[k++] = L[i++];
arr[k++] = R[j++];
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int main() {
mergeSort(arr, 0, n - 1);
return 0;
Explanation:
2. The halves are sorted and merged into a single sorted array.
---
Examples of advanced searching include Binary Search, Search Trees, and Hashing.
#include <iostream>
if (arr[mid] == target)
return mid;
low = mid + 1;
else
high = mid - 1;
return -1;
}
int main() {
int target = 5;
if (index != -1)
else
return 0;
Explanation:
(g) Hashing
Hashing maps keys to values using a hash function for fast retrieval.
#include <iostream>
#include <list>
class HashTable {
int size;
public:
HashTable(int s) {
size = s;
hash += c;
if (p.first == key) {
p.second = value;
return;
table[index].emplace_back(key, value);
if (p.first == key)
return p.second;