0% found this document useful (0 votes)
17 views28 pages

DS Lab Record

The document provides a series of C programs demonstrating various data structures and algorithms, including linear search, binary search, selection sort, bubble sort, merge sort, factorial calculation using recursion, stack operations, tower of Hanoi, queue implementation, linked list operations, Fibonacci series generation, and GCD calculation using recursion. Each program includes code snippets, prompts for user input, and basic output statements. Additionally, sections for time/space complexity analysis are indicated but not filled in.

Uploaded by

wbwwfmwx9y
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)
17 views28 pages

DS Lab Record

The document provides a series of C programs demonstrating various data structures and algorithms, including linear search, binary search, selection sort, bubble sort, merge sort, factorial calculation using recursion, stack operations, tower of Hanoi, queue implementation, linked list operations, Fibonacci series generation, and GCD calculation using recursion. Each program includes code snippets, prompts for user input, and basic output statements. Additionally, sections for time/space complexity analysis are indicated but not filled in.

Uploaded by

wbwwfmwx9y
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/ 28

Data Structures and Algorithms

1. Write a C program to demonstrate linear search.

#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

2. Write a program to demonstrate binary search.

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

return -1; // Target not found


}
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 sorted array elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\n Enter the key to search:");
scanf("%d",&key);
flag = binary_search(arr,0, n, key);
if(flag>=0)
printf("Element is present at index %d",flag);
else
printf("Element not Found");
return 0;
}

Page 3
Data Structures and Algorithms

Output:-

Time/Space Complexity:-

Page 4
Data Structures and Algorithms

3. Write a C program to demonstrate selection sort.

#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

4. Write a C program to demonstrate bubble sort.


#include <stdio.h>
// Function to perform bubble sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = 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, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}

Page 7
Data Structures and Algorithms

Output:-

Time/Space Complexity:-

Page 8
Data Structures and Algorithms

5. Write a C program to demonstrate merge sort.

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

int LeftArray[n1], RightArray[n2];

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


LeftArray[i] = a[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];

i = 0;
j = 0;
k = beg;

while (i < n1 && j < n2)


{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}

Page 9
Data Structures and Algorithms

while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}

void mergeSort(int a[], int beg, int end)


{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}

void printArray(int a[], int n)


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

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

6. Write a C program to find factorial of a number using recursion.

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

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output:-

Page 12
Data Structures and Algorithms

7. Write a C program to perform stack operations.

#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

8. Write a C program to perform tower of Hanoi.

#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

9. Write a C program to implement queue using array.


#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main
Menu*****************************\n");

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

10. Write a C Program to implement Linked List.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void begin_delete();
void display();
void main ()
{
int choice;
for(;;)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Delete from Beginning\n \n3.Show\n4.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1: beginsert();
break;
case 2: begin_delete();
break;
case 3: display();
break;
case 4:exit(0);
break;
default: printf("Please enter valid choice..");
}
}
}

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

11. Write a C program to perform Fibonacci series using recursion.


#include <stdio.h>
void printFib(int n)
{

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

12. Write a C program to find GCD of two numbers using recursion.

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

int gcd(int n1, int n2) {


if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
Output:-

Page 28

You might also like