0% found this document useful (0 votes)
26 views8 pages

Dsa 2

The document contains code examples for sorting algorithms in C, including bubble sort, insertion sort, and selection sort, applied to both user-input and randomly generated arrays of integers. It also includes functionality to read employee data from a file and sort it by age using the same sorting algorithms. Additionally, modifications for sorting in descending order are provided for each sorting algorithm.

Uploaded by

Sid1
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)
26 views8 pages

Dsa 2

The document contains code examples for sorting algorithms in C, including bubble sort, insertion sort, and selection sort, applied to both user-input and randomly generated arrays of integers. It also includes functionality to read employee data from a file and sort it by age using the same sorting algorithms. Additionally, modifications for sorting in descending order are provided for each sorting algorithm.

Uploaded by

Sid1
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/ 8

Practical 2

Set A
a) Sort a random array of n integers (accept the value of n from user) in ascending order
by using bubble sort algorithm.
#include <stdio.h>
#include<conio.h>
void bubbleSort(int array[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
int main()
{
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int array[10];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Array before sorting:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
bubbleSort(array, n);
printf("\nArray after sorting:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
Getch();
Clrscr();
return 0;
}
b) Sort a random array of n integers (create a random array of n integers) in ascending order by using
insertion sort algorithm.
 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
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 n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
srand(time(0));
printf("Random array generated:\n");
for (int i = 0; i < n; i++) {
arr[i] = rand() % 100;
printf("%d ", arr[i]);
}
printf("\n");
insertionSort(arr, n);
printf("Sorted array in ascending order:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
c) Sort a random array of n integers (accept the value of n from user) in ascending order
by using selection sort algorithm.
#include <stdio.h>
#include<conio.h>
void selectionSort(int array[], int n)
{
int i, j, min_idx, temp;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
{
if (array[j] < array[min_idx])
{
min_idx = j;
}
}
temp = array[min_idx];
array[min_idx] = array[i];
array[i] = temp;
}
}
int main()
{
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int array[10];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Array before sorting:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
selectionSort(array, n);
printf("\nArray after sorting:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
getch();
clrscr();
return 0;
}

Set B
a) Read the data from the file “employee.txt” and sort on age using bubble sort, insertion
sort and selection sort.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee
{
char name[50];
int age;
};
int readEmployeeData(struct Employee employees[], int max_size)
{
FILE *file = fopen("C:/Users/Anuja/OneDrive/Dokumen/employee.txt", "r");
if (file == NULL)
{
printf("Error opening file.\n");
return -1;
}
int i = 0;
while (fscanf(file, "%s %d", employees[i].name, &employees[i].age)!= EOF && i < max_size)
{
i++;
}
fclose(file);
return i;
}
void printEmployees(struct Employee employees[], int n)
{
printf("Name - Age");
for (int i = 0; i < n; i++)
{
printf("%s, %d\n", employees[i].name, employees[i].age);
}
}
void bubbleSort(struct Employee employees[], int n)
{
struct Employee temp;
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (employees[j].age > employees[j+1].age)
{
temp = employees[j];
employees[j] = employees[j+1];
employees[j+1] = temp;
}
}
}
}
void insertionSort(struct Employee employees[], int n)
{
for (int i = 1; i < n; i++)
{
struct Employee key = employees[i];
int j = i - 1;
while (j >= 0 && employees[j].age > key.age)
{
employees[j + 1] = employees[j];
j--;
}
employees[j + 1] = key;
}
}
void selectionSort(struct Employee employees[], int n)
{
for (int i = 0; i < n-1; i++)
{
int min_index = i;
for (int j = i+1; j < n; j++)
{
if (employees[j].age < employees[min_index].age)
{
min_index = j;
}
}
struct Employee temp = employees[i];
employees[i] = employees[min_index];
employees[min_index] = temp;
}
}
int main()
{
struct Employee employees[100];
int n = readEmployeeData(employees, 100);
if (n == -1)
{
return 1;
}
printf("Original Employee Data:\n");
printEmployees(employees, n);
printf("\nSorted Employee Data by Age (Bubble Sort):\n");
bubbleSort(employees, n);
printEmployees(employees, n);
printf("\nSorted Employee Data by Age (Insertion Sort):\n");
insertionSort(employees, n);
printEmployees(employees, n);
printf("\nSorted Employee Data by Age (Selection Sort):\n");
selectionSort(employees, n);
printEmployees(employees, n);
return 0;
}

Set C
a) What modification is required to bubble sort, insertion sort and selection to sort the integers in descending
order?
 #include <stdio.h>
void bubbleSortDescending(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void insertionSortDescending(int arr[], int size) {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void selectionSortDescending(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
if (maxIndex != i) {
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
}
}
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 size = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");

bubbleSortDescending(arr, size);
printf("Array sorted using Bubble Sort in descending order:\n");
printArray(arr, size);
int arr2[] = {64, 25, 12, 22, 11};
insertionSortDescending(arr2, size);
printf("Array sorted using Insertion Sort in descending order:\n");
printArray(arr2, size);
int arr3[] = {64, 25, 12, 22, 11};
printf("Resetting the array for Selection Sort.\n");
selectionSortDescending(arr3, size);
printf("Array sorted using Selection Sort in descending order:\n");
printArray(arr3, size);
return 0;
}

You might also like