Dsa File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 65

Dsa lab file

6th semester

BACHELORS OF TECHNOLOGY
IN
Electronics and communication
Engineering

Submitted by -abhishek Yadav


Enroll no – 01513202821

Submitted to – mrs. Manisha mittal


INDEX

Sno. Experiments Date sign


EXPERIMENT – 01

AIM: Implementation of an Array and deletion from a specified location.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>
#include <conio.h>
void insert_ele(int i, int arr[], int len)
{
int ele, loc;
printf("\n enter the element to be inserted \n");
scanf("%d", &ele);
printf("\n enter the location\n");
scanf("%d", &loc);
if (i == 19)
{
printf("\nOverflow\n");
}
else
{
while (i >= loc)
{
arr[i + 1] = arr[i];
i--;
}
}
arr[loc] = ele;
len++;

printf("\n New array: ");


for (i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}

void delete_ele(int i, int arr[], int len)


{
int loc;
printf("enter the location of element to be deleted \n");
scanf("%d", &loc);
if (len == 0)

{
printf("\n Underflow");
}
else
{
for (i = loc; i < len; i++)
{
arr[i] = arr[i + 1];
}
len--;
printf("\n new array is\n");
for (i = 0; i < len; i++)
{
printf("\t%d ", arr[i]);
}
}
}
void main()
{
int arr[20], i = 0, len = 0, ch;
char choice;
printf("\nEnter number of elements you want to insert: ");
scanf("%d", &len);
for (i = 0; i < len; i++)
{
printf("enter the element %d ", i);
scanf("%d", &arr[i]);
}
i--;

label:
printf("\n 1.To insert element,2.To delete \n");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert_ele(i, arr, len);
break;
case 2:
delete_ele(i, arr, len);
break;
default:
printf("\n Invalid operation\n");
break;
}
printf("\n press y to continue or any other key to quit : ");
scanf("%s", &choice);
if (choice = 'y' || choice == 'Y')
{
goto label;
}
}
OUTPUT:
EXPERIMENT – 02

AIM: Implementation of a matrix, and see whether it is a sparse matrix or not.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>

void main()
{
int matrix[10][10];
int i, j, m, n;
int sparse_counter = 0;
printf("enter the orer of matrix : \n");
scanf("%d%d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &matrix[i][j]);
if (matrix[i][j] = 0)
{
++sparse_counter;
}
}
}
if (sparse_counter > ((m * n) / 2))
{
printf("The given matrix is sparse matrix!!!\n");
}
else
{
printf("The given matrix is not a sparse matrix\n");
}

printf("There are %d no of zeroes.", sparse_counter);


}
OUTPUT:
EXPERIMENT – 03

AIM: Implementation of Linear Search.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>

int main()
{
int arr[100], search, c, n;
printf("Enter no of elements\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &arr[c]);
}
printf("Enter the element to search for \n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (arr[c] == search)
{

printf("%d is present at location %d\n", search, c + 1);


break;
}
}
if (c == n)
{
printf("%d isnt present in the array.\n", search);
}
return 0;
}
OUTPUT:
EXPERIMENT – 04

AIM: Implementation of Binary Search.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>

int main()
{
int c, first, l, last, middle, n, search, arr[100];
printf("Enter No of elements \n");
scanf("%d", &n);
printf("Enter %d Integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &arr[c]);
}
printf("Enter the value to find :\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last)
{
if (arr[middle < search])
{
first = middle + 1;
}
else if (arr[middle] == search)
{

printf("%d found at location %d.\n", search, middle + 1);


break;
}
else
{
last = middle - 1;
if (first > last)
middle = (first + last) / 2;
}
}
if (first > last)
{
printf("Not found! %d isn't present in the list\n", search);
}

return 0;
}

OUTPUT:
EXPERIMENT – 05

AIM: Implementation of stack as an array.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>
int stack[100], choice, n, top, x, i;
void push(void);
void pop(void);

int main()
{
top = -1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d", &n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t- ---");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t
4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}

case 3:
{
printf("\n\t EXIT POINT");
break;
}

default:
{
printf("\n\t Please Enter a Valid
Choice(1/2/3/4)");
}
}
} while (choice != 4);
return 0;
}
void push()
{
if (top >= n - 1)
{
printf("\n\tSTACK is over flow");
}
else
{

printf(" Enter a value to be pushed:");


scanf("%d", &x);
top++;
stack[top] = x;
}
}
void pop()
{
if (top = 0)
{
printf("\n The elements in STACK \n");
for (i = top; i >= 0; i--)
printf("\n%d", stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}

OUTPUT:
EXPERIMENT – 06

AIM: Implementation of stack as a linked list.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>
#include <stdlib.h>
struct node
{

int data;
struct node *next;
};

struct node *front;


struct node *rear;
void insert();
void delete();
void display();
void main()
{
int choice;
while (choice != 4)
{
printf("\n********Main menu*******");
printf("\n1.insert an element\n2.delete an
element\n3.display\n4.exitthe program\n");
printf("\n enter your choice: ");
printf("\
n=========================================");
scanf("%d", &choice);
switch (choice)

{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n enter a valid choice: \n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\n enter a value?\n");
scanf("%d", &item);
ptr->data = item;
if (front == NULL)
{

front = ptr;
rear = ptr;
front->next = NULL;
rear->next = NULL;
}
else
{
rear->next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete()
{
struct node *ptr;
if (front == NULL)
{
printf("\nUNDERFLOW");
return;
}
else
{
ptr = front;
front = front->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
if (front == NULL)
{
printf("\nEMPTY QUEUE\n");
}

else
{
printf("\n printing values...\n");
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
}

OUTPUT:
EXPERIMENT – 07

AIM: Implementation of insertion , deletion on linked list.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>
#include <stdlib.h>
struct node
{

int data;
struct node *next;
};

struct node *front;


struct node *rear;
void insert();
void delete();
void display();
void main()
{
int choice;
while (choice != 4)
{
printf("\n********Main menu*******");
printf("\n1.insert an element\n2.delete an
element\n3.display\n4.exitthe program\n");
printf("\n enter your choice: ");
printf("\
n=========================================");
scanf("%d", &choice);
switch (choice)

{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n enter a valid choice: \n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\n enter a value?\n");
scanf("%d", &item);
ptr->data = item;
if (front == NULL)
{

front = ptr;
rear = ptr;
front->next = NULL;
rear->next = NULL;
}
else
{
rear->next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete()
{
struct node *ptr;
if (front == NULL)
{
printf("\nUNDERFLOW");
return;
}
else
{
ptr = front;
front = front->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
if (front == NULL)
{
printf("\nEMPTY QUEUE\n");
}

else
{
printf("\n printing values...\n");
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
}

OUTPUT:
EXPERIMENT – 08

AIM: Implementation of Queque.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void main()
{
int choice = 0;
while (choice != 9)
{
printf("\n\n *****Main menu *****\n");
printf("\n choose one of the option\n");
printf("\n-------------------------\n");
printf("\n1.Insert in beginning\n2.insert at
last\n3.Insert in the
middle\n4.delete in the beginning\n5.delete in
the last n6.delete in
the middle\n7.display the list\n");

printf("\n Enter the choice \n");


scanf("\n %d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("\n Enter any valid choice\
n");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct
node *));
if (ptr == NULL)
{
printf("OVERFLOW");
{

else
{

printf("\n Enter the value");


scanf("%d", &item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\n Node inserted\n");
}
}
}

void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
{

else
{
printf("\n Enter the value?\
n");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{

ptr->next = NULL;
head = ptr;
printf("\n Node
inserted");
}
}
else
{
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\n Node inserted");
}
}
}
}
void randominsert()
{
int i, loc, item;
struct node *ptr, *temp;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\n OVERFLOW");
}

else
{
printf("\n Enter the value\n");
scanf("%d", &item);
ptr->data = item;
printf("\n Enter the location of the
element to be inserted\n");
scanf("\n %d", &loc);
temp = head;
for (i = 0; i < loc; i++)
{
temp = temp->next;
if (temp == NULL)

printf("\n can't insert");


return;
}
}
ptr->next = temp->next;
temp->next = ptr;
printf("\n Node insert");
}
}

void display()
{
struct node *ptr;
ptr = head;
if (ptr == NULL)
{
printf("\n Nothing to print ");
}
else
{
printf("\n printing values.........\
n");
while (ptr != NULL)
{
printf("%d", ptr->data);
ptr = ptr->next;
}
}
}

void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\n list is empty\n");
}
else
{

ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the
beginning ..\n");
}
}
void last_delete()
{
struct node *ptr, *ptrl;
if (head == NULL)
{
printf("\n the list is empty\n");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list
deleted ..\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptrl = ptr;
ptr = ptr->next;
}
ptrl->next = NULL;
free(ptr);
printf("\n Deleted node from the
last..\n");
}
}

void random_delete()
{
struct node *ptr, *ptrl;
int loc, i;
printf("\n Enter the location of the
node after which you want to delete\n");
scanf("%d", &loc);
ptr = head;

for (i = 0; i < loc; i++)


{
ptrl = ptr;
ptr = ptr->next;
if (ptr == NULL)
{
printf("\n can't delete");
return;
}
}
ptrl->next = ptr->next;
free(ptr);
printf("\n deleted nodes%d", loc + 1);
}

་ OUTPUT:



EXPERIMENT – 09

AIM: Implementation of Queque as a linked list.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void main()
{
int choice = 0;

printf("\n\n *****Main menu *****\


n");
printf("\n choose one of the option\
n");
printf("\n-------------------------\
n");
printf("\n1.Insert in beginning\n");
printf("\ninsert at last\n");
printf("\nInsert in themiddle\n");
printf("\ndelete in the beginning\
n");
printf("\n delete in the last \n");
printf("\n delete inthe middle \n");
printf("\n display the list\n");
printf("\n Enter the choice \n");
scanf("\n %d", &choice);
return 0;
}
switch (choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("\n Enter any valid choice\
n");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node
*)malloc(sizeof(struct node *));
if (ptr == NULL)
{
printf("OVERFLOW");
{

else
{

printf("\n Enter the


value");
scanf("%d", &item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\n Node inserted\
n");
}
}
}

void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
{

else
{
printf("\n Enter the
value?\n");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{

ptr->next = NULL;
head = ptr;
printf("\n Node
inserted");
}
}
else
{
temp = head;
while (temp->next !=
NULL)
{
temp = temp-
>next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\n Node
inserted");
}
}
}
}
void randominsert()
{
int i, loc, item;
struct node *ptr, *temp;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\n OVERFLOW");
}

else
{
printf("\n Enter the value\
n");
scanf("%d", &item);
ptr->data = item;
printf("\n Enter the location
of the element to be inserted\n");
scanf("\n %d", &loc);
temp = head;
for (i = 0; i < loc; i++)
{
temp = temp->next;
if (temp == NULL)

{
printf("\n can't
insert");
return;
}
}
ptr->next = temp->next;
temp->next = ptr;
printf("\n Node insert");
}
}

void display()
{
struct node *ptr;
ptr = head;
if (ptr == NULL)
{
printf("\n Nothing to print
");
}
else
{
printf("\n printing
values.........\n");
while (ptr != NULL)
{
printf("%d", ptr->data);
ptr = ptr->next;
}
}
}

void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\n list is empty\n");
}
else
{

ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from
the beginning ..\n");
}
}
void last_delete()
{
struct node *ptr, *ptrl;
if (head == NULL)
{
printf("\n the list is empty\
n");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the
list deleted ..\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptrl = ptr;
ptr = ptr->next;
}
ptrl->next = NULL;
free(ptr);
printf("\n Deleted node from
the last..\n");
}
}

void random_delete()
{
struct node *ptr, *ptrl;
int loc, i;
printf("\n Enter the location of
the node after which you want to delete\
n");
scanf("%d", &loc);
ptr = head;

for (i = 0; i < loc; i++)


{
ptrl = ptr;
ptr = ptr->next;
if (ptr == NULL)
{
printf("\n can't
delete");
return;
}
}
ptrl->next = ptr->next;
free(ptr);
printf("\n deleted nodes%d", loc
+ 1);
}

OUTPUT:
EXPERIMENT – 10

AIM: Implementation of circular Queque with stack.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#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********");
printf("\
n=======================\n");
printf("\n1.insert an element\
n2.delete an element\n3.displaylist\n.4
exit");
printf("\n enter your choice?");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete ();
break;

case 3:
exit(0);
break;
default:
printf("\n Enter valid
choice\n");
}
}
}
void insert()
{
int item;
printf("\n enter the elements\n");
scanf("\n %d", &item);
if (rear == maxsize - 1)
{
printf("\n OVERFLOW\n");
return;
}
if (front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear + 1;
}
queue[rear] = item;
printf("\n Value inserted\n");
}

void delete()
{
int item;
if (front == -1 || front > rear)
{

printf("\nUNDERFLOEW\n");
return;
}
else
{
item = queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front = front + 1;
printf("\n value deleted\n");
}
}
}
OUTPUT:
EXPERIMENT – 11

AIM: Binary search tree insertion deletion and traversal.

SOFTWARE USED: VS CODE

PROGRAM CODE:

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

struct node
{
int key;
struct node *left, *right;
};

struct node *newNode(int item)


{
struct node *temp = (struct node
*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

void inorder(struct node *root)


{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}

struct node *find_Minimum(struct node


*node)
{
struct node *current = node;
while (current && current->left !=
NULL)
{
current = current->left;
}
return current;
}

struct node *insert(struct node *node, int


key)
{
if (node == NULL)
{
return newNode(key);
}
if (key < node->key)
{
node->left = insert(node->left,
key);
}
else
{
node->right = insert(node->right,
key);
}
return node;
}

struct node *deleteNode(struct node *root,


int key)
{
if (root == NULL)
{
return root;
}
if (key < root->key)
{
root->left = deleteNode(root->left,
key);
}
else if (key > root->key)
{
root->right = deleteNode(root-
>right, key);
}
else
{
if (root->left == NULL)
{
struct node *temp = root-
>right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
else
{
struct node *temp =
find_Minimum(root->right);
root->key = temp->key;
root->right = deleteNode(root-
>right, temp->key);
}
}
return root;
}

int main()
{
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);

printf("Inorder traversal before


deletion:\n");
inorder(root);
printf("\n");

printf("After deleting 8:\n");


root = deleteNode(root, 8);
inorder(root);

return 0;
}
OUTPUT:
EXPERIMENT – 12

AIM: Implementation of heap sort.

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>
void heapify(int a[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && a[left] > a[largest])
{
largest = left;
}
if (right < n && a[right] > a[largest])
{
largest = right;
}
if (largest != i)
{
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
void heapsort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(a, n, i);
}
for (int i = n - 1; i >= 0; i--)
{
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a, 1, 0);
}
}
void printarr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}
}
int main()
{
int a[] = {48, 10, 23, 43.8, 12, 36};
int n = sizeof(a) / sizeof(a[0]);
printarr(a, n);
heapsort(a, n);
printf("\n after sorting elements: ");
printarr(a, n);
return 0;
}

OUTPUT:
EXPERIMENT – 13

AIM: Implementation of insertion sort .

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>

int main()
{
int s, i, j, t, lst[50];

printf("Enter the size of the list: \


n");
scanf("%d", &s);
printf("Enter the integer values: \n");
for (i = 0; i < s; i++)
{
scanf("%d", &lst[i]);
}

for (i = 1; i < s; i++)


{
t = lst[i];
j = i - 1;
while ((t < lst[j]) && (j >= 0))
{
lst[j + 1] = lst[j];
j--;
}
lst[j + 1] = t;
}

printf("List after sorting is :");


for (i = 0; i < s; i++)
{
printf("%d ", lst[i]);
}

return 0;
}

OUTPUT:
EXPERIMENT – 14

AIM: Implementation of quick sort .

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>

void swap(int *a, int *b)


{
int t = *a;
*a = *b;
*b = t;
}

int partition(int array[], int low, int


high)
{
int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++)
{
if (array[j] <= pivot)
{
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 printarray(int array[], int size)


{
for (int i = 0; i < size; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted array: ");
printarray(data, n);
quicksort(data, 0, n - 1);
printf("Sorted array in ascending
order: \n");
printarray(data, n);
return 0;
}

OUTPUT:
EXPERIMENT – 15

AIM: Implementation of selection sort .

SOFTWARE USED: VS CODE

PROGRAM CODE:

#include <stdio.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionsort(int arr[], int n)


{
int i, j, min_id;
for (i = 0; i < n - 1; i++)
{
min_id = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_id])
{
min_id = j;
}
}
swap(&arr[min_id], &arr[i]);
}
}

void printarray(int arr[], int size)


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

int main()
{
int arr[] = {164, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionsort(arr, n);
printarray(arr, n);
return 0;
}
OUTPUT:

You might also like