Professional Documents
Culture Documents
Dsa Da1 FINAL
Dsa Da1 FINAL
Dsa Da1 FINAL
22BCE0431
DSA Lab
Digital Assignment 1
L79+80+L89+90
Name: Rakshit Bansal
Reg: 22BCE0431
Submitted to: Prof. Iyappan P
Rakshit Bansal
22BCE0431
1.(i)Insertion Sort:
a)algorithm:
InsertionSort(A[], n)
for j = 2 to n
item = A[j]
i=j-1
A[i + 1] = A[i]
i=i-1
A[i + 1] = item
b)Code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
Rakshit Bansal
22BCE0431
scanf("%d", &n);
int A[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
insertionSort(A, n);
printf("Sorted array: ");
printArray(A, n);
return 0;
}
1.(ii)Selection Sort:
a)algorithm:
SelectionSort(A[], n)
for i = 0 to n - 2
minIndex = i
for j = i + 1 to n - 1
minIndex = j
b)code:
#include <stdio.h>
Rakshit Bansal
22BCE0431
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int A[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
selectionSort(A, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d, ", A[i]);
}
printf("\n");
return 0;
}
1.(iii)Bubble Sort:
Rakshit Bansal
22BCE0431
a)algorithm:
BubbleSort(A[], n)
for i = 1 to n do
for j = 1 to n – i do
b)code :
#include<stdio.h>
void bubbleSort(int A[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (A[j] > A[j + 1]) {
int temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
void printarr(int A[],int n){
for (int i=0;i<n;i++){
printf("%d, ",A[i]);
}
}
int main(){
int i,n;
printf("Enter the no. of elements: ");
scanf("%d",&n);
int A[n];
printf("\nEnter the elements: ");
for(i=0;i<n;i++){
scanf("%d",&A[i]);
}
bubbleSort(A,n);
printarr(A,n);
return 0;
}
Rakshit Bansal
22BCE0431
(iv)Merge sort
a)algorithm:
n1 = mid - left + 1
n2 = right - mid
L[n1], R[n2]
for i = 0 to n1 - 1
L[i] = A[left + i]
Rakshit Bansal
22BCE0431
for j = 0 to n2 - 1
R[j] = A[mid + 1 + j]
i = 0, j = 0, k = left
A[k] = L[i]
i++
else
A[k] = R[j]
j++
k++
while i < n1
A[k] = L[i]
i++
k++
while j < n2
A[k] = R[j]
j++
k++
b)code:
#include <stdio.h>
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
A[k++] = L[i++];
else
A[k++] = R[j++];
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int A[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
mergeSort(A, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d, ", A[i]);
}
printf("\n");
Rakshit Bansal
22BCE0431
return 0;
}
(v)Quick Sort
a)algorithm:
pivot = A[high]
i = low - 1
i++
return i + 1
Rakshit Bansal
22BCE0431
b)code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int A[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
quickSort(A, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d, ", A[i]);
}
printf("\n");
return 0;
Rakshit Bansal
22BCE0431
Searching:
i)Linear Search
a)code:
#include <stdio.h>
int main() {
int n, searchElement;
printf("Enter the number of elements: ");
scanf("%d", &n);
int array[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
printf("Enter the element to search: ");
scanf("%d", &searchElement);
int position = linearSearch(array, n, searchElement);
if (position != -1) {
printf("Element found at position: %d\n", position);
} else {
Rakshit Bansal
22BCE0431
ii)Binary search
a)algorithm:
if x == array[mid]
return mid + 1
high = mid - 1
else
low = mid + 1
return -1
b)code:
#include <stdio.h>
if (x == array[mid]) {
return mid + 1; // Return the position (1-indexed) of the element
found
} else if (x < array[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1; // Return -1 if the element is not found
}
int main() {
int n, searchElement;
printf("Enter the number of elements: ");
scanf("%d", &n);
int array[n];
printf("Enter the sorted elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
printf("Enter the element to search: ");
scanf("%d", &searchElement);
int position = binarySearch(array, n, searchElement);
if (position != -1) {
printf("Element found at position: %d\n", position);
} else {
printf("Element not found.\n");
}
return 0;
}
Q.2 Illustrate Stack operation and its applications, Conversion of infix to prefix and postfix.
Rakshit Bansal
22BCE0431
Stack
a)algorithm:
i)push
Push(value)
else
Increment top by 1
ii)pop
Pop()
if top is equal to -1
else
Decrement top by 1
iii)display
Display()
if top is equal to -1
else
Display stack[i]
b)Code:
#include <stdio.h>
#include <stdlib.h>
int stack[SIZE];
int top = -1;
void pop() {
if (top == -1) {
printf("Stack is Empty!!! Deletion is not possible!!!\n");
} else {
printf("Deleted: %d\n", stack[top]);
top--;
}
}
void display() {
if (top == -1) {
printf("Stack is Empty!!!\n");
} else {
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
int main() {
Rakshit Bansal
22BCE0431
a)algorithm:
infix to prefix:
InfixToPrefix(infix[], prefix[])
if symbol is an operand
Pop and append symbols from stack to prefix until ')' is encountered
While stack is not empty and precedence of top of stack >= precedence of symbol
Infix to postfix:
InfixToPostfix(infix[], postfix[])
if symbol is an operand
Pop and append symbols from stack to postfix until '(' is encountered
Rakshit Bansal
22BCE0431
While stack is not empty and precedence of top of stack >= precedence of symbol
b)code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 50
char stack[SIZE];
int top = -1;
char pop() {
if (top < 0) {
printf("Stack Underflow\n");
return '\0';
} else {
return stack[top--];
}
}
if (isalnum(symbol)) {
postfix[j++] = symbol;
} else if (symbol == '(') {
push(symbol);
} else if (symbol == ')') {
while (stack[top] != '(') {
postfix[j++] = pop();
}
pop(); // Pop '('
} else if (isOperator(symbol)) {
while (top >= 0 && precedence(stack[top]) >=
precedence(symbol)) {
postfix[j++] = pop();
}
push(symbol);
}
}
postfix[j] = '\0';
}
int main() {
char infix[SIZE], prefix[SIZE], postfix[SIZE];
infixToPrefix(infix, prefix);
infixToPostfix(infix, postfix);
return 0;
}