DS Lab Record
DS Lab Record
#include <stdio.h>
int search(int arr[100], int N, int x)
{
for (int i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}
int main(void)
{
int arr[100] ,n,key,flag=-1,i;
printf("\n Enter the size of array");
scanf("%d",&n);
printf("\n Enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\n Enter the key to search:");
scanf("%d",&key);
flag = search(arr, n, key);
if(flag>=0)
printf("Element is present at index %d",flag);
else
printf("Element not Found");
return 0;
}
Page 1
Data Structures and Algorithms
Output:-
Time/Space Complexity:-
Page 2
Data Structures and Algorithms
#include <stdio.h>
int binary_search(int arr[], int first, int last, int key)
{
while (first <= last) {
int mid = first + (last - first) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
first = mid + 1;
} else {
last = mid - 1;
}
}
Page 3
Data Structures and Algorithms
Output:-
Time/Space Complexity:-
Page 4
Data Structures and Algorithms
#include <stdio.h>
// Function to perform selection sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
// Function to print the array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Page 5
Data Structures and Algorithms
Output:-
Time/Space Complexity:-
Page 6
Data Structures and Algorithms
Page 7
Data Structures and Algorithms
Output:-
Time/Space Complexity:-
Page 8
Data Structures and Algorithms
#include <stdio.h>
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
i = 0;
j = 0;
k = beg;
Page 9
Data Structures and Algorithms
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After sorting array elements are - \n");
printArray(a, n);
return 0;
}
Page 10
Data Structures and Algorithms
Output:-
Time/Space Complexity:-
Page 11
Data Structures and Algorithms
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
Output:-
Page 12
Data Structures and Algorithms
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int top = -1, inp_array[SIZE];
void push();
void pop();
void show();
int main()
{
int choice;
while (1)
{
printf("\nPerform operations on the stack:");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice!!");
}
}
}
Page 13
Data Structures and Algorithms
void push()
{
int x;
if (top == SIZE - 1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter the element to be added onto the stack: ");
scanf("%d", &x);
top = top + 1;
inp_array[top] = x;
}
}
void pop()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", inp_array[top]);
top = top - 1;
}
}
void show()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
Page 14
Data Structures and Algorithms
{
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; --i)
printf("%d\n", inp_array[i]);
}
}
Page 15
Data Structures and Algorithms
Output:-
Page 16
Data Structures and Algorithms
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main()
{
int n =4;
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
Page 17
Data Structures and Algorithms
Output:-
Page 18
Data Structures and Algorithms
printf("\n=========================================================
========\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
Page 19
Data Structures and Algorithms
}
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
Page 20
Data Structures and Algorithms
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}
Page 21
Data Structures and Algorithms
Output:-
Page 22
Data Structures and Algorithms
Page 23
Data Structures and Algorithms
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
Page 24
Data Structures and Algorithms
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Page 25
Data Structures and Algorithms
Output:-
Page 26
Data Structures and Algorithms
if (n < 1) {
printf("Invalid Number of terms\n");
return;
}
int prev1 = 1;
int prev2 = 0;
for (int i = 1; i <= n; i++) {
if (i > 2) {
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
printf("%d ", curr);
}
else if (i == 1)
printf("%d ", prev2);
else if (i == 2)
printf("%d ", prev1);
}
}
int main() {
int n = 9;
printFib(n);
return 0;
}
Output:-
Page 27
Data Structures and Algorithms
#include <stdio.h>
int gcd(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1, n2));
return 0;
}
Page 28