Dsa Ac
Dsa Ac
Dsa Ac
PRACTICAL FILE
OF
CSP-003
Session: 2023-24
COURSE: B-Tech
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 23;
int result = linear_search(arr, size, target);
if (result != -1)
printf("Element %d found at index %d\n", target, result);
else
printf("Element %d not found in the array\n", target);
return 0;
}
Output:
2. BINARY SEARCH
#include <stdio.h>
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 23;
int result = binary_search(arr, size, target);
if (result != -1)
printf("Element %d found at index %d\n", target, result);
else
printf("Element %d not found in the array\n", target);
return 0;
}
Output :
3. BUBBLE SORT
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
}
Output :
4. QUICK SORT
#include <stdio.h>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("\n");
quickSort(arr, 0, n - 1);
return 0;}
Output :
5. MERGE SORT
#include <stdio.h>
// Merge two subarrays of arr[]
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// Create temporary arrays
int L[n1], R[n2];
// Copy data to temporary arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temporary arrays back into arr[l..r]
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
// Copy the remaining elements of L[], if there are any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
return 0;
}
Output :
6. INFIX TO POSTFIX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // Include this header for isalnum function
#define MAX_SIZE 100
// Structure to represent a stack
struct Stack {
int top;
unsigned capacity;
char* array;
};
// Function to create a stack
struct Stack* createStack(unsigned capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to push an element to the stack
void push(struct Stack* stack, char item) {
stack->array[++stack->top] = item;
}
// Function to pop an element from the stack
char pop(struct Stack* stack) {
if (!isEmpty(stack))
return stack->array[stack->top--];
return '$'; // Placeholder for empty stack
}
// Function to get the precedence of an operator
int getPrecedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;}
// Function to convert infix to postfix
void infixToPostfix(char* infix, char* postfix) {
struct Stack* stack = createStack(strlen(infix));
int i, j;
i = j = 0;
while (infix[i] != '\0') {
if (isalnum(infix[i])) {
postfix[j++] = infix[i++];
} else if (infix[i] == '(') {
push(stack, infix[i++]);
} else if (infix[i] == ')') {
while (!isEmpty(stack) && stack->array[stack->top] != '(') {
postfix[j++] = pop(stack);
}
if (!isEmpty(stack) && stack->array[stack->top] != '(') {
printf("Invalid expression\n");
return;
} else {
pop(stack);
}
i++;
} else { // Operator
while (!isEmpty(stack) && getPrecedence(infix[i]) <=
getPrecedence(stack->array[stack->top])) {
postfix[j++] = pop(stack);
}
push(stack, infix[i++]);
}
}
postfix[j] = '\0';
}
// Example usage
int main() {
char infix[MAX_SIZE];
char postfix[MAX_SIZE];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
OUTPUT :
7. IMPLEMENTATION OF STACK USING ARRAY
#include <stdio.h>
#include <stdlib.h>
// Example usage
int main() {
struct Stack stack;
initializeStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
return 0;
}
Output :
8. IMPLEMENTATION OF STACK USING LINKED LIST
#include <stdio.h>
#include <stdlib.h>
// Stack structure
struct Stack {
struct Node* top;
};
// Example usage
int main() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
push(stack, 10);
push(stack, 20);
push(stack, 30);
display(stack);
display(stack);
Output :
9. OPERATION ON BINARY TREE
#include <stdio.h>
#include <stdlib.h>
#define DEGREE 2
int main() {
int btree[100] = {0}; // Assuming a maximum of 100 elements in the tree
int n = 0; // Number of elements in the tree
int key = 5;
btree_delete(btree, &n, key);
return 0;
}
tree[i + 1] = key;
(*n)++;
Output :
10. WRITE PROGRAMS FOR IMPLEMENTING BFS AND DFS
// Graph structure
struct Graph {
int numVertices;
struct Node* adjList[MAX_VERTICES];
};
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct Node* newNode = createNode(dest);
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
visited[startVertex] = true;
enqueue(queue, startVertex);
while (!isEmpty(queue)) {
int currentVertex = dequeue(queue);
printf("%d ", currentVertex);
printf("\n");
}
return 0;
}
Output: