Exp ALL
Exp ALL
h>
if (arr[i] == x)
return i;
return -1;
int main() {
int n = 1000;
int arr[n];
arr[i] = i + 1;
x = 1;
start = clock();
linearSearch(arr, n, x);
end = clock();
x = n / 2;
start = clock();
linearSearch(arr, n, x);
end = clock();
x = n + 1;
start = clock();
end = clock();
return 0;
OUTPUT-
Source Code:
#include <stdio.h>
int main() {
int n = sizeof(arr)/sizeof(arr[0]);
int i, j, temp;
printf("Input: \n");
printf("\n");
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
printf("Output: \n");
printf("\n");
Input-Output:
Input: Output:
64 34 25 12 22 11 60 11 12 22 25 34 64 60
Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the
smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of
the list. The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of
the list and swaps it with the first element of the unsorted part. This process is repeated for the
remaining unsorted portion until the entire list is sorted.
Source Code:
#include <stdio.h>
int main() {
int n = sizeof(arr)/sizeof(arr[0]);
printf("\n");
min_idx = i;
min_idx = j;
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
printf("\n");
return 0;
}
Input-Output:
64 25 12 22 11 11 12 22 25 64
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an
unsorted list into its correct position in a sorted portion of the list. It is a stable sorting algorithm,
meaning that elements with equal values maintain their relative order in the sorted output.
Source Code:
#include <stdio.h>
int main() {
int n = sizeof(arr)/sizeof(arr[0]);
int i, j, key;
printf("\n");
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
printf("\n");
return 0;
Input-Output:
5 6 11 12 13 12 11 13 5 6
Aim: To Implement the Best, Worst and Average Cases of Time Complexity
of Quick Sort.
Best Case:
The array is already sorted, and the pivot always divides the array into two equal halves, resulting in a
time complexity of O(nlogn).
Source Code:
#include <stdio.h>
*a = *b;
*b = temp;
i++;
swap(sarr[i], sarr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
printf("\n");
int main() {
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
Input-Output:
Sorted array:
123456
Average Case:
The array elements are in random order, and the pivot divides the array into two subarrays of random
sizes, leading to an average time complexity of O(nlogn).
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
*a = *b;
*b = temp;
i++;
swap(sarr[i], sarr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
printf("\n");
}
int main() {
srand(time(0));
int r = rand() % n;
swap(sarr[i], sarr[r]);
printArray(arr, n);
return 0;
Input-Output:
Sorted array:
1 5 7 8 6 10
Worst Case:
The array is sorted in reverse order, and the pivot always picks the greatest or smallest element,
resulting in a time complexity of O(n^2).
Source Code:
#include <stdio.h>
*a = *b;
*b = temp;
i++;
swap(sarr[i], sarr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
printf("\n");
}
int main() {
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
Input-Output:
Sorted array:
123456
#include <stdlib.h>
int data;
Color color;
} Node;
node->data = data;
node->color = RED;
return node;
Node* y = x->right;
x->right = y->left;
if (y->left) y->left->parent = x;
y->parent = x->parent;
if (!x->parent)
*root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
}
Node* x = y->left;
y->left = x->right;
if (x->right) x->right->parent = y;
x->parent = y->parent;
if (!y->parent)
*root = x;
else if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
x->right = y;
y->parent = x;
}
if (z->parent == z->parent->parent->left) {
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->right) { // Case 2
z = z->parent;
leftRotate(root, z);
}
z->parent->parent->color = RED;
rightRotate(root, z->parent->parent);
} else {
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->left) { // Case 2
z = z->parent;
rightRotate(root, z);
z->parent->parent->color = RED;
leftRotate(root, z->parent->parent);
(*root)->color = BLACK;
Node* z = createNode(data);
Node* y = NULL;
Node* x = *root;
while (x) {
y = x;
x = x->left;
else
x = x->right;
z->parent = y;
if (!y)
*root = z;
y->left = z;
else
y->right = z;
fixInsertion(root, z);
if (!u->parent)
*root = v;
else if (u == u->parent->left)
u->parent->left = v;
else
u->parent->right = v;
while (node->left)
node = node->left;
return node;
if (x == x->parent->left) {
Node* w = x->parent->right;
w->color = BLACK;
leftRotate(root, x->parent);
w = x->parent->right;
w->color = RED;
x = x->parent;
} else {
w->color = RED;
rightRotate(root, w);
w = x->parent->right;
x->parent->color = BLACK;
leftRotate(root, x->parent);
x = *root;
}
} else {
Node* w = x->parent->left;
w->color = BLACK;
x->parent->color = RED;
rightRotate(root, x->parent);
w = x->parent->left;
}
x = x->parent;
} else {
w->color = RED;
leftRotate(root, w);
w = x->parent->left;
x->parent->color = BLACK;
rightRotate(root, x->parent);
x = *root;
// Delete a node
Node* z = *root;
if (!z) return;
Node* y = z;
Color yOriginalColor = y->color;
Node* x;
if (!z->left) {
x = z->right;
} else if (!z->right) {
x = z->left;
transplant(root, z, z->left);
} else {
y = minimum(z->right);
yOriginalColor = y->color;
x = y->right;
if (y->parent == z) {
if (x) x->parent = y;
} else {
transplant(root, y, y->right);
y->right = z->right;
y->right->parent = y;
transplant(root, z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
}
if (yOriginalColor == BLACK)
fixDeletion(root, x);
free(z);
}
if (root) {
inorder(root->left);
inorder(root->right);
}
}
insert(sroot, 10);
insert(sroot, 20);
insert(sroot, 30);
insert(sroot, 15);
insert(sroot, 25);
inorder(root);
printf("\n");
delete(sroot, 20);
inorder(root);
printf("\n");
return 0;
OUTPUT-
#include <stdlib.h>
#define DEGREE 3
node->t = t;
node->leaf = leaf;
node->keys = (int *)malloc((2 * t - 1) * sizeof(int));
node->n = 0;
return node;
}
if (root) {
if (!root->leaf)
traverse(root->children[i]);
if (!root->leaf)
traverse(root->children[root->n]);
int i = 0;
i++;
return root;
if (root->leaf)
return NULL;
int t = child->t;
BTreeNode *newChild = createNode(t, child->leaf);
newChild->n = t - 1;
if (!child->leaf) {
child->n = t - 1;
parent->children[j + 1] = parent->children[j];
parent->children[i + 1] = newChild;
parent->keys[j + 1] = parent->keys[j];
parent->keys[i] = child->keys[t - 1];
int i = node->n - 1;
if (node->leaf) {
node->keys[i + 1] = node->keys[i];
i--;
}
node->keys[i + 1] = key;
node->n++;
} else {
i--;
i++;
if (node->children[i]->n == 2 * node->t - 1) {
splitChild(node, i, node->children[i]);
i++;
}
insertNonFull(node->children[i], key);
if (!root) {
root->keys[0] = key;
root->n = 1;
return root;
}
if (root->n == 2 * DEGREE - 1) {
newRoot->children[0] = root;
splitChild(newRoot, 0, root);
insertNonFull(newRoot->children[i], key);
return newRoot;
insertNonFull(root, key);
return root;
int t = child->t;
child->keys[t - 1] = node->keys[idx];
child->keys[i + t] = sibling->keys[i];
if (!child->leaf) {
child->children[i + t] = sibling->children[i];
node->keys[i - 1] = node->keys[i];
for (int i = idx + 2; i <= node->n; i++)
node->children[i - 1] = node->children[i];
child->n += sibling->n + 1;
node->n--;
free(sibling);
}
node->n--;
if (!root)
return root;
int idx = 0;
idx++;
if (root->leaf) {
removeFromNode(root, idx);
while (!pred->leaf)
pred = pred->children[pred->n];
while (!succ->leaf)
succ = succ->children[0];
root->keys[idx] = succ->keys[0];
} else {
merge(root, idx);
} else if (!root->leaf) {
if (!child->leaf)
leftSibling->n--;
child->n++;
child->keys[child->n] = root->keys[idx];
root->keys[idx] = rightSibling->keys[0];
if (!rightSibling->leaf)
child->n++;
rightSibling->n--;
} else {
merge(root, idx);
else
merge(root, idx - 1);
return root;
int main() {
traverse(root);
printf("\n");
root = removeKey(root, 10);
traverse(root);
printf("\n");
return 0;}
OUTPUT-
Aim: To Implement the min heap for- Heap sort , Binomial Sort and Fibonacci
Heap.
Heap Sort
#include <stdio.h>
#include <stdlib.h>
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
smallest = left;
smallest = right;
if (smallest != i) {
arr[i] = arr[smallest];
arr[smallest] = temp;
heapify(arr, n, smallest);
}
}
heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
printf("\n");
int main() {
printArray(arr, n);
heapSort(arr, n);
printArray(arr, n);
return 0;
OUTPUT-
Binomial Sort
#include <stdio.h>
#include <stdlib.h>
int key;
int degree;
node->key = key;
node->degree = 0;
return node;
BinomialNode *head;
head = h1;
h1 = h1->sibling;
} else {
head = h2;
h2 = h2->sibling;
tail->sibling = h1;
h1 = h1->sibling;
} else {
tail->sibling = h2;
h2 = h2->sibling;
}
tail = tail->sibling;
y->parent = z;
y->sibling = z->child;
z->child = y;
z->degree++;
while (next) {
if ((curr->degree != next->degree) ||
prev = curr;
curr = next;
} else {
curr->sibling = next->sibling;
linkTrees(next, curr);
} else {
linkTrees(curr, next);
curr = next;
}
}
next = curr->sibling;
return head;
while (curr) {
if (curr->key < min) {
min = curr->key;
prevMin = prev;
minNode = curr;
}
prev = curr;
curr = curr->sibling;
while (child) {
child->sibling = newHeap;
child->parent = NULL;
newHeap = child;
child = next;
free(minNode);
return min;
if (node) {
printHeap(node->child);
printHeap(node->sibling);
int main() {
printHeap(heap);
printf("\n");
printHeap(heap);
printf("\n");
return 0;}
OUTPUT-
Fabonacci Heap
#include <stdio.h>
#include <stdlib.h>
int key;
int degree;
int mark;
FibonacciNode *min;
int n;
} FibonacciHeap;
FibonacciHeap *createHeap() {
heap->min = NULL;
heap->n = 0;
return heap;
}
node->key = key;
node->degree = 0;
node->mark = 0;
return node;
if (!heap->min) {
heap->min = node;
} else {
node->left = heap->min;
heap->min->right->left = node;
heap->min->right = node;
heap->min = node;
heap->n++;
if (node) {
do {
printHeap(node->child);
node = node->right;
int main() {
insertNode(heap, n1);
insertNode(heap, n2);
insertNode(heap, n3);
printHeap(heap->min);
printf("\n");
return 0;