0% found this document useful (0 votes)
49 views37 pages

Document From Abhishek Singh

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)
49 views37 pages

Document From Abhishek Singh

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/ 37

EX-1

Q1:WAP in C to insert any number in between the user


defined array.

INPUT:

#include <stdio.h>
void main()
{
int array[100];
int i, n, x, pos;
printf("Enter the number of elements in the array \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are: \n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\nEnter the new element to be inserted: ");
scanf("%d", &x);
printf("Enter the position where element is to be inserted: ");
scanf("%d", &pos);
n=n+1;
for(i = n-1; i >= pos; i--)
array[i]=array[i-1];
array[pos-1]=x;

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


{
printf("%d ", array[i]);
}
}

OUTPUT:

Q2:WAP in C to insert any element in the starting of a


user defined array.
INPUT:
#include<stdio.h>
int main()
{
int arr[10],i,new;
for(int i=0;i<6;i++)
{
arr[i]=i+1;
}
printf("The initial array is:\n");
for(i=0;i<10;i++)
{
printf("%d\n",arr[i]);
}
printf("Enter the element you want is insert:");
scanf("%d",&new);
for(i=8;i>=0;i--)
{
arr[i+1]=arr[i];
}
arr[0]=new;
printf("The final array:\n");
for(i=0;i<10;i++)
{
printf("%d\t",arr[i]);
}
return 0;
}

Q-3 Write a program in c to insert an element in


ending of the ]given array
Input:
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int arr[MAX_SIZE];
int size, newElement;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &size);

// Input elements of the array


printf("Enter elements of array: ");
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);

// Input the new element


printf("Enter the element to insert at the end: ");
scanf("%d", &newElement);

// Insert the new element at the end


arr[size] = newElement;

// Increase the size of the array


size++;

// Print the updated array


printf("Array after insertion:\n");
for(int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");

return 0;
}

Output:
Enter the size of the array: 5
Enter elements of array: 2 3 4 5 6
Enter the element to insert at the end: 7
Array after insertion:
234567
EX-2

Q-1 Write a program in c to delete any element from


the given array:

#include <stdio.h>

int main() {
int arr[100], size, i, pos;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of array: ");
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("Enter the position to delete: ");
scanf("%d", &pos);
if (pos < 0 || pos >= size) {
printf("Invalid position!\n");
return 1;
}
for(i = pos; i < size - 1; i++)
arr[i] = arr[i + 1];
size--;
printf("Array after deletion:\n");
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");

return 0;
}

OUTPUT:
Enter the size of the array: 5
Enter elements of array: 2 3 4 5 6
Enter the position to delete: 2
Array after deletion:
2356

Q-2Write a program in c to delete any element from


the beginning of the array

Input:
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int arr[MAX_SIZE];
int size, i;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &size);

// Input elements of the array


printf("Enter elements of array: ");
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);

// Shift elements to the left to fill the gap


for(i = 0; i < size - 1; i++)
arr[i] = arr[i + 1];

// Decrease the size of the array


size--;

// Print the updated array


printf("Array after deletion:\n");
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");

return 0;
}

Output:
Enter the size of the array: 5
Enter elements of array: 2 3 4 5 6
Array after deletion:
3456
Q-3Write a program in c to delete any element from
the ending of the array

Input:
#include <stdio.h>

#define MAX_SIZE 100

int main() {
int arr[MAX_SIZE];
int size, i;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &size);

// Input elements of the array


printf("Enter elements of array: ");
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);

// Decrease the size of the array


size--;

// Print the updated array


printf("Array after deletion:\n");
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");

return 0;
}
Output:

Enter the size of the array: 5


Enter elements of array: 2 3 4 5 6
Array after deletion:
2345

PRACTICAL-1
*Write a program to allocate memory dynamically using malloc
and calloc function.
input:malloc
#include <stdio.h>
#include <stdlib.h>

int main()
{
int* ptr;
int n, i;

printf("Enter number of elements:");


scanf("%d",&n);
printf("Entered number of elements: %d\n", n);

ptr = (int*)malloc(n * sizeof(int));


if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}

