0% found this document useful (0 votes)
14 views21 pages

Dsa Lab

Uploaded by

raj8602753523
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views21 pages

Dsa Lab

Uploaded by

raj8602753523
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

Page |1

NAME: ________________________
ROLL NO:______________________
COURSE:________ SEM:__________
SUBJECT:______________________

INDEX
Sr NAME OF EXPERIMENTS PAGE
1 To perform insertion and deletion operations in an 2
array
2 To perform linear search in an array 3

3 To perform binary search in an array 4

4 To perform push and pop operations in a stack data 5-6


structure
5 To perform enqueue and dequeue operations in a 7-8
queue data structure
6 To perform insertion and deletion operation at the 9-10
tail of a linked list
7 To perform insertion and deletion operation at the 11-12
front of a linked list
8 To perform insertion and deletion operation at mth 13-14
and nth position of a linked list
9 To implement bubble sort algorithm 15

10 To implement insertion sort algorithm 16

11 To implement selection sort algorithm 17

12 To implement quick sort algorithm 18

13 To implement heap sort algorithm 19

14 To implement merge sort algorithm 20-21


Page |2

PRACTICAL NO. 1
AIM: To perform deletion and insertion operations in an Array.
#include <stdio.h>
void display(int arr[], int size){
printf("Elements of array: ");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int insertion(int arr[], int size, int element, int capacity, int index){
if (size >= capacity){
printf("insertion not posible");
}
for (int i = size - 1; i >= index; i--){
arr[i + 1] = arr[i];
}
arr[index] = element;
}
int deletion (int arr[], int size, int m ){
for( int i = m; i < (size -1); i++){
arr[i]=arr[i+1];
}
return 0;
}
int main(){
int arr[100] = {7, 8, 12, 27, 88},size = 5,element = 0,index = 2,capacity =
100;
display(arr,size);
insertion(arr, size, element, capacity, index);
size++;
printf("After Insertion, ");
display(arr, size);
index = 4;
deletion(arr,size,index);
size--;
printf("After Deletion, ");
display(arr, size);
return 0;
}

Output:
Elements of array: 7 8 12 27 88
After Insertion, Elements of array: 7 8 0 12 27 88
After Deletion, Elements of array: 7 8 0 12 88
Page |3

PRACTICAL NO. 2
AIM: To perform linear search in an Array.
#include <stdio.h>
int main(){
int n, i, e;
int a[] = { 1, 3, 2, 45, 11, 25, 7, 9};
n = 8;
e = 45;
for(i = 0; i < n; i++){
if (e == a[i]){
printf("Element found at index i = %d.",i);
break;
}
}
if(i >= n){
printf("Element not found!!");
}
return 0;
}

Output:
Element found at index i = 3
Page |4

PRACTICAL NO. 3
AIM: To perform binary search in an Array.
#include <stdio.h>
int binarysearch(int arr[], int size, int element){
int first, last, mid;
first = 0;
last = size - 1;
while (first <= last){
mid = (first + last) / 2;
if (arr[mid] == element){
return mid;
}
else if (arr[mid] < element){
first = mid + 1;
}
else if (arr[mid]> element){
last = mid - 1;
}
}
return -1;
}
int main(){
int arr[] = {7,8,11,23,34,45,56};
int size = sizeof(arr)/sizeof(int);
int element = 8;
printf("The index is %d", binarysearch(arr, size, element));
return 0;
}

Output:
The index is 1
Page |5

PRACTICAL NO. 4
AIM: To perform Push and Pop operations in Stack data structure.
#include <stdio.h>
#include <stdlib.h>
#define size 5
int top = -1;
int stack[size];
void push(int x){
if (top == (size - 1))
printf("Stack Overflow\n");
else{
top++;
stack[top] = x;
}
}
int pop(){
if (top == -1){
printf("Stack Underflow\n");
exit(0);
}
else{
int x = 0;
x = stack[top];
stack[top] == 0;
top--;
return x;
}
}
void display()
{
int n = top;
if (n == -1){
printf("Stack underflow\n");
exit(0);
}
printf("Elements of stack are: ");
while (n != -1){
if (n==0){
printf("%d ",stack[n]);
n--;
break;
}
printf("%d ,",stack[n]);
n--;
}
}
int main(){
int x = 0;
printf("Enter the elements of stack: ");
for (int i = 0; i < size; i++){
scanf("%d", &x);
Page |6
push(x);
}
display();
printf("\nDeleted element : %d\n",pop());
display();
return 0;
}

Output:
Enter the elements of stack: 6
2
8
3
7
Elements of stack are: 7 ,3 ,8 ,2 ,6
Deleted element : 7
Elements of stack are: 3 ,8 ,2 ,6
Page |7

PRACTICAL NO. 5
AIM: To perform Enqueue and Dequeue operations in QUEUE data structure..
#include <stdio.h>
#include <stdlib.h>
#define size 5
int queue[size];
int front = -1;
int rear = -1;
int isempty(){
if (front == -1)
return 1;
else
return 0;
}
int isfull(){
if (rear == (size - 1))
return 1;
else
return 0;
}
void enqueue(int x){
if (rear == (size - 1))
printf("Queue Overflow\n");
else{
if (rear == -1){
rear++;
front++;
queue[rear] = x;
}
else{
rear++;
queue[rear] = x;
}
}
}
void dequeue(){
int x;
if (front == -1){
printf("Queue Underflow\n");
}
else if (rear == front){
printf("Deleted element: %d\n", queue[front]);
front--;
rear--;
}
else{
printf("Deleted element: %d\n", queue[front]);
for (int i = 0; i < (size - 1); i++){
if (i + 1 == rear){
queue[i] = queue[i + 1];
front = 0;
Page |8
rear = i;
break;
}
else{
queue[i] = queue[i + 1];
}
}
}
}
void display(){
if (rear == -1)
printf("Queue underflow\n");
else{
printf("The elements of the queue are: \n");
if (rear == front){
printf("front -> %d <- rear\n", queue[front]);
}
else{
printf("front -> %d ,", queue[front]);
for (int i = front + 1; i <= rear - 1; i++){
printf("%d ,", queue[i]);
}
printf("%d <- rear\n", queue[rear]);
}
}
}
int main(){
enqueue(7);
enqueue(1);
enqueue(4);
enqueue(6);
enqueue(2);
display();
dequeue();
dequeue();
dequeue();
display();
return 0;
}

Output:
The elements of the queue are:
front -> 7 ,1 ,4 ,6 ,2 <- rear
Deleted element: 7
Deleted element: 1
Deleted element: 4
The elements of the queue are:
front -> 6 ,2 <- rear
Page |9

PRACTICAL NO. 6
AIM: To perform insertion and deletion operations at the tail of a linked list.
#include <stdio.h>
#include <malloc.h>
struct node{
int data;
struct node *next;
};
struct node *header = NULL;
typedef struct node Node;
void insert_head(int x){
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = x;
p->next = header;
header = p;
}
void insert_tail(int z){
struct node *p, *t;
p = (struct node *)malloc(sizeof(struct node));
p->data = z;
p->next = NULL;
t = header;
while (t->next != NULL){
t = t->next;
}
t->next = p;
}
void delete_tail(){
Node *t, *tp;
t = header;
while (t->next != NULL){
tp = t;
t = t->next;
}
tp->next = NULL;
free(t);
}
void display(Node *p){
printf("The elements of the linked list are: \n");
while (p != NULL){
if (p->next == NULL){
printf("%p-->", p);
printf("%d \n", p->data);
p = p->next;
}
else{
printf("%p-->", p);
printf("%d , ", p->data);
p = p->next;
}
P a g e | 10
}
}
int main(){
insert_head(2);
insert_tail(3);
insert_tail(7);
insert_tail(5);
display(header);
delete_tail();
display(header);
return 0;
}

Output:
The elements of the linked list are:

00D92980-->2 , 00D92990-->3 , 00D929A0-->7 , 00D929B0-->5

The elements of the linked list are:

00D92980-->2 , 00D92990-->3 , 00D929A0-->7


P a g e | 11

PRACTICAL NO. 7
AIM: To perform insertion and deletion operations at the front of a linked list.
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *header = NULL;
typedef struct node Node;
void insert_head(int x)
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = x;
p->next = header;
header = p;
}
void delete_head()
{
Node *p;
p = (struct node *)malloc(sizeof(struct node));
p = header;
header = header->next;
free(p);
}
void display(Node*p)
{
printf("The elements of the linked list are: \n");
while(p!=NULL)
{
if(p->next == NULL)
{
printf("%p-->",p);
printf("%d \n",p->data);
p = p->next;
}
else
{
printf("%p-->",p);
printf("%d , ",p->data);
p = p->next;
}
}
}
int main()
{
insert_head(1);
insert_head(7);
insert_head(13);
P a g e | 12
display(header);
insert_head(5);
printf("After Head Insertion, ");
display(header);
delete_head();
printf("After Head Deletion, ");
display(header);
return 0;
}

