0% found this document useful (0 votes)
6 views16 pages

Minordegree

The document contains a C program that implements various algorithms including Quick Sort (both recursive and iterative), AVL Tree operations (insertion and deletion), and Breadth-First Search (BFS) traversal for graph data structures. It defines necessary functions for sorting, tree manipulation, and queue management, along with examples demonstrating their usage. The main function showcases the sorting of arrays, AVL tree operations, and BFS traversal results.

Uploaded by

823123
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)
6 views16 pages

Minordegree

The document contains a C program that implements various algorithms including Quick Sort (both recursive and iterative), AVL Tree operations (insertion and deletion), and Breadth-First Search (BFS) traversal for graph data structures. It defines necessary functions for sorting, tree manipulation, and queue management, along with examples demonstrating their usage. The main function showcases the sorting of arrays, AVL tree operations, and BFS traversal results.

Uploaded by

823123
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/ 16

//Name-

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

*a, int *b) {

int

temp = *a;

*a = *b;
*b = temp;
}

// Partition function (common for both recursive

and iterative) int partition(int arr[], int low, int high)


{

int pivot

= arr[low]; int i

= low;
int j = high;

hile (i < j)

while (arr[i] <=

pivot && i <= high - 1) i++;


while (arr[j] > pivot && j >= low + 1)
j--;
if (i < j)
swap(&arr[i], &arr[j]);
}

swap(&arr[lo

w], &arr[j]); return j;

}
// Recursive QuickSort helper

void quickSortRecursiveHelper(int arr[],

int low, int high) {

if

(low < high)

int pivotIndex = partition(arr, low,

high); quickSortRecursiveHelper(arr, low,

pivotIndex - 1);

quickSortRecursiveHelper(arr, pivotIndex +

1, high);
}
}
// Recursive QuickSort
void quickSortRecursive(int

arr[], int size) {


quickSortRecursiveHelper(arr, 0, size - 1);
}
// Iterative QuickSort

void quickSortIterative(int

arr[], int size) {

int

stack[size];

int top = -1;

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

arr[], int size) {


for (int i = 0; i < size; i++)

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) {

Node *node = (Node

*)malloc(sizeof(Node)); node->key =

key;

node->left = node-

>right = NULL; node-

>height = 1;
return node;
}

Node

*rightRotate(Node *y) {

Node *x =

y->left; Node *T2

= 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, int key) {


if (node == NULL)
return newNode(key);
if (key < node->key)

node->left =

insert(node->left, key); else if

(key > node->key)

node->right =

insert(node->right, key); else


return node;

node->height = 1 + max(height(node->left),

height(node->right)); int balance = getBalance(node);


if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);

if (balance > 1 && key >

node->left->key) {

node->left =

leftRotate(node->left); return

rightRotate(node);
}

if (balance < -1 && key <

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, int key) {


if (root == NULL)
return root;
if (key < root->key)

root->left =

deleteNode(root->left, key); else if

(key > root->key)

root->right =

deleteNode(root->right, key); else


{

if ((root->left == NULL) || (root-

>right == NULL)) {

Node *temp = root->left ?

root->left : root->right; if (temp == 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),

height(root->right)); int balance = getBalance(root);


if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);

if (balance > 1 &&

getBalance(root->left) < 0) {

root->left =

leftRotate(root->left); return

rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);

if (balance < -1 &&

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, int value) {


if (q->rear == MAX_VERTICES - 1)
return;
if (isEmpty(q))

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->rear = -1; else

q-

>front++;

return}

void addEdge(int

*graph[], int u, int v) {


int i = 0;

while

(graph[u][i] !=

-1) i++;
graph[u][i] = v;
i = 0;

while

(graph[v][i] !=

-1) i++;
graph[v][i] = u;
}

void bfsTraversal(int totalNodes, int

*graph[], int output[]) {

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;

graph[currentNode][i] != -1; i++) {

int neighbor =

graph[currentNode][i]; if (!

visited[neighbor])
{

visited

[neighbor] = 1;

enqueue(&q,
}

void printBFSResult(int

result[], int size) {


for (int i = 0; i < size; i++)

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

Quick Sort:\n"); printArray(arr1,

size1);
quickSortRecursive(arr1, size1);

printf("After Recursive

Quick Sort:\n"); printArray(arr1,

size1);
int arr2[] = {10, 7, 8, 9, 1, 5};
int size2 = sizeof(arr2) / sizeof(arr2[0]);

printf("\nBefore Iterative

Quick Sort:\n"); printArray(arr2,

size2);
quickSortIterative(arr2, size2);

printf("After Iterative

Quick Sort:\n"); printArray(arr2,

size2);
// AVL Tree

Example Node

*root = NULL; root

= 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

AVL tree:\n"); preOrder(root);


printf("\n");
root = deleteNode(root, 40);

printf("\nPreorder traversal after

deleting node 40:\n"); preOrder(root);


printf("\n");
// BFS Example
int totalVertices = 6;
int *graph[MAX_VERTICES];

for (int i = 0; i <

MAX_VERTICES; i++) {

graph[i] = (int

*)malloc(MAX_VERTICES * sizeof(int)); for (int j

= 0; j < MAX_VERTICES; j++)


graph[i][j] = -1;
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy