Minordegree
Minordegree
Shrishant
//class-2nd btech
//branch -MME
//roll no-823123
//subject-Algorithmics and programming(csm-251)
#include
<stdio.h>
#include
<stdlib.h>
#define MAX_VERTICES 100
// ---------- Quick Sort (Recursive + Iterative) ----------
// Swap function
void swap(int
int
temp = *a;
*a = *b;
*b = temp;
}
int pivot
= arr[low]; int i
= low;
int j = high;
hile (i < j)
swap(&arr[lo
}
// Recursive QuickSort helper
if
pivotIndex - 1);
quickSortRecursiveHelper(arr, pivotIndex +
1, high);
}
}
// Recursive QuickSort
void quickSortRecursive(int
void quickSortIterative(int
int
stack[size];
stack[+
+top] = 0; stack[+
+top] = size - 1;
while
(top >= 0) {
int high
= stack[top--]; int
low = stack[top--];
int p = partition(arr, low, high);
if
(p - 1 > low)
stack
[++top] = low;
stack[++top] =
p - 1;
}
if (p
+ 1 < high) {
stack[
++top] = p + 1;
stack[++top] =
high;
}
void printArray(int
printf("
%d ", arr[i]);
printf("\n");
}
// ---------- AVL Tree ----------
typedef
struct Node {
int key;
struct
Node *left;
struct Node
*right; int
height;
} Node;
int
height(Node *N)
{
return (N == NULL) ? 0 : N->height;
int
max(int a, int b)
{
return (a > b) ? a : b;
}
Node
*newNode(int key) {
*)malloc(sizeof(Node)); node->key =
key;
node->left = node-
>height = 1;
return node;
}
Node
*rightRotate(Node *y) {
Node *x =
= x->right; x-
>right = y;
y->left = T2;
y->height = max(height(y->left),
height(y->right)) + 1; x->height =
max(height(x->left), height(x->right)) + 1;
return x;
}
Node
*leftRotate(Node *x) {
Node *y
= x->right;
Node *T2 = y-
>left; y->left =
x;
x->right = T2;
x->height = max(height(x->left),
height(x->right)) + 1; y->height =
max(height(y->left), height(y->right)) + 1;
return y;
}
int
getBalance(Node *N)
{
return (N == NULL) ? 0 : height(N->left) - height(N->right);
}
Node *insert(Node
node->left =
node->right =
node->height = 1 + max(height(node->left),
node->left->key) {
node->left =
leftRotate(node->left); return
rightRotate(node);
}
node->right->key) {
node->right =
rightRotate(node->right); return
leftRotate(node);
}
return node;
Node
*minValueNode(Node *node) {
Node *current = node;
while (current->left != NULL)
current =
current->left; return
current;
}
Node *deleteNode(Node
root->left =
root->right =
>right == NULL)) {
emp =
root; root
= NULL;
}
else
*root = *temp;
free(temp);
}
else
{
Node *temp =
minValueNode(root->right); root-
>key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),
getBalance(root->left) < 0) {
root->left =
leftRotate(root->left); return
rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
getBalance(root->right) > 0) {
root->right =
rightRotate(root->right); return
leftRotate(root);
}
return root;
void
preOrder(Node *root) {
if (root
!= NULL) {
printf("%d
", root->key);
preOrder(root-
>left);
preOrder(root-
>right);
}
}
// ---------- BFS Traversal ----------
typed
ef struct {
int
items[MAX_VERTICES];
int front;
nt rear;
Queue;
void
initQueue(Queue *q) {
q->front = -1;
q->rear = -1;
int
isEmpty(Queue *q)
{
return q->front == -1;
}
void enqueue(Queue
q->front =
q->rear = 0; else
q->rear++;
q->items[q->rear] = value;
}
int
dequeue(Queue *q)
{
if (isEmpty(q))
return -1;
int value = q->items[q->front];
if (q->front == q->rear)
q->front =
q-
>front++;
return}
void addEdge(int
while
(graph[u][i] !=
-1) i++;
graph[u][i] = v;
i = 0;
while
(graph[v][i] !=
-1) i++;
graph[v][i] = u;
}
int
visited[MAX_VERTICES] =
{0}; Queue q;
initQueue(&q);
int index = 0;
visite
d[0] = 1;
enqueue(&q,
0);
while (!
isEmpty(&q)) {
int currentNode =
dequeue(&q);
output[index++] =
currentNode;
for (int i = 0;
int neighbor =
graph[currentNode][i]; if (!
visited[neighbor])
{
visited
[neighbor] = 1;
enqueue(&q,
}
void printBFSResult(int
printf("%d
", result[i]);
printf("\n");
}
// ---------- Main Function ----------
in
t main()
{
// QuickSort Example
int arr1[] = {4, 6, 2, 5, 7, 9, 1, 3};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
printf("Before Recursive
size1);
quickSortRecursive(arr1, size1);
printf("After Recursive
size1);
int arr2[] = {10, 7, 8, 9, 1, 5};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
printf("\nBefore Iterative
size2);
quickSortIterative(arr2, size2);
printf("After Iterative
size2);
// AVL Tree
Example Node
= insert(root, 10);
root = insert(root,
20); root =
insert(root, 30);
root = insert(root,
40); root =
insert(root, 50);
root = insert(root,
25);
printf("\nPreorder traversal of
MAX_VERTICES; i++) {
graph[i] = (int
addEdge(g
raph, 0, 1);
addEdge(graph, 1,
2);
addEdge(graph, 1,
3);
addEdge(graph, 0,
4);
int bfsResult[MAX_VERTICES];
bfsTraversal(totalVertices, graph,
bfsResult);
printf("\nBFS Traversal
Result:\n");
printBFSResult(bfsResult,
totalVertices);
for (int i = 0; i < MAX_VERTICES; i++)
free(graph[i]);
return 0;
}