[go: up one dir, main page]

0% found this document useful (0 votes)
13 views62 pages

DSA List of Experiments

This document outlines a lab file for a Data Structure and Algorithm course, detailing practical programming exercises related to various data structures such as arrays, stacks, queues, and linked lists. Each practical includes a description of the task, example code, and expected output. The document serves as a guide for students to implement and understand fundamental data structures and algorithms in programming.

Uploaded by

arorasargam3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views62 pages

DSA List of Experiments

This document outlines a lab file for a Data Structure and Algorithm course, detailing practical programming exercises related to various data structures such as arrays, stacks, queues, and linked lists. Each practical includes a description of the task, example code, and expected output. The document serves as a guide for students to implement and understand fundamental data structures and algorithms in programming.

Uploaded by

arorasargam3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

LAB FILE

DATA STRUCTURE AND ALGORITHM


CAN605

Session :2024-25

Submitted to: Submitted by:


Dr. Sanjay Gupta Sargam Arora
Assistant Professor 1000026809
School of Computing MCA-IInd Sem
DIT University

S. No. List of Practical’s


1 Write a program to search an element from array using linear search.

2 Write a program to search an element from array using Binary Search.

3 Write a program to implement Stack using array.

4 Write a program to implement Queue using array.

5 Write a program to implement Circular Queue using array.

6 Write a program to implement Stack Using Linked List

7 Write a program to implement Queue using linked list.

8 Write a program to show the implementation of single linked list


(Insertion, deletion).

9 Write a program to show the implementation of circular single linked list


(Insertion, deletion).

10 Write a program to show the implementation of double linked list


(Insertion, deletion).

11 Write a program to sort elements using Bubble Sort.

12 Write a program to sort elements using Merge Sort.

13 Write a program to sort elements using Heap Sort.

14 Write a program to sort elements using Quick Sort.

15 Write a program to sort elements using Selection Sort.

Practical :1
Write a program to search an element from array using linear search.

Code:

#include <stdio.h>

int main() {

int arr[100], n, i, search, found = 0;

printf("Enter number of elements in array: ");

scanf("%d", &n);

printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);

printf("Enter the element to search: ");

scanf("%d", &search);

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