Output:
The elements of the linked list are:

00C529A0-->13 , 00C52990-->7 , 00C52980-->1

After Head Insertion, The elements of the linked list are:

00C529B0-->5 , 00C529A0-->13 , 00C52990-->7 , 00C52980-->1

After Head Deletion, The elements of the linked list are:

00C529A0-->13 , 00C52990-->7 , 00C52980-->1


P a g e | 13

PRACTICAL NO. 8
AIM: To perform insertion and deletion operations at mth and nth position of a linked list.
#include <stdio.h>
#include <malloc.h>
struct node{
int data;
struct node *next;
};
struct node *header = NULL;
typedef struct node node;
void insert_head(int x){
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = x;
p->next = header;
header = p;
}
void insert_m(int x, int m){
node *p, *t, *tp;
int c = 1;
t = header;
p = (node *)malloc(sizeof(node));
while (c != m && t->next != NULL){
tp = t;
t = t->next;
c++;
}
if (c == m){
if (t == header)
insert_head(x);
else{
p->data = x;
tp->next = p;
p->next = t;
}
}
else
printf("The linked list doesn't have %d elements\n", m);
}
void delete_n(int n){
node *tp, *t;
int c = 1;
t = header;
while (c != n && t->next != NULL){
tp = t;
t = t->next;
c++;
}
if (c == n){
if (t == header){
node *p;
P a g e | 14
p = (struct node *)malloc(sizeof(struct node));
p = header;
header = header->next;
free(p);
}
else{
tp->next = t ->next;
t = NULL;
free(t);
}
}
else
printf("The linked list doesn't have %d elements\n", n);
}
void display(node *p){
printf("The elements of the linked list are: \n");
while (p != NULL){
if (p->next == NULL){
printf("%p-->", p);
printf("%d \n", p->data);
p = p->next;
}
else{
printf("%p-->", p);
printf("%d , ", p->data);
p = p->next;
}
}
}
int main(){
insert_head(9);
insert_head(5);
insert_head(3);
insert_head(2);
insert_head(1);
display(header);
insert_m(6, 4);
printf("After Insertion, ");
display(header);
delete_n(3);
printf("After Deletion, ");
display(header);
return 0;
}
Output:
The elements of the linked list are:
007C29C0-->1 , 007C29B0-->2 , 007C29A0-->3 , 007C2990-->5 , 007C2980-->9
After Insertion, The elements of the linked list are:
007C29C0-->1 , 007C29B0-->2 , 007C29A0-->3 , 007C29D0-->6 , 007C2990-->5 ,
007C2980-->9
After Deletion, The elements of the linked list are:
007C29C0-->1 , 007C29B0-->2 , 007C29D0-->6 , 007C2990-->5 , 007C2980-->9
P a g e | 15