printf("The elements of the array are: ");


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

return 0;
}
OUTPUT:
Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,

Input:calloc
#include <stdio.h>
#include <stdlib.h>

int main()
{

int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}

return 0;
}
OUTPUT:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,

PRACTICAL-2
*Write a program to implement
(a) linear Search
#include <stdio.h>

int main()
{
int a[10]={1,2,3,13,15,19,21,24,33,40};
int n,i,flag=0;

printf("enter no you want to search:");


scanf("%d",&n);
for(i=0;i<10;i++)
{ if(a[i]==n)
{
printf("no.found at position %d",i);
flag=1;
break;
}
}
if(flag=0)
printf("no.is not found");
return 0;
}
OUT OUT:
enter no you want to search:21
no.found at position 6

(b) Binary Search


#include <stdio.h>

int main()
{
int a[10]={1,2,3,13,15,19,21,24,33,40};
int i,num,low,high,mid;

printf("enter no you want to search:");


scanf("%d",&num);
low=0;
high=9;
while(low+high)
{
if (a[mid]<num)
{low=mid+1;}
else if(a[mid]==num)
{
printf("no. found at position %d", i);
break;
}
else
{
high=mid-1;
}
mid=(low+high)/2;
}
return 0;
}
OUT PUT:
enter no you want to search:3
no. found at position 0
Practical-3
Implement a program for stack that performs
following operations using array.
(a)PUSH (b) POP
(c) PEEP (d) CHANGE
(e) DISPLAY
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 20

int st[MAX], top = -1;

void push(int val) {


if (top == MAX - 1) {
printf("\nStack overflow");
} else {
top++;
st[top] = val;
}
}

int pop() {
int val;
if (top == -1) {
printf("\nStack underflow");
return -1;
} else {
val = st[top];
--top;
return val;
}
}

void peck() {
if (top == -1) {
printf("\nStack is empty");
} else {
printf("\nThe value at top is: %d\n", st[top]);
}
}

void display() {
int i;
if (top == -1) {
printf("\nStack is empty");
} else {
for (i = top; i >= 0; i--) {
printf("\n%d", st[i]);
}
printf("\n");
}
}

