Assignment 3 Sorting Algorithms –Counting Sort, Merge Sort, Quick Sort
employee.txt (Input File)
Amit 25 30000
Neha 22 35000
Rahul 30 45000
Priya 24 40000
Karan 28 38000
(Format: Emp_name Age Salary)
1. Counting Sort on Age
#include <stdio.h>
#include <string.h>
struct Employee
{
char name[30];
int age;
float salary;
};
int main()
{
struct Employee emp[100], output[100];
int count[101] = {0};
int n = 0, i;
FILE *fp, *fs;
fp = fopen("employee.txt", "r");
if (fp == NULL)
{
printf("File not found.\n");
return 1;
}
while (fscanf(fp, "%s%d%f",
emp[n].name,
&emp[n].age,
&emp[n].salary) != EOF)
{
n++;
}
fclose(fp);
// Count frequency of ages
for (i = 0; i < n; i++)
count[emp[i].age]++;
// Cumulative count
for (i = 1; i <= 100; i++)
count[i] += count[i - 1];
// Build output array
for (i = n - 1; i >= 0; i--)
{
output[count[emp[i].age] - 1] = emp[i];
count[emp[i].age]--;
}
fs = fopen("sortedemponage.txt", "w");
fprintf(fs, "Name\tAge\tSalary\n");
for (i = 0; i < n; i++)
fprintf(fs, "%s\t%d\t%.2f\n",
output[i].name,
output[i].age,
output[i].salary);
fclose(fs);
printf("Data sorted using Counting Sort.\n");
return 0;
}
2. Merge Sort on Age
#include <stdio.h>
#include <string.h>
struct Employee
{
char name[30];
int age;
float salary;
};
void merge(struct Employee a[], int low, int mid, int high)
{
struct Employee temp[100];
int i = low, j = mid + 1, k = low;
while (i <= mid && j <= high)
{
if (a[i].age <= a[j].age)
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i <= mid)
temp[k++] = a[i++];
while (j <= high)
temp[k++] = a[j++];
for (i = low; i <= high; i++)
a[i] = temp[i];
}
void mergeSort(struct Employee a[], int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
merge(a, low, mid, high);
}
}
int main()
{
struct Employee emp[100];
int n = 0, i;
FILE *fp, *fs;
fp = fopen("employee.txt", "r");
if (fp == NULL)
{
printf("File not found.\n");
return 1;
}
while (fscanf(fp, "%s%d%f",
emp[n].name,
&emp[n].age,
&emp[n].salary) != EOF)
{
n++;
}
fclose(fp);
mergeSort(emp, 0, n - 1);
fs = fopen("sortedemponage.txt", "w");
fprintf(fs, "Name\tAge\tSalary\n");
for (i = 0; i < n; i++)
fprintf(fs, "%s\t%d\t%.2f\n",
emp[i].name,
emp[i].age,
emp[i].salary);
fclose(fs);
printf("Data sorted using Merge Sort.\n");
return 0;
}
employee.txt (Input File)
Amit 25 30000
Neha 22 35000
Rahul 30 45000
Priya 24 40000
Karan 28 38000
Format: Emp_name Age Salary
C Program (Quick Sort on Employee Name)
#include <stdio.h>
#include <string.h>
struct Employee
{
char name[30];
int age;
float salary;
};
void quickSort(struct Employee emp[], int low, int high);
int partition(struct Employee emp[], int low, int high);
int main()
{
struct Employee emp[100];
int n = 0, i;
FILE *fp, *fs;
fp = fopen("employee.txt", "r");
if (fp == NULL)
{
printf("File not found.\n");
return 1;
}
while (fscanf(fp, "%s%d%f",
emp[n].name,
&emp[n].age,
&emp[n].salary) != EOF)
{
n++;
}
fclose(fp);
quickSort(emp, 0, n - 1);
fs = fopen("sortedemponname.txt", "w");
if (fs == NULL)
{
printf("Cannot create output file.\n");
return 1;
}
fprintf(fs, "Name\tAge\tSalary\n");
for (i = 0; i < n; i++)
{
fprintf(fs, "%s\t%d\t%.2f\n",
emp[i].name,
emp[i].age,
emp[i].salary);
}
fclose(fs);
printf("Data sorted successfully using Quick Sort.\n");
printf("Sorted data stored in sortedemponname.txt\n");
return 0;
}
int partition(struct Employee emp[], int low, int high)
{
struct Employee pivot = emp[high], temp;
int i = low - 1, j;
for (j = low; j < high; j++)
{
if (strcmp(emp[j].name, pivot.name) < 0)
{
i++;
temp = emp[i];
emp[i] = emp[j];
emp[j] = temp;
}
}
temp = emp[i + 1];
emp[i + 1] = emp[high];
emp[high] = temp;
return i + 1;
}
void quickSort(struct Employee emp[], int low, int high)
{
if (low < high)
{
int p = partition(emp, low, high);
quickSort(emp, low, p - 1);
quickSort(emp, p + 1, high);
}
}
No comments:
Post a Comment