DAA File (1) Himanshu
DAA File (1) Himanshu
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
if (arr[i] ==
x) return
i;
return -1;
int main() {
int n =
1000; int
arr[n];
i++) { arr[i] = i +
1;
clock_t start,
end; x = 1;
start = clock();
linearSearch(arr, n,
CLOCKS_PER_SEC); x = n / 2;
start = clock();
Himanshu Jain 2200911540054
CSDS-1 G2
linearSearch(arr, n,
CLOCKS_PER_SEC); x = n + 1;
start = clock();
CLOCKS_PER_SEC); return 0;
OUTPUT-
Source Code:
#include
<stdio.h> int
main() {
int n =
sizeof(arr)/sizeof(arr[0]); int i,
j, temp;
printf("Input: \n");
for (i = 0; i < n;
i++) {
printf("\n");
if (arr[j] >
arr[j+1]) { temp
= arr[j]; arr[j] =
arr[j+1];
arr[j+1] =
temp;
}
Himanshu Jain 2200911540054
CSDS-1 G2
printf("Output: \
n; i++) {
printf("\n");
Input-Output:
Input: Output:
64 34 25 12 22 11 90 11 12 22 25 34 64 90
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]); int i,
j, min_idx, temp;
printf("Original array: \
+) {
printf("\n");
+) { min_idx = i;
min_idx = j;
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("Original array: \
+) {
printf("\n");
for (i = 1; i < n;
i++) { key =
Himanshu 22009115400
Jain
}
arr[i];
j = i - 1;
j = j - 1;
Himanshu 22009115400
Jain
}
arr[j + 1] = key;
printf("Sorted array: \
+) {
printf("\n");
return 0;
Input-Output:
5 6 11 12 13 12 11 13 5 6
Himanshu 22009115400
Jain
EXPERIMENT-3
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(&arr[i], &arr[j]);
return (i + 1);
- 1); quickSort(arr, pi + 1,
high);
Himanshu 22009115400
Jain
}
i++)
printf("%d ",
arr[i]); printf("\
n");
int main() {
int n = sizeof(arr) /
sizeof(arr[0]); 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>
Himanshu 22009115400
Jain
void swap(int* a, int*
*a = *b;
*b = temp;
Himanshu 22009115400
Jain
int pivot =
arr[high]; int i =
(low - 1);
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
- 1); quickSort(arr, pi + 1,
high);
i++)
printf("%d ",
arr[i]); printf("\
n");
int main() {
int n = sizeof(arr) /
sizeof(arr[0]);
srand(time(0));
Himanshu 22009115400
Jain
for (int i = 0; i < n;
i++) { int r =
rand() % n;
swap(&arr[i], &arr[r]);
Himanshu 22009115400
Jain
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Input-Output:
Sorted array:
1 5 7 8 9 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(&arr[i], &arr[j]);
return (i + 1);
Himanshu 22009115400
Jain
}
- 1); quickSort(arr, pi + 1,
high);
i++)
printf("%d ",
arr[i]); printf("\
n");
int main() {
int n = sizeof(arr) /
sizeof(arr[0]); quickSort(arr,
0, n - 1);
printArray(arr, n);
return 0;
Input-
Output:
Sorted
array: 1 2 3
456
Himanshu 22009115400
Jain
EXPERIMENT-4
<stdio.h>
#include
<stdlib.h>
int data;
Color color;
} Node;
Node* node =
(Node*)malloc(sizeof(Node)); node-
>data = data;
node->color = RED;
x) { 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;
Himanshu 22009115400
Jain
else
x->parent->right
= y; y->left = x;
x->parent = y;
Himanshu 22009115400
Jain
void rightRotate(Node** root, Node*
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;
>parent->left) {
Node* y = z->parent->parent->right; //
Case 1
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);
Himanshu 22009115400
Jain
}
z->parent->color = BLACK; //
Case 3 z->parent->parent-
z->parent->parent);
Himanshu 22009115400
Jain
}
} else {
Node* y = z->parent->parent->left; //
Case 1
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->color = BLACK; //
Case 3 z->parent->parent-
>parent->parent);
(*root)->color = BLACK;
data) { Node* z =
createNode(data); Node* y
= NULL;
Node* x =
y = x;
if (z->data < x-
Himanshu 22009115400
Jain
>data) x = x-
>left;
else
x = x->right;
Himanshu 22009115400
Jain
}
z->parent =
y; if (!y)
*root = z;
>data) y->left = z;
else
y->right = z;
fixInsertion(root, z);
Node* v) { 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-
BLACK)) { if (x == x->parent->left) {
Node* w = x->parent->right;
if (w->color == RED) { //
Case 1 w->color =
Himanshu 22009115400
Jain
BLACK;
Himanshu 22009115400
Jain
x->parent->color =
RED; leftRotate(root, x-
>parent); w = x-
>parent->right;
x = x->parent;
} else {
w->color = RED;
rightRotate(root,
w); w = x-
>parent->right;
w->color = x->parent->color; //
Case 4 x->parent->color =
BLACK;
if (w->right) w->right->color =
x = *root;
} else {
Node* w = x->parent->left;
if (w->color == RED) { //
Case 1 w->color =
BLACK;
x->parent->color = RED;
rightRotate(root, x-
>parent); w = x->parent-
Himanshu 22009115400
Jain
>left;
Himanshu 22009115400
Jain
w->color =
RED; x = x-
>parent;
} else {
BLACK;
w->color = RED;
leftRotate(root,
w); w = x-
>parent->left;
w->color = x->parent->color; //
Case 4 x->parent->color =
BLACK;
if (w->left) w->left->color =
BLACK; rightRotate(root, x-
>parent);
x = *root;
// Delete a node
{ Node* z = *root;
Node* y = z;
Himanshu 22009115400
Jain
Color yOriginalColor = y-
>color; Node* x;
if (!z->left) {
x = z->right;
Himanshu 22009115400
Jain
transplant(root, z, 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);
void inorder(Node*
root) { if (root) {
Himanshu 22009115400
Jain
inorder(root->left);
>data);
inorder(root-
>right);
Himanshu 22009115400
Jain
int main() {
Node* root =
NULL;
insert(&root, 10);
insert(&root, 20);
insert(&root, 30);
insert(&root, 15);
insert(&root, 25);
inorder(root);
printf("\n");
delete(&root, 20);
"); inorder(root);
printf("\n");
return 0;
OUTPU
T-
Himanshu 22009115400
Jain
EXPERIMENT-5
<stdio.h>
#include
<stdlib.h>
#define
DEGREE 3
int t; // Minimum
degree
} BTreeNode;
*)malloc(sizeof(BTreeNode)); node->t = t;
node->leaf = leaf;
return node;
void traverse(BTreeNode
*root) { if (root) {
i++) { if (!root->leaf)
traverse(root-
>children[i]); printf("%d
Himanshu 22009115400
Jain
", root->keys[i]);
if (!root->leaf)
traverse(root->children[root->n]);
Himanshu 22009115400
Jain
}
key) { int i = 0;
>keys[i]) i++;
if (root->leaf)
return NULL;
>leaf); newChild->n = t - 1;
newChild->keys[j] = child->keys[j
+ t]; 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];
Himanshu 22009115400
Jain
parent->keys[i] = child->keys[t - 1];
Himanshu 22009115400
Jain
parent->n++;
if (node->leaf) {
>keys[i]) { node->keys[i + 1]
= node->keys[i];
i--;
node->keys[i + 1] =
key; node->n++;
} else {
>keys[i]) i--;
i++;
if (node->children[i]->n == 2 * node->t -
1) { splitChild(node, i, node->children[i]);
>keys[i]) i++;
insertNonFull(node->children[i], key);
key) { if (!root) {
root =
createNode(DEGREE, 1);
root->keys[0] = key;
root->n =
1; return
root;
Himanshu 22009115400
Jain
}
if (root->n == 2 * DEGREE - 1) {
Himanshu 22009115400
Jain
BTreeNode *newRoot =
>children[0] = root;
splitChild(newRoot, 0, root);
1 : 0; insertNonFull(newRoot-
insertNonFull(root, key);
return root;
child->keys[t - 1] = node-
sibling->n; i++)
child->keys[i + t] = sibling-
>keys[i]; if (!child->leaf) {
child->children[i + t] = sibling->children[i];
i++) node->keys[i - 1] =
node->keys[i];
node->children[i - 1] = node-
+ 1;
node-
>n--;
Himanshu 22009115400
Jain
free(sibling
);
Himanshu 22009115400
Jain
node->keys[i - 1] = node-
>keys[i]; node->n--;
key) { if (!root)
return
= 0;
key) idx++;
== key) { if (root->leaf) {
removeFromNode(root, idx);
pred = pred->children[pred->n];
succ = succ->children[0];
root->keys[idx] = succ->keys[0];
} else {
merge(root, idx);
} else if (!root->leaf) {
child->keys[i] = child->keys[i
root->keys[idx - 1] = leftSibling-
child->n++;
root->keys[idx];
root->keys[idx] = rightSibling-
rightSibling->n - 1; i++)
rightSibling->keys[i] = rightSibling-
+ 1]; child->n++;
rightSibling->n--;
} else {
merge(root, idx);
else
}
Himanshu 22009115400
Jain
root->children[idx] = removeKey(root->children[idx], key);
Himanshu 22009115400
Jain
}
return root;
int main() {
BTreeNode *root =
NULL; root =
insert(root, 10);
root = insert(root,
20); root =
= insert(root, 6);
root = insert(root,
12);
traverse(root);
printf("\n");
"); traverse(root);
printf("\n");
return 0;}
OUTPUT-
Himanshu 22009115400
Jain
EXPERIMENT-6
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;
if (smallest != i)
{ int temp =
arr[i];
arr[i] = arr[smallest];
arr[smallest] = temp;
heapify(arr, n, smallest);
}
n) { for (int i = n / 2 - 1; i
>= 0; i--)
heapify(arr, n, i);
arr[0];
Himanshu 22009115400
Jain
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i,
0);
Himanshu 22009115400
Jain
void printArray(int arr[], int
+)
printf("%d ",
arr[i]); printf("\
n");
int main() {
int n = sizeof(arr) /
sizeof(arr[0]); printf("Original
array: ");
printArray(arr,
n);
heapSort(arr,
n);
printArray(arr, n);
return 0;
OUTPU
T-
Himanshu 22009115400
Jain
Binomial Sort
#include
<stdio.h>
#include
<stdlib.h>
typedef struct
int degree;
Himanshu 22009115400
Jain
} BinomialNode;
node->degree = 0;
BinomialNode
*head;
h1 = h1->sibling;
} else {
head =
h2;
h2 = h2->sibling;
BinomialNode *tail =
>degree) { tail->sibling
= h1;
h1 = h1->sibling;
} else {
tail->sibling =
h2; h2 = h2-
>sibling;
}
Himanshu 22009115400
Jain
tail = tail->sibling;
Himanshu 22009115400
Jain
return head;
y->sibling = z->child;
z->child =
y; z-
>degree+
+;
mergeHeaps(h1, h2);
if ((curr->degree != next->degree) ||
curr = next;
} else {
>key) { curr->sibling =
next->sibling;
linkTrees(next, curr);
} else {
if (prev) prev->sibling =
linkTrees(curr, next);
curr = next;
}
Himanshu 22009115400
Jain
next = curr->sibling;
return head;
Himanshu 22009115400
Jain
}
BinomialNode *insert(BinomialNode *heap, int key)
while (curr) {
if (curr->key <
min) { min =
curr->key;
prevMin = prev;
minNode =
curr;
prev = curr;
curr = curr->sibling;
NULL;
while (child) {
>sibling; child->sibling =
newHeap;
child->parent =
NULL; newHeap =
}
Himanshu 22009115400
Jain
}
*heap = unionHeaps(*heap,
newHeap); free(minNode);
return min;
Himanshu 22009115400
Jain
}
void printHeap(BinomialNode
*node) { if (node) {
>key);
printHeap(node-
>child);
printHeap(node-
>sibling);
int main() {
BinomialNode *heap =
NULL; heap =
insert(heap, 5);
"); printHeap(heap);
printf("\n");
extraction: ");
printHeap(heap);
printf("\n");
return
0;}
OUTPUT-
Himanshu 22009115400
Jain
}
Fabonacci
Heap #include
<stdio.h>
#include
<stdlib.h>
Himanshu 22009115400
Jain
#include <math.h>
typedef struct
int
degree;
int mark;
} FibonacciNode;
{ FibonacciNode *min;
int n;
} FibonacciHeap;
FibonacciHeap *createHeap() {
heap->n =
0; return
heap;
node->degree = 0;
node->mark = 0;
node->parent = node->child =
= node;
return node;
*node) { if (!heap->min) {
heap->min = node;
Himanshu 22009115400
Jain
} else {
node->left = heap->min;
Himanshu 22009115400
Jain
node->right = heap->min-
>right; heap->min->right-
>right = node;
heap->n++;
void printHeap(FibonacciNode
*node) { if (node) {
FibonacciNode *start =
node; do {
>key);
printHeap(node-
>child); node =
node->right;
int main() {
FibonacciHeap *heap =
= createNode(10); FibonacciNode
*n2 = createNode(20);
FibonacciNode *n3 =
createNode(5); insertNode(heap,
n1);
insertNode(heap,
n2);
insertNode(heap,
Himanshu 22009115400
Jain
n3);
printf("Fibonacci Heap:
"); printHeap(heap-
>min);
printf("\n");
return 0;
Himanshu 22009115400
Jain
OUTPUT-
Himanshu 22009115400
Jain