int main() {
int val, option;
do {
printf("\nChoose an option:");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Peck");
printf("\n4. Display");
printf("\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &option);

switch (option) {
case 1:
printf("\nEnter the number to push: ");
scanf("%d", &val);
push(val);
break;

case 2:
val = pop();
if (val!= -1) {
printf("\nThe value deleted is: %d\n", val);
}
break;

case 3:
peck();
break;

case 4:
display();
break;
}
} while (option!= 5);

return 0;
}

output:-
[?2004l

Choose an option:
1. Push
2. Pop
3. Peck
4. Display
5. Exit
Enter your choice: 1

Enter the number to push: 12

Choose an option:
1. Push
2. Pop
3. Peck
4. Display
5. Exit
Enter your choice: 2

The value deleted is: 12

Practical-4
Implement a program to convert infix notation
to postfix notation-using stack.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 50

char infix[MAX];
char postfix[MAX];
char stack[MAX];
int top = -1;

void push(char val);


char pop();
void process();
int precedence(char op);

int main() {
printf("Enter an infix expression: ");
scanf("%s", infix);
process();
printf("%s\n", postfix);
return 0;
}

void process() {
int i = 0, j = 0;
while (infix[i]!= '\0') {
if (isalpha(infix[i]) || isdigit(infix[i])) {
postfix[j] = infix[i];
i++;
j++;
} else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '^') {
while (top!= -1 && precedence(infix[i]) <= precedence(stack[top])) {
postfix[j] = pop();
j++;
}
push(infix[i]);
i++;
} else {
printf("Invalid expression\n");
exit(1);
}
}
while (top!= -1) {
postfix[j] = pop();
j++;
}
postfix[j] = '\0';
}

int precedence(char op) {


if (op == '^')
return 3;
else if (op == '*' || op == '/')
return 2;
else if (op == '+' || op == '-')
return 1;
else
return 0;
}

void push(char val) {


if (top == MAX - 1) {
printf("Stack overflow\n");
} else {
top++;
stack[top] = val;
}
}

char pop() {
char val;
if (top == -1) {
printf("Stack underflow\n");
} else {
val = stack[top];
top--;
}
return val;
}
Output:-
Enter an infix expression: a+b-c
ab+c-

Practical-5
Write a program to implement QUEUE using arrays that
performs following operations
(a)INSERT (b) DELETE (c) DISPLAY

#include<stdio.h>
#define MAX 50
int queue[MAX];
int front=-1,rear=-1;
void insert();
int delet();
void display();

void insert()
{
int num;
printf("Enter the number you want to enter :");
scanf("%d",&num);
if(rear==MAX-1)
{
printf("!! QUEUE OVERFLOW !!");
}
else
{
rear++;
queue[rear]=num;
}
}

int delet()
{
int val;
if(front==-1 || front>rear)
{
printf("!! QUEUE UNDERFLOW !!");
return -1;
}
else
{
val=queue[front];
front++;
if(front>rear)
front=rear=-1;
return val;
}
}

void display()
{
int i;
if(front==-1 || front>rear)
{
printf("!! QUEUE IS EMPTY !!");
}
else
{
for(i=front;front<=rear;i++);
printf("%d",queue[i]);
}
}
int main()
{
int op,x;

do{
printf("CHOODE AN OPTION :\n*\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.display\n");
printf("4.Exit\n*\n");
printf("Your Choice : ");
scanf("%d",&op);

switch(op)
{
case 1:
insert();
break;
case 2:
{
x=delet();
if(x!=-1)
printf("deleted value : %d",x);
}break;
case 3:
display();
break;
}
}while(op!=4);

return 0;
}

Output:-
CHOOsE AN OPTION :
*
1.Insert
2.Delete
3.display
4.Exit
*
Your Choice : 1
Enter the number you want to enter :13

Practical-6,7,8
Write a menu driven program to implement following operations on
the singly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Insert a node at the specified position
(d) Delete a first node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
struct node //construction of nodes
{
int data;
struct node *next;
};

struct node *start = NULL; // this is just like object but methods as object.
struct node *create_ll();
struct node *display();
struct node *insert_beg();
struct node *insert_end();
struct node *insert_after();
struct node *delete_beg();
struct node *delete_end();
struct node *delete_after();
struct node *delete_list();

int main ()
{
int option;
do
{

printf ("\n\n **MAIN MENU **");


printf ("\n 1: Create a list");
printf ("\n 2: Display the list");
printf ("\n 3: Add a node at the beginning");
printf ("\n 4: Add a node at the end");
printf ("\n 5: Add a node after a given node");
printf ("\n 6: Delete a node from the beginning");
printf ("\n 7: Delete a node from the end");
printf ("\n 8: Delete a node after a given node");
printf ("\n 9: Delete the entire list");
printf ("\n 10: EXIT");
printf ("\n\n Enter your option : ");
scanf ("%d", &option);

switch (option)
{

case 1:
start = create_ll ();
printf ("\n LINKED LIST CREATED");
break;
case 2:
start = display ();
break;
case 3:
start = insert_beg ();
break;
case 4:
start = insert_end ();
break;
case 5:
start = insert_after ();
break;
case 6:
start = delete_beg ();
break;
case 7:
start = delete_end ();
break;
case 8:
start = delete_after ();
break;
case 9:
start = delete_list ();
printf ("\n LINKED LIST DELETED");
break;
}
}while (option != 10);
return 0;
}

struct node *create_ll ()


{
struct node *new_node, *ptr;
int num;
printf ("\n Enter -1 to end\n");
printf ("\n Enter the data : ");
scanf ("%d", &num);
while (num != -1)
{
new_node = (struct node *) malloc (sizeof (struct node)); //new_node will hold
the new memory allocated.And malloc will make space which will be the type of struct so
that it'll hold data members which are inside it.
new_node->data = num;
if (start == NULL)
{
new_node->next = NULL;
start = new_node;
}
else
{
ptr = start;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = NULL;
}
printf ("\n Enter the data : ");
scanf ("%d", &num);
}

return start;
}

struct node *display ()


{
struct node *ptr;
ptr = start;
while (ptr != NULL)
{
printf ("\t %d", ptr->data);
ptr = ptr->next;
}

return start;
}

struct node *insert_beg ()


{
struct node *new_node;
int num;
printf ("\n Enter the data : ");
scanf ("%d", &num);
new_node = (struct node *) malloc (sizeof (struct node));
new_node->data = num;
new_node->next = start;
start = new_node;

return start;
}

struct node *insert_end ()


{
struct node *ptr, *new_node;
int num;
printf ("\n Enter the data : ");
scanf ("%d", &num);
new_node = (struct node *) malloc (sizeof (struct node));
new_node->data = num;
new_node->next = NULL;
ptr = start;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new_node;

return start;
}

struct node *insert_after ()


{
struct node *new_node, *ptr, *preptr;
int num, val;
printf ("\n Enter the data : ");
scanf ("%d", &num);
printf ("\n Enter the value after which the data has to be inserted : ");
scanf ("%d", &val);
new_node = (struct node *) malloc (sizeof (struct node));
new_node->data = num;
ptr = start;
preptr = ptr;
while (preptr->data != val)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = new_node;
new_node->next = ptr;

return start;
}

struct node *delete_beg ()


{
struct node *ptr;
ptr = start;
start = start->next;
free (ptr);
return start;
}

struct node *delete_end ()


{
struct node *ptr, *preptr;
ptr = start;
while (ptr->next != NULL)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = NULL;
free (ptr);

return start;
}

struct node *delete_after ()


{
struct node *ptr, *preptr;
int val;
printf ("\n Enter the value after which the node has to deleted : ");
scanf ("%d", &val);
ptr = start;
preptr = ptr;
while (preptr->data != val)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
free (ptr);
return start;
}

struct node *delete_list ()


{
struct node *ptr; // Lines 252-254 were modified from original code to fix
unresposiveness in output window
if (start != NULL)
{
ptr = start;
while (ptr != NULL)
{
printf ("\n %d is to be deleted next", ptr->data);
start = delete_beg (ptr);
ptr = start;

}
}
return start;
}
Output:-
**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node after a given node
6: Delete a node from the beginning
7: Delete a node from the end
8: Delete a node after a given node
9: Delete the entire list
10: EXIT

Enter your option :


Practical-9,10
Write a menu driven program to implement following operations on the
singly linked list.
(a)insert a node at the end of the linked list
(b)insert a node before specified position
(c)delete a first node of the linked list
(d)deletea node after specified position

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

struct Node {
int data;
struct Node* next;
};

void insertAtEnd(struct Node** startRef, int data);


void insertBeforePosition(struct Node** startRef, int data, int position);
void deleteFirstNode(struct Node** startRef);
void deleteAfterPosition(struct Node** startRef, int position);
void displayList(struct Node* node);

int main() {
struct Node* start = NULL;
int choice, data, position;

while (1) {
printf("\n1. Insert at the end\n2. Insert before a position\n3. Delete the first node\n4. Delete after a
position\n5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &data);
insertAtEnd(&start, data);
break;
case 2:
printf("Enter the value to insert: ");
scanf("%d", &data);
printf("Enter the position before which to insert: ");
scanf("%d", &position);
insertBeforePosition(&start, data, position);
break;
case 3:
deleteFirstNode(&start);
break;
case 4:
printf("Enter the position after which to delete: ");
scanf("%d", &position);
deleteAfterPosition(&start, position);
break;
case 5:
displayList(start);
break;
case 6:
exit(0);
default:
printf("Invalid choice\n");
}
}

return 0;
}