if(arr[i] == search) {

printf("Element %d found at position %d.\n", search, i + 1);

found = 1;

break;

if(!found) {

printf("Element %d not found in the array.\n", search);

}
return 0;

Output:

Practical: 2
Write a program to search an element from array using Binary Search

Code:

#include <stdio.h>

int main() {

int arr[100], n, i, search, low, high, mid;

printf("Enter number of elements in the array: ");

scanf("%d", &n);

printf("Enter %d elements in sorted order:\n", n);

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

scanf("%d", &arr[i]);

printf("Enter the element to search: ");

scanf("%d", &search);

low = 0;

high = n - 1;

while(low <= high) {

mid = (low + high) / 2;

if(arr[mid] == search) {

printf("Element %d found at position %d.\n", search, mid + 1);

return 0;

} else if(arr[mid] < search) {

low = mid + 1;

} else {
high = mid - 1;

printf("Element %d not found in the array.\n", search);

return 0;

Output:

Practical: 3
Write a program to implement Stack using array.

Code:
#include <stdio.h>

#define SIZE 100

int stack[SIZE];

int top = -1;

void push(int value) {

if(top == SIZE - 1) {

printf("Stack Overflow! Cannot push %d\n", value);

} else {

top++;

stack[top] = value;

printf("%d pushed to stack.\n", value);

void pop() {

if(top == -1) {

printf("Stack Underflow! No elements to pop.\n");

} else {

printf("Popped element: %d\n", stack[top]);

top--;

void peek() {

if(top == -1) {

printf("Stack is empty.\n");

} else {
printf("Top element is: %d\n", stack[top]);

void display() {

if(top == -1) {

printf("Stack is empty.\n");

} else {

printf("Stack elements:\n");

for(int i = top; i >= 0; i--) {

printf("%d\n", stack[i]);

int main() {

int choice, value;

while(1) {

printf("\n--- Stack Menu ---\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Peek\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch(choice) {

case 1:
printf("Enter value to push: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

break;

case 5:

printf("Exiting...\n");

return 0;

default:

printf("Invalid choice! Try again.\n");

return 0;

Output:
Practical: 4

Write a program to implement Queue using array.


Code:

#include <stdio.h>

#define SIZE 100

int queue[SIZE];

int front = -1, rear = -1;

void enqueue(int value) {

if (rear == SIZE - 1) {

printf("Queue Overflow\n");

} else {

if (front == -1)

front = 0;

rear++;

queue[rear] = value;

printf("%d enqueued to queue\n", value);

void dequeue() {

if (front == -1 || front > rear) {

printf("Queue Underflow\n");

} else {

printf("%d dequeued from queue\n", queue[front]);

front++;

}
void peek() {

if (front == -1 || front > rear) {

printf("Queue is empty\n");

} else {

printf("Front element is %d\n", queue[front]);

void display() {

if (front == -1 || front > rear) {

printf("Queue is empty\n");

} else {

printf("Queue elements: ");

for (int i = front; i <= rear; i++) {

printf("%d ", queue[i]);

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\nQueue Operations:\n");

printf("1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
printf("Enter value to enqueue: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

peek();

break;

case 4:

display();

break;

case 5:

return 0;

default:

printf("Invalid choice\n");

return 0;

}
Output:
Practical :5

Write a program to implement Circular Queue using array.

Code:

#include <stdio.h>

#define SIZE 5

int queue[SIZE];

int front = -1, rear = -1;

void enqueue(int value) {

if ((front == 0 && rear == SIZE - 1) || (rear + 1) % SIZE == front) {

printf("Queue Overflow (Circular Queue is Full)\n");

} else {

if (front == -1) front = 0;

rear = (rear + 1) % SIZE;

queue[rear] = value;

printf("%d enqueued to queue\n", value);

void dequeue() {

if (front == -1) {

printf("Queue Underflow (Queue is Empty)\n");

} else {

printf("%d dequeued from queue\n", queue[front]);

if (front == rear) {
front = rear = -1;

} else {

front = (front + 1) % SIZE;

void peek() {

if (front == -1) {

printf("Queue is empty\n");

} else {

printf("Front element is %d\n", queue[front]);

void display() {

if (front == -1) {

printf("Queue is empty\n");

} else {

printf("Queue elements: ");

int i = front;

while (1) {

printf("%d ", queue[i]);

if (i == rear)

break;

i = (i + 1) % SIZE;
}

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\nCircular Queue Operations:\n");

printf("1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to enqueue: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

peek();

break;

case 4:
display();

break;

case 5:

return 0;

default:

printf("Invalid choice\n");

return 0;

Output:
Practical: 6

Write a program to implement Stack Using Linked List.

Code:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* top = NULL;

void push(int value) {

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

if (!newNode) {

printf("Heap Overflow\n");

return;

newNode->data = value;

newNode->next = top;

top = newNode;

printf("%d pushed to stack\n", value);

void pop() {

if (top == NULL) {

printf("Stack Underflow\n");
return;

struct Node* temp = top;

printf("%d popped from stack\n", top->data);

top = top->next;

free(temp);

void peek() {

if (top == NULL) {

printf("Stack is empty\n");

} else {

printf("Top element is %d\n", top->data);

void display() {

if (top == NULL) {

printf("Stack is empty\n");

} else {

struct Node* temp = top;

printf("Stack elements: ");

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

}
printf("\n");

int main() {

int choice, value;

while (1) {

printf("\nStack Operations Using Linked List:\n");

printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();
break;

case 5:

return 0;

default:

printf("Invalid choice\n");

return 0;

Output:
Practical: 7

Write a program to implement Queue using linked list.


Code:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* front = NULL;

struct Node* rear = NULL;

void enqueue(int value) {

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

if (!newNode) {

printf("Memory allocation failed\n");

return;

newNode->data = value;

newNode->next = NULL;

if (rear == NULL) {

front = rear = newNode;

} else {

rear->next = newNode;

rear = newNode;

printf("%d enqueued to queue\n", value);

}
void dequeue() {

if (front == NULL) {

printf("Queue Underflow (Queue is empty)\n");

return;

struct Node* temp = front;

printf("%d dequeued from queue\n", front->data);

front = front->next;

if (front == NULL)

rear = NULL;

free(temp);

void peek() {

if (front == NULL) {

printf("Queue is empty\n");

} else {

printf("Front element is %d\n", front->data);

void display() {

if (front == NULL) {

printf("Queue is empty\n");

} else {

struct Node* temp = front;

printf("Queue elements: ");


while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\nQueue Operations Using Linked List:\n");

printf("1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to enqueue: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

peek();

break;

case 4:
display();

break;

case 5:

return 0;

default:

printf("Invalid choice\n");

return 0;

}
Practical:8

Write a program to show the implementation of single linked list (Insertion,


deletion).

Code:
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

void insertAtBeginning(int value) {

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

if (!newNode) {

printf("Memory allocation failed\n");

return;

newNode->data = value;

newNode->next = head;

head = newNode;

printf("%d inserted at the beginning\n", value);

void insertAtEnd(int value) {

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

if (!newNode) {

printf("Memory allocation failed\n");


return;

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

printf("%d inserted at the end\n", value);

void insertAtPosition(int value, int position) {

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

if (!newNode) {

printf("Memory allocation failed\n");

return;

newNode->data = value;

if (position == 1) {

newNode->next = head;

head = newNode;

printf("%d inserted at position %d\n", value, position);

return;
}

struct Node* temp = head;

for (int i = 1; i < position - 1 && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL) {

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

} else {

newNode->next = temp->next;

temp->next = newNode;

printf("%d inserted at position %d\n", value, position);

void deleteAtBeginning() {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

head = head->next;

printf("%d deleted from the beginning\n", temp->data);

free(temp);

void deleteAtEnd() {

if (head == NULL) {

printf("List is empty\n");

return;
}

struct Node* temp = head;

if (temp->next == NULL) {

head = NULL;

printf("%d deleted from the end\n", temp->data);

free(temp);

return;

while (temp->next->next != NULL) {

temp = temp->next;

struct Node* lastNode = temp->next;

temp->next = NULL;

printf("%d deleted from the end\n", lastNode->data);

free(lastNode);

void deleteAtPosition(int position) {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

if (position == 1) {

head = head->next;

printf("%d deleted from position %d\n", temp->data, position);

free(temp);

return;
}

for (int i = 1; i < position - 1 && temp != NULL; i++) {

temp = temp->next;

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

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

} else {

struct Node* nodeToDelete = temp->next;

temp->next = temp->next->next;

printf("%d deleted from position %d\n", nodeToDelete->data, position);

free(nodeToDelete);

void display() {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

printf("List: ");

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

}
int main() {

int choice, value, position;

while (1) {

printf("\nSingle Linked List Operations:\n");

printf("1. Insert at Beginning\n2. Insert at End\n3. Insert at Position\n");

printf("4. Delete at Beginning\n5. Delete at End\n6. Delete at Position\n");

printf("7. Display\n8. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insertAtBeginning(value);

break;

case 2:

printf("Enter value to insert: ");

scanf("%d", &value);

insertAtEnd(value);

break;

case 3:

printf("Enter value to insert: ");

scanf("%d", &value);

printf("Enter position: ");

scanf("%d", &position);

insertAtPosition(value, position);
break;

case 4:

deleteAtBeginning();

break;

case 5:

deleteAtEnd();

break;

case 6:

printf("Enter position: ");

scanf("%d", &position);

deleteAtPosition(position);

break;

case 7:

display();

break;

case 8:

return 0;

default:

printf("Invalid choice\n");

return 0;

}
Output:
Practical:9

Write a program to show the implementation of circular single linked list


(Insertion, deletion).

Code:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

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

if (newNode == NULL) {

printf("Memory allocation failed\n");

return NULL;

newNode->data = data;

newNode->next = newNode;

return newNode;

void insertEnd(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {
struct Node* temp = *head;

while (temp->next != *head) {

temp = temp->next;

temp->next = newNode;

newNode->next = *head;

void insertBegin(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

struct Node* temp = *head;

while (temp->next != *head) {

temp = temp->next;

temp->next = newNode;

newNode->next = *head;

*head = newNode;

void deleteBegin(struct Node** head) {

if (*head == NULL) {
printf("List is empty\n");

return;

if (*head == (*head)->next) {

free(*head);

*head = NULL;

} else {

struct Node* temp = *head;

struct Node* last = *head;

while (last->next != *head) {

last = last->next;

*head = (*head)->next;

last->next = *head;

free(temp);

void deleteEnd(struct Node** head) {

if (*head == NULL) {

printf("List is empty\n");

return;

if (*head == (*head)->next) {

free(*head);
*head = NULL;

} else {

struct Node* temp = *head;

struct Node* prev = NULL;

while (temp->next != *head) {

prev = temp;

temp = temp->next;

prev->next = *head;

free(temp);

void display(struct Node* head) {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("(head)\n");

}
int main() {

struct Node* head = NULL;

insertEnd(&head, 10);

insertEnd(&head, 20);

insertEnd(&head, 30);

insertEnd(&head, 40);

printf("Circular Linked List after Insertion at End: ");

display(head);

insertBegin(&head, 5);

printf("Circular Linked List after Insertion at Beginning: ");

display(head);

deleteBegin(&head);

printf("Circular Linked List after Deletion from Beginning: ");

display(head);

deleteEnd(&head);

printf("Circular Linked List after Deletion from End: ");

display(head);
return 0;

Output:
Practical:10

Write a program to show the implementation of double linked list (Insertion,


deletion).

Code:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

struct Node* createNode(int data) {

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

if (newNode == NULL) {

printf("Memory allocation failed\n");

return NULL;

newNode->data = data;

newNode->next = NULL;

newNode->prev = NULL;

return newNode;

void insertEnd(struct Node** head, int data) {


struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

void insertBegin(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

newNode->next = *head;

(*head)->prev = newNode;

*head = newNode;

}
void deleteBegin(struct Node** head) {

if (*head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = *head;

if (*head == (*head)->next) { // Only one node in the list

*head = NULL;

} else {

*head = (*head)->next;

(*head)->prev = NULL;

free(temp);

void deleteEnd(struct Node** head) {

if (*head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = *head;

if (*head == (*head)->next) { // Only one node in the list

*head = NULL;

free(temp);
} else {

while (temp->next != NULL) {

temp = temp->next;

temp->prev->next = NULL;

free(temp);

void display(struct Node* head) {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

while (temp != NULL) {

printf("%d <-> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

insertEnd(&head, 10);
insertEnd(&head, 20);

insertEnd(&head, 30);

insertEnd(&head, 40);

printf("Doubly Linked List after Insertion at End: ");

display(head);

insertBegin(&head, 5);

printf("Doubly Linked List after Insertion at Beginning: ");

display(head);

deleteBegin(&head);

printf("Doubly Linked List after Deletion from Beginning: ");

display(head);

deleteEnd(&head);

printf("Doubly Linked List after Deletion from End: ");

display(head);

return 0;

}
Output:

Practical:10
Write a program to sort elements using Bubble Sort.
Code:

#include <stdio.h>

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

int i, j, temp;

for (i = 0; i < n - 1; i++) {

for (j = 0; j < n - 1 - i; j++) {

if (arr[j] > arr[j + 1]) {

// Swap arr[j] and arr[j+1]

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

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;

Output:

Practical:12
Write a program to sort elements using Merge Sort.
Code:

#include <stdio.h>

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

int n1 = mid - left + 1;

int n2 = right - mid;

int leftArr[n1], rightArr[n2];

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

leftArr[i] = arr[left + i];

for (int j = 0; j < n2; j++) {

rightArr[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {

if (leftArr[i] <= rightArr[j]) {

arr[k] = leftArr[i];

i++;

} else {

arr[k] = rightArr[j];

j++;

k++;

while (i < n1) {

arr[k] = leftArr[i];
i++;

k++;

while (j < n2) {

arr[k] = rightArr[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[] = {38, 27, 43, 3, 9, 82, 10};

int n = sizeof(arr) / sizeof(arr[0]);


printf("Original array: ");

printArray(arr, n);

mergeSort(arr, 0, n - 1);

printf("Sorted array: ");

printArray(arr, n);

return 0;

Output:

Practical:13
Write a program to sort elements using Heap Sort.

Code:
#include <stdio.h>

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

int temp = *a;

*a = *b;

*b = temp;

void heapify(int arr[], int n, int i) {

int largest = i; // Initialize largest as root

int left = 2 * i + 1; // left = 2*i + 1

int right = 2 * i + 2; // right = 2*i + 2

if (left < n && arr[left] > arr[largest]) {

largest = left;

if (right < n && arr[right] > arr[largest]) {

largest = right;

if (largest != i) {

swap(&arr[i], &arr[largest]);

heapify(arr, n, largest);

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

for (int i = n / 2 - 1; i >= 0; i--) {

heapify(arr, n, i);

}
for (int i = n - 1; i > 0; i--) {

swap(&arr[0], &arr[i]); // Move current root to end

heapify(arr, i, 0); // Call max heapify on the reduced heap

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

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

printArray(arr, n);

heapSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0;

Output:
Practical:14
Write a program to sort elements using Quick Sort.

Code:
#include <stdio.h>

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

int temp = *a;

*a = *b;

*b = temp;

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

int pivot = arr[high]; // pivot element

int i = low - 1; // index of smaller element

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

if (arr[j] <= pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return i + 1; // return the partitioning index

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

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1); // sort the left part

quickSort(arr, pi + 1, high); // sort the right part

}
}

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]);

printf("Original array: ");

printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: ");

printArray(arr, n);

return 0;

Output:

Practical:15
. Write a program to sort elements using Selection Sort.

Code:
#include <stdio.h>

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

int temp = *a;

*a = *b;

*b = temp;

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

for (int i = 0; i < n - 1; i++) {

int minIndex = i; // Assume the minimum element is at the current index

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j; // Update the minimum element index

swap(&arr[minIndex], &arr[i]);

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

for (int i = 0; i < size; 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;

Output:

You might also like