PRACTICAL NO. 9
AIM: To implement bubble sort algorithm.
#include <stdio.h>
void bubble_sort(int arr[], int size){
int temp;
for (int i = 0; i < size - 1; i++){
for (int j = 0; j < size - i - 1; j++){
if (arr[j] > arr[j + 1]){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void display(int arr[], int size){
printf("Elements of Array: ");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int main(){
int arr[] = {8, 2, 6, 4, 5, 9};
int size = sizeof(arr) / sizeof(int);
printf("Before sorting\n");
display(arr,size);
bubble_sort(arr, size);
printf("After Bubble Sort\n");
display(arr,size);
return 0;
}

Output:
Before sorting
Elements of Array: 8 2 6 4 5 9
After Bubble Sort
Elements of Array: 2 4 5 6 8 9
P a g e | 16

PRACTICAL NO. 10
AIM: To implement insertion sort algorithm.
#include <stdio.h>
void insertion_sort(int arr[], int size){
int temp;
for (int i = 1; i < size; i++){
for (int j = i - 1; j > -1; j--){
if (arr[j] > arr[j + 1]){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
else
break;
}
}
}
void display(int arr[], int size){
printf("Elements of Array: ");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int main(){
int arr[] = {6,2,9,8,3,4};
int size = sizeof(arr)/sizeof(int);
printf("Before sorting\n");
display(arr,size);
insertion_sort(arr,size);
printf("After Insertion Sort\n");
display(arr,size);
return 0;
}

Output:
Before sorting
Elements of Array: 6 2 9 8 3 4
After Insertion Sort
Elements of Array: 2 3 4 6 8 9
P a g e | 17

PRACTICAL NO. 11
AIM: To implement selection sort algorithm.
#include<stdio.h>
void selection_sort(int arr[],int size){
int minindex,temp;
for(int i = 0; i<size-1 ; i++){
minindex = i;
for(int j = i+1; j<size;j++){
if(arr[j]<arr[minindex]){
minindex = j;
}
}
if (minindex != i){
temp = arr[i];
arr[i] = arr[minindex];
arr[minindex] = temp;
}
}
}
void display(int arr[], int size){
printf("Elements of Array: ");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int main(){
int arr[] = {6,7,3,5,1,8};
int size = sizeof(arr)/sizeof(int);
printf("Before sorting\n");
display(arr,size);
selection_sort(arr,size);
printf("After Selection Sort\n");
display(arr,size);
return 0;
}

Output:
Before sorting
Elements of Array: 6 7 3 5 1 8
After Selection Sort
Elements of Array: 1 3 5 6 7 8
P a g e | 18

PRACTICAL NO. 12
AIM: To implement quick sort algorithm.
#include <stdio.h>
void swap(int *temp1, int *temp2){
int t = *temp1;
*temp1 = *temp2;
*temp2 = t;
}
int partition(int array[], int low, int high){

int piv = array[high];


int i = (low - 1);
for (int j = low; j < high; j++){
if (array[j] <= piv){
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}
void quickSort(int array[], int low, int high){
if (low < high){
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}
void display(int arr[], int size){
printf("Elements of Array: ");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int main(){
int arr[] = {6,7,8,2,3,1};
int size = sizeof(arr)/sizeof(int);
printf("Before Sorting\n");
display(arr, size);
quickSort(arr, 0, size - 1);
printf("After Quick Sort\n");
display(arr, size);
return 0;
}
Output:
Before Sorting
Elements of Array: 6 7 8 2 3 1
After Quick Sort
Elements of Array: 1 2 3 6 7 8
P a g e | 19

PRACTICAL NO. 13
AIM: To implement heap sort algorithm.
#include <stdio.h>
void swap(int *temp1, int *temp2){
int t = *temp1;
*temp1 = *temp2;
*temp2 = t;
}
void heapify(int arr[], int n, int i){
int lrg = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[lrg])
lrg = l;
if (r < n && arr[r] > arr[lrg])
lrg = r;
if (lrg != i){
swap(&arr[i], &arr[lrg]);
heapify(arr, n, lrg);
}
}
void heapSort(int arr[], int size){
for (int i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);
for (int i = size - 1; i >= 0; i--){
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
void display(int arr[], int size){
printf("Elements of Array: ");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int main(){
int arr[] = {8,3,6,9,4,1}, size = sizeof(arr)/sizeof(int);
printf("Before Sorting\n");
display(arr, size);
heapSort(arr, size);
printf("After Heap Sort\n");
display(arr, size);
return 0;
}
Output:
Before Sorting
Elements of Array: 8 3 6 9 4 1
After Heap Sort
Elements of Array: 1 3 4 6 8 9
P a g e | 20

PRACTICAL NO. 14
AIM: To implement merge sort algorithm.
#include <stdio.h>
void display(int arr[], int size){
printf("Elements of Array: ");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
void merge(int arr[], int l, int m, int r){
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2){
if (L[i] <= R[j]){
arr[k] = L[i];
i++;
}
else{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1){
arr[k] = L[i];
i++;
k++;
}
while (j < n2){
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r){
if (l < r){
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
P a g e | 21
}
int main(){
int arr[] = {2,7,9,8,6,3};
int size = sizeof(arr)/sizeof(int);
printf("Before Sorting\n");
display(arr, size);
mergeSort(arr, 0, size - 1);
printf("After Merge Sort\n");
display(arr, size);
return 0;
}

Output:
Before Sorting
Elements of Array: 2 7 9 8 6 3
After Merge Sort
Elements of Array: 2 3 6 7 8 9

You might also like