void insertAtEnd(struct Node** startRef, int data) {


struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
struct Node* last = *startRef;

newNode->data = data;
newNode->next = NULL;

if (*startRef == NULL) {
*startRef = newNode;
return;
}

while (last->next != NULL)


last = last->next;

last->next = newNode;
}

void insertBeforePosition(struct Node** startRef, int data, int position) {


struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
struct Node* current = *startRef;
struct Node* previous = NULL;

newNode->data = data;
newNode->next = NULL;

if (position == 1) {
newNode->next = current;
*startRef = newNode;
return;
}

while (current != NULL && position > 1) {


previous = current;
current = current->next;
position--;
}
if (current == NULL) {
printf("Position out of range\n");
return;
}

newNode->next = current;
previous->next = newNode;
}

void deleteFirstNode(struct Node** startRef) {


struct Node* temp = *startRef;

if (*startRef == NULL) {
printf("List is empty\n");
return;
}

*startRef = (*startRef)->next;
free(temp);
}

void deleteAfterPosition(struct Node** startRef, int position) {


struct Node* current = *startRef;
struct Node* nextNode = NULL;

if (current == NULL) {
printf("List is empty\n");
return;
}

if (position == 1) {
printf("Deletion after the first node is not allowed\n");
return;
}
while (current != NULL && position > 1) {
current = current->next;
position--;
}

if (current == NULL || current->next == NULL) {


printf("Position out of range\n");
return;
}

nextNode = current->next;
current->next = nextNode->next;
free(nextNode);
}

