Dsa Da1 FINAL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Rakshit Bansal

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

while i > 0 and item < A[i]

A[i + 1] = A[i]

i=i-1

A[i + 1] = item

b)Code:

#include <stdio.h>

void insertionSort(int A[], int n) {


for (int j = 1; j < n; j++) {
int item = A[j];
int i = j - 1;
while (i >= 0 && item < A[i]) {
A[i + 1] = A[i];
i--;
}
A[i + 1] = item;
}
}

void printArray(int A[], int n) {


for (int i = 0; i < n; i++) {
printf("%d, ", A[i]);
}
printf("\n");
}

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

c)ip and o/p:

1.(ii)Selection Sort:

a)algorithm:

SelectionSort(A[], n)

for i = 0 to n - 2

minIndex = i

for j = i + 1 to n - 1

if A[j] < A[minIndex]

minIndex = j

swap A[i] and A[minIndex]

b)code:

#include <stdio.h>
Rakshit Bansal
22BCE0431

void selectionSort(int A[], int n) {


for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (A[j] < A[minIndex]) {
minIndex = j;
}
}
int temp = A[i];
A[i] = A[minIndex];
A[minIndex] = temp;
}
}

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

c)i/p and o/p:

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

if A[j] > A[j + 1]

swap A[j] and A[j + 1]

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

C) i/p and o/p:

(iv)Merge sort

a)algorithm:

MergeSort(A[], left, right)

if left < right

mid = left + (right - left) / 2

MergeSort(A, left, mid)

MergeSort(A, mid + 1, right)

Merge(A, left, mid, right)

Merge(A[], left, mid, right)

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

while i < n1 and j < n2

if L[i] <= R[j]

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>

void merge(int A[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
Rakshit Bansal
22BCE0431

int L[n1], R[n2];


for (int i = 0; i < n1; i++)
L[i] = A[left + i];
for (int j = 0; j < n2; j++)
R[j] = A[mid + 1 + j];

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

while (i < n1)


A[k++] = L[i++];
while (j < n2)
A[k++] = R[j++];
}

void mergeSort(int A[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(A, left, mid);
mergeSort(A, mid + 1, right);
merge(A, left, mid, right);
}
}

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

c)i/p and o/p:

(v)Quick Sort

a)algorithm:

QuickSort(A[], low, high)

if low < high

pivot = Partition(A, low, high)

QuickSort(A, low, pivot - 1)

QuickSort(A, pivot + 1, high)

Partition(A[], low, high)

pivot = A[high]

i = low - 1

for j = low to high - 1

if A[j] < pivot

i++

swap A[i] and A[j]

swap A[i + 1] and A[high]

return i + 1
Rakshit Bansal
22BCE0431

b)code:

#include <stdio.h>

int partition(int A[], int low, int high) {


int pivot = A[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (A[j] < pivot) {
i++;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
int temp = A[i + 1];
A[i + 1] = A[high];
A[high] = temp;
return i + 1;
}

void quickSort(int A[], int low, int high) {


if (low < high) {
int pi = partition(A, low, high);
quickSort(A, low, pi - 1);
quickSort(A, pi + 1, high);
}
}

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

c)i/p and o/p:

Searching:

i)Linear Search

a)code:

#include <stdio.h>

int linearSearch(int array[], int n, int searchElement) {


for (int i = 0; i < n; i++) {
if (array[i] == searchElement) {
return i + 1; // Return the position (1-indexed) of the element found
}
}
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 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

printf("Element not found.\n");


}
return 0;
}

c)i/p and o/p:

ii)Binary search

a)algorithm:

BinarySearch(array[], x, low, high)

while low <= high

mid = (low + high) / 2

if x == array[mid]

return mid + 1

else if x < array[mid]

high = mid - 1

else

low = mid + 1

return -1

b)code:

#include <stdio.h>

int binarySearch(int array[], int n, int x) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
Rakshit Bansal
22BCE0431

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

c)i/p and o/p

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)

if top is equal to SIZE - 1

Display "Stack is Full!!! Insertion is not possible!!!"

else

Increment top by 1

Assign value to stack[top]

Display "Insertion success!!!"

ii)pop

Pop()

if top is equal to -1

Display "Stack is Empty!!! Deletion is not possible!!!"

else

Display "Deleted: " + stack[top]

Decrement top by 1

iii)display

Display()

if top is equal to -1

Display "Stack is Empty!!!"

else

Display "Stack elements are:"

for i from top to 0 (inclusive)


Rakshit Bansal
22BCE0431

Display stack[i]

b)Code:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10 // Size of the stack

int stack[SIZE];
int top = -1;

void push(int value) {


if (top == SIZE - 1) {
printf("Stack is Full!!! Insertion is not possible!!!\n");
} else {
top++;
stack[top] = value;
printf("Insertion success!!!\n");
}
}

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

int value, choice;


while (1) {
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong selection!!! Try again!!!\n");
}
}
return 0;
}

c)i/p and o/p:


Rakshit Bansal
22BCE0431
Rakshit Bansal
22BCE0431

ii)conversion of infix to prefix and postfix

a)algorithm:

infix to prefix:

InfixToPrefix(infix[], prefix[])

Initialize an empty stack

Reverse the infix expression


Rakshit Bansal
22BCE0431

for each symbol in reversed infix

if symbol is an operand

Append it to the prefix expression

else if symbol is a closing parenthesis ')'

Push it onto the stack

else if symbol is an opening parenthesis '('

Pop and append symbols from stack to prefix until ')' is encountered

else if symbol is an operator

While stack is not empty and precedence of top of stack >= precedence of symbol

Pop and append symbol from stack to prefix

Push symbol onto stack

Pop and append remaining symbols from stack to prefix

Reverse the prefix expression to get the final result

Infix to postfix:

InfixToPostfix(infix[], postfix[])

Initialize an empty stack

for each symbol in infix

if symbol is an operand

Append it to the postfix expression

else if symbol is an opening parenthesis '('

Push it onto the stack

else if symbol is a closing parenthesis ')'

Pop and append symbols from stack to postfix until '(' is encountered
Rakshit Bansal
22BCE0431

Pop '(' from stack

else if symbol is an operator

While stack is not empty and precedence of top of stack >= precedence of symbol

Pop and append symbol from stack to postfix

Push symbol onto stack

Pop and append remaining symbols from stack to postfix

b)code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define SIZE 50

char stack[SIZE];
int top = -1;

void push(char item) {


if (top >= SIZE - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = item;
}
}

char pop() {
if (top < 0) {
printf("Stack Underflow\n");
return '\0';
} else {
return stack[top--];
}
}

int isOperator(char symbol) {


Rakshit Bansal
22BCE0431

return (symbol == '+' || symbol == '-' || symbol == '*' || symbol ==


'/');
}

int precedence(char symbol) {


if (symbol == '*' || symbol == '/') return 2;
if (symbol == '+' || symbol == '-') return 1;
return 0;
}

void infixToPrefix(char infix[], char prefix[]) {


int i = strlen(infix) - 1, j = 0;
char symbol, temp;
while (i >= 0) {
symbol = infix[i--];
if (isalnum(symbol)) {
prefix[j++] = symbol;
} else if (symbol == ')') {
push(symbol);
} else if (symbol == '(') {
while ((temp = pop()) != ')') {
prefix[j++] = temp;
}
} else if (isOperator(symbol)) {
while (top >= 0 && precedence(stack[top]) >=
precedence(symbol)) {
prefix[j++] = pop();
}
push(symbol);
}
}
while (top >= 0) {
prefix[j++] = pop();
}
prefix[j] = '\0';
strrev(prefix);
}

void infixToPostfix(char infix[], char postfix[]) {


int i = 0, j = 0;
char symbol;

while ((symbol = infix[i++]) != '\0') {


Rakshit Bansal
22BCE0431

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

while (top >= 0) {


postfix[j++] = pop();
}

postfix[j] = '\0';
}

int main() {
char infix[SIZE], prefix[SIZE], postfix[SIZE];

printf("Enter an infix expression: ");


gets(infix);

infixToPrefix(infix, prefix);
infixToPostfix(infix, postfix);

printf("Prefix expression: %s\n", prefix);


printf("Postfix expression: %s\n", postfix);

return 0;
}

c)i/p and o/p:


Rakshit Bansal
22BCE0431

You might also like