void displayList(struct Node* node) {


while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}

output:-

1. Insert at the end


2. Insert before a position
3. Delete the first node
4. Delete after a position
5. Display
6. Exit
Enter your choice: 1
Enter the value to insert: 2

1. Insert at the end


2. Insert before a position
3. Delete the first node
4. Delete after a position
5. Display
6. Exit
Enter your choice:

practical-11
Write a program to implement stack using linked list
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node* next;
};

struct node* top = NULL;

int is_empty() {
return top == NULL;
}

void push(int data) {


struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = top;
top = new_node;
}

int pop() {
if (is_empty()) {
printf("Error: Stack is empty.\n");
exit(1);
}
int data = top->data;
struct node* temp = top;
top = top->next;
free(temp);
return data;
}

void print_stack() {
struct node* temp = top;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
push(10);
push(20);
push(30);
push(40);
push(50);

printf("Stack after pushing 10, 20, 30, 40, 50:\n");


print_stack();

printf("Popped %d from stack.\n", pop());


printf("Popped %d from stack.\n", pop());

printf("Stack after popping 50 and 40:\n");


print_stack();

return 0;
}
Output:-
Stack after pushing 10, 20, 30, 40, 50:
50 -> 40 -> 30 -> 20 -> 10 -> NULL
Popped 50 from stack.
Popped 40 from stack.
Stack after popping 50 and 40:
30 -> 20 -> 10 -> NULL

Practical-12
Write a program to implement Bubble sort and Insertion
Sort.

Bubble sort
#include <stdio.h>

void bubble_sort(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;
}
}
}
}

void print_array(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("Unsorted array: \n");


print_array(arr, n);

bubble_sort(arr, n);

printf("Sorted array: \n");


print_array(arr, n);

return 0;
}

Output:-

Unsorted array:
64 34 25 12 22 11 90
Sorted array:
11 12 22 25 34 64 90

Insertion Sort.
#include <stdio.h>

void insertion_sort(int arr[], int n) {


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

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertion_sort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;

Output:-
Sorted array:
5 6 11 12 13

Practical-13

Write a program to implement Selection sort and Quick


Sort.

Selection sort
#include <stdio.h>

void selection_sort(int arr[], int n) {


int i, j, min_index, temp;
for (i = 0; i < n - 1; i++) {
min_index = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
if (min_index != i) {
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
}

int main() {
int arr[] = {21, 11, 31, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

Output:-
Sorted array:
6 7 11 21 31

Quick Sort.

#include <stdio.h>

void swap(int* a, int* b) {


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

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


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

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


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

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

Output:-
Sorted array:
1 5 7 8 9 10

Practical-13
Write a program to implement Merge sort.
#include <stdio.h>

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


int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;

int L[n1], R[n2];

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


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

i = 0;
j = 0;
k = left;
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 left, int right) {


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

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {24, 31, 73, 5,86, 7};
int n = sizeof(arr) / sizeof(arr[0]);

mergeSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:-
Sorted array:
5 7 24 31 73 86

You might also like