Submitted to: Shawban Mahmud
Submitted By:
Name & ID: Anika Tasnim (252014041)
Joye arufa neushe (252014004)
Afrina Jamal Moumita (252014026)
Course Title: Data structures lab
Course Code: 0613-014-1302
Section: 02
Semester: Fall 2026
Date of Submission: 18-04-2026
Title: Student Record Management and Sorting System Using C
[Link]
This project focuses on developing a menu-driven C program to manage student
academic records and implement multiple sorting algorithms. The main purpose
of this assignment is to understand how different sorting algorithms work and to
evaluate their performance under different conditions.
Sorting is a fundamental concept in computer science. Different algorithms
behave differently depending on the dataset. This project allows us to compare
their efficiency in terms of execution time, number of comparisons, and number
of swaps or movements.
The program is designed in a modular way, ensuring readability, reusability, and
maintainability of the code.
[Link] Structure Design:
The program uses an array of structures to store student information.
Structure definition:
typedef struct {
int id;
char name[50];
float marks;
char grade;
} stu;
Two arrays are maintained:
● s[] → stores the original dataset
● t[] → used as a copy for sorting operations
The maximum number of students is defined as 200.
Each student record includes:
● Student ID
● Name
● Marks
● Grade
Marks are validated to stay within 0 to 100. The grade is automatically assigned
using the grd() function based on marks:
A (80+), B (70–79), C (60–69), D (50–59), F (below 50)
This ensures consistent and error-free data handling.
[Link] Algorithms Implementation:
The program supports two methods for creating datasets:
Manual Input (inp function):
The user enters each student’s ID, name, and marks. The program validates
marks and automatically assigns the correct grade.
Random Data Generation (rnd function):
The program generates random student records, including:
● Random ID (1000–9999)
● Random names from a predefined list
● Random marks (0–100)
● Automatically calculated grade
This feature is useful for testing sorting algorithms with larger datasets.
Sorting Algorithms Implementation:
Six sorting algorithms are implemented, each in a separate function:
Bubble Sort (bub):
Compares adjacent elements and swaps them if necessary. It includes an
optimization to stop early if the array becomes sorted.
Selection Sort (sel):
Finds the minimum or maximum element from the unsorted part and places it in
the correct position.
Insertion Sort (ins):
Builds the sorted array step by step by inserting each element into its correct
position. It uses movement counting instead of swaps.
Merge Sort (mgs, mrg):
Uses a divide-and-conquer approach. The array is recursively divided into
smaller parts and then merged in sorted order.
Quick Sort (qck, prt):
Selects the last element as a pivot and partitions the array into smaller and larger
elements relative to the pivot.
Heap Sort (hip, hpf):
Constructs a heap from the data and repeatedly extracts the root element to sort
the array.
Each algorithm is implemented manually without using any built-in sorting
functions.
Algorithm Analysis:
This program implements six different sorting algorithms to organize student
records based on
either ID or Marks in ascending or descending order. The performance of each
algorithm is
evaluated using three metrics:
C (Comparisons): Number of comparisons made between elements
S / M (Swaps / Movements):
Swaps (for Bubble, Selection, Quick, Heap)
Movements (for Insertion, Merge)
T (Time): Execution time in seconds
[Link] Sort
Idea: Repeatedly compares adjacent elements and swaps them if they are in the
wrong order.
Best Case: O(n) (already sorted, optimized with flag)
Average Case: O(n²)
Worst Case: O(n²)
Stable: Yes
Observation: Bubble Sort performs well only for small or nearly sorted datasets.
The optimization using a flag reduces unnecessary passes.
[Link] Sort
Idea: Selects the minimum (or maximum) element and places it in the correct
position.
Best Case: O(n²)
Average Case: O(n²)
Worst Case: O(n²)
Stable: No
Observation: Number of comparisons is always the same, but swaps are fewer
compared to Bubble Sort.
[Link] Sort
Idea: Inserts each element into its correct position in the sorted portion.
Best Case: O(n)
Average Case: O(n²)
Worst Case: O(n²)
Stable: Yes
Observation: Efficient for small datasets and nearly sorted data. Uses
movements instead of swaps, which is reflected in the output.
[Link] Sort
Idea: Uses divide-and-conquer strategy by dividing the array and merging sorted
subarrays.
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)
Stable: Yes
Observation: Consistent performance regardless of input order. Requires extra
memory for temporary arrays.
[Link] Sort
Idea: Selects a pivot and partitions the array around it.
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n²) (when poorly chosen pivot)
Stable: No
Observation: Very fast in practice, but performance depends on pivot selection.
In this program, the last element is used as pivot.
[Link] Sort
Idea: Builds a heap and repeatedly extracts the maximum (or minimum) element.
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)
Stable: No
Observation: Does not require extra memory like Merge Sort and guarantees
consistent performance.
[Link] Features:
The program allows flexible sorting based on user preferences.
Sorting Field:
● 1 → Student ID
● 2 → Marks
Sorting Order:
● 1 → Ascending
● 2 → Descending
[Link]-Driven System:
The program operates through a continuous loop and displays the following
menu:
1. Size
2. Rand
3. Inp
4. Show
5. Save
6. Load
7. Sort
8. Bench
9. Exit
Users can:
● Set dataset size (10, 30, 50, or custom)
● Generate or input data
● Display current records
● Save data to file
● Load data from file
● Apply sorting algorithms
● Run benchmark analysis
The system allows repeated operations without restarting, making it user-friendly
and efficient.
[Link] Measurement:
Each sorting algorithm tracks:
● Number of comparisons (C)
● Number of swaps (S) or movements (M)
● Execution time using clock()
Example output:
C=120 S=45 T=0.00012
Insertion Sort uses movement count instead of swaps, which better reflects its
behavior.
[Link] Analysis:
The benchmark feature (option 8) compares all sorting algorithms.
Process:
● A random dataset is generated
● The dataset is copied for fair comparison
● A sorted dataset is created using Quick Sort
● A reverse dataset is created from the sorted data
● Each algorithm runs on the same base dataset
Output is displayed in tabular form:
Algo C Op T
Bub ... ... ...
Sel ... ... ...
Ins ... ... ...
Mer ... ... ...
Qck ... ... ...
Hip ... ... ...
This helps in analyzing which algorithm performs better under similar conditions.
[Link] Handling:
The program supports saving and loading data using a CSV file named
"[Link]".
Save (sv):
Writes all student records into the file.
Load (ld):
Reads student records from the file and updates the dataset.
The format used:
ID,Name,Marks,Grade
This allows easy storage and reuse of data.
[Link]:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX 200
typedef struct {
int id;
char name[50];
float marks;
char grade;
} stu;
stu s[MAX], t[MAX];
int n;
char grd(float m) {
if(m>=80) return 'A';
else if(m>=70) return 'B';
else if(m>=60) return 'C';
else if(m>=50) return 'D';
else return 'F';
void swp(stu *a, stu *b) {
stu tmp = *a;
*a = *b;
*b = tmp;
int gt(stu a, stu b, int f) {
if(f==1) return [Link] > [Link];
else return [Link] > [Link];
int lt(stu a, stu b, int f) {
if(f==1) return [Link] < [Link];
else return [Link] < [Link];
}
void cpy(stu d[], stu src[]) {
for(int i=0; i<n; i++) d[i] = src[i];
void show(stu a[]) {
printf("\n");
for(int i=0; i<n; i++) {
printf("%d\t%s\t%.2f\t%c\n", a[i].id, a[i].name, a[i].marks, a[i].grade);
void inp() {
for(int i=0; i<n; i++) {
printf("\nStudent %d:\n", i+1);
printf("ID: ");
scanf("%d", &s[i].id);
printf("Name: ");
scanf(" %[^\n]", s[i].name);
printf("Marks: ");
scanf("%f", &s[i].marks);
if(s[i].marks<0) s[i].marks=0;
if(s[i].marks>100) s[i].marks=100;
s[i].grade = grd(s[i].marks);
printf("\nAdded %d\n", n);
void rnd() {
char *nm[] = {"John","Emma","Mike","Sara","Tom","Lisa","Ali","Mia"};
for(int i=0; i<n; i++) {
s[i].id = 1000 + rand()%9000;
strcpy(s[i].name, nm[rand()%8]);
s[i].marks = (rand()%10100)/100.0;
s[i].grade = grd(s[i].marks);
printf("\nGenerated %d\n", n);
void sv() {
FILE *f = fopen("[Link]", "w");
if(!f) { printf("Error\n"); return; }
fprintf(f, "ID,Name,Marks,Grade\n");
for(int i=0; i<n; i++) {
fprintf(f, "%d,%s,%.2f,%c\n", s[i].id, s[i].name, s[i].marks, s[i].grade);
}
fclose(f);
printf("\nSaved\n");
void ld() {
FILE *f = fopen("[Link]", "r");
if(!f) { printf("Not found\n"); return; }
char buf[200];
fgets(buf, sizeof(buf), f);
int i=0;
while(fscanf(f, "%d,%[^,],%f,%c", &s[i].id, s[i].name, &s[i].marks, &s[i].grade)==4 &&
i<MAX) {
i++;
n = i;
fclose(f);
printf("\nLoaded %d\n", n);
void bub(stu a[], int f, int up, long *c, long *sw) {
*c=0; *sw=0;
for(int i=0; i<n-1; i++) {
int flg=0;
for(int j=0; j<n-i-1; j++) {
(*c)++;
if(up) {
if(gt(a[j], a[j+1], f)) {
swp(&a[j], &a[j+1]);
(*sw)++;
flg=1;
} else {
if(lt(a[j], a[j+1], f)) {
swp(&a[j], &a[j+1]);
(*sw)++;
flg=1;
if(!flg) break;
void sel(stu a[], int f, int up, long *c, long *sw) {
*c=0; *sw=0;
for(int i=0; i<n-1; i++) {
int idx=i;
for(int j=i+1; j<n; j++) {
(*c)++;
if(up) {
if(lt(a[j], a[idx], f)) idx=j;
} else {
if(gt(a[j], a[idx], f)) idx=j;
if(idx!=i) {
swp(&a[i], &a[idx]);
(*sw)++;
void ins(stu a[], int f, int up, long *c, long *mv) {
*c=0; *mv=0;
for(int i=1; i<n; i++) {
stu key = a[i];
int j = i-1;
if(up) {
while(j>=0 && gt(a[j], key, f)) {
(*c)++;
a[j+1] = a[j];
j--;
(*mv)++;
} else {
while(j>=0 && lt(a[j], key, f)) {
(*c)++;
a[j+1] = a[j];
j--;
(*mv)++;
a[j+1] = key;
(*mv)++;
void mrg(stu a[], int l, int m, int r, int f, int up, long *c, long *mv) {
stu L[MAX], R[MAX];
int n1 = m-l+1, n2 = r-m;
for(int i=0; i<n1; i++) L[i] = a[l+i];
for(int j=0; j<n2; j++) R[j] = a[m+1+j];
int i=0, j=0, k=l;
while(i<n1 && j<n2) {
(*c)++;
if(up) {
if(!gt(L[i], R[j], f)) a[k++] = L[i++];
else {
a[k++] = R[j++];
(*mv)++;
} else {
if(!lt(L[i], R[j], f)) a[k++] = L[i++];
else {
a[k++] = R[j++];
(*mv)++;
while(i<n1) a[k++] = L[i++];
while(j<n2) a[k++] = R[j++];
void mgs(stu a[], int l, int r, int f, int up, long *c, long *mv) {
if(l<r) {
int m = (l+r)/2;
mgs(a, l, m, f, up, c, mv);
mgs(a, m+1, r, f, up, c, mv);
mrg(a, l, m, r, f, up, c, mv);
int prt(stu a[], int l, int h, int f, int up, long *c, long *sw) {
stu pvt = a[h];
int i = l-1;
for(int j=l; j<h; j++) {
(*c)++;
if(up) {
if(!gt(a[j], pvt, f)) {
i++;
swp(&a[i], &a[j]);
(*sw)++;
} else {
if(!lt(a[j], pvt, f)) {
i++;
swp(&a[i], &a[j]);
(*sw)++;
}
swp(&a[i+1], &a[h]);
(*sw)++;
return i+1;
void qck(stu a[], int l, int h, int f, int up, long *c, long *sw) {
if(l<h) {
int p = prt(a, l, h, f, up, c, sw);
qck(a, l, p-1, f, up, c, sw);
qck(a, p+1, h, f, up, c, sw);
void hpf(stu a[], int i, int sz, int f, int up, long *c, long *sw) {
int ex = i;
int l = 2*i+1, r = 2*i+2;
if(up) {
if(l<sz) { (*c)++; if(gt(a[l], a[ex], f)) ex = l; }
if(r<sz) { (*c)++; if(gt(a[r], a[ex], f)) ex = r; }
} else {
if(l<sz) { (*c)++; if(lt(a[l], a[ex], f)) ex = l; }
if(r<sz) { (*c)++; if(lt(a[r], a[ex], f)) ex = r; }
if(ex != i) {
swp(&a[i], &a[ex]);
(*sw)++;
hpf(a, ex, sz, f, up, c, sw);
void hip(stu a[], int f, int up, long *c, long *sw) {
*c=0; *sw=0;
for(int i=n/2-1; i>=0; i--) hpf(a, i, n, f, up, c, sw);
for(int i=n-1; i>0; i--) {
swp(&a[0], &a[i]);
(*sw)++;
hpf(a, 0, i, f, up, c, sw);
int main() {
int ch, f, up, al;
long c, v;
clock_t st, en;
srand(time(NULL));
n=0;
while(1) {
printf("\[Link]\[Link]\[Link]\[Link]\[Link]\[Link]\[Link]\[Link]\[Link]\n");
printf("Choice: ");
scanf("%d", &ch);
if(ch==9) { printf("Bye\n"); break; }
if(ch==1) {
int sz;
printf("\[Link](10)\[Link](30)\[Link](50)\[Link]\n");
printf("Choice: ");
scanf("%d", &sz);
if(sz==1) n=10;
else if(sz==2) n=30;
else if(sz==3) n=50;
else if(sz==4) {
printf("Size: ");
scanf("%d", &n);
if(n<1) n=10;
if(n>MAX) n=MAX;
else n=50;
printf("Size=%d\n", n);
else if(ch==2) { if(n==0) printf("Set size\n"); else rnd(); }
else if(ch==3) { if(n==0) printf("Set size\n"); else inp(); }
else if(ch==4) { if(n==0) printf("No data\n"); else show(s); }
else if(ch==5) { if(n==0) printf("No data\n"); else sv(); }
else if(ch==6) { ld(); }
else if(ch==7) {
if(n==0) { printf("Set size\n"); continue; }
printf("F(1=ID 2=Marks): "); scanf("%d", &f);
printf("O(1=Asc 2=Desc): "); scanf("%d", &up);
printf("A(1=Bub 2=Sel 3=Ins 4=Mer 5=Qck 6=Hip): "); scanf("%d", &al);
cpy(t, s);
c=0; v=0;
st = clock();
if(al==1) bub(t, f, up==1, &c, &v);
else if(al==2) sel(t, f, up==1, &c, &v);
else if(al==3) ins(t, f, up==1, &c, &v);
else if(al==4) mgs(t, 0, n-1, f, up==1, &c, &v);
else if(al==5) qck(t, 0, n-1, f, up==1, &c, &v);
else if(al==6) hip(t, f, up==1, &c, &v);
else { printf("Invalid\n"); continue; }
en = clock();
show(t);
if(al==3) printf("C=%ld M=%ld T=%.6f\n", c, v,
(double)(en-st)/CLOCKS_PER_SEC);
else printf("C=%ld S=%ld T=%.6f\n", c, v, (double)(en-st)/CLOCKS_PER_SEC);
else if(ch==8) {
if(n==0) { printf("Set size\n"); continue; }
printf("F(1=ID 2=Marks): "); scanf("%d", &f);
printf("O(1=Asc 2=Desc): "); scanf("%d", &up);
int asc = (up==1);
stu fix[MAX], srt[MAX], rev[MAX];
rnd();
cpy(fix, s);
cpy(srt, fix);
long x,y;
qck(srt, 0, n-1, f, asc, &x, &y);
for(int i=0; i<n; i++) rev[i] = srt[n-1-i];
printf("\nAlgo\tC\tOp\tT\n");
for(al=1; al<=6; al++) {
cpy(t, fix);
long c1, v1;
c1=0; v1=0;
st = clock();
if(al==1) bub(t, f, asc, &c1, &v1);
else if(al==2) sel(t, f, asc, &c1, &v1);
else if(al==3) ins(t, f, asc, &c1, &v1);
else if(al==4) mgs(t, 0, n-1, f, asc, &c1, &v1);
else if(al==5) qck(t, 0, n-1, f, asc, &c1, &v1);
else hip(t, f, asc, &c1, &v1);
en = clock();
double tm = (double)(en-st)/CLOCKS_PER_SEC;
if(al==1) printf("Bub\t%ld\t%ld\t%.6f\n", c1, v1, tm);
if(al==2) printf("Sel\t%ld\t%ld\t%.6f\n", c1, v1, tm);
if(al==3) printf("Ins\t%ld\t%ld\t%.6f\n", c1, v1, tm);
if(al==4) printf("Mer\t%ld\t%ld\t%.6f\n", c1, v1, tm);
if(al==5) printf("Qck\t%ld\t%ld\t%.6f\n", c1, v1, tm);
if(al==6) printf("Hip\t%ld\t%ld\t%.6f\n", c1, v1, tm);
else printf("Invalid\n");
return 0;
}
[Link]:
[Link] Display:
The show() function is used to display student records in a clear tabular format:
ID Name Marks Grade
This makes the output easy to read and understand.
[Link] Structure:
The program follows a modular structure:
● Separate functions for each sorting algorithm
● Helper functions for comparison and swapping
● Dedicated functions for input, output, and file handling
● Benchmarking handled independently
This structure improves readability, debugging, and maintainability.
[Link]:
This project successfully demonstrates the implementation of multiple sorting
algorithms in a real-world scenario. It shows how different algorithms perform
under different conditions.
From the observations:
● Quick Sort and Merge Sort perform efficiently for large datasets
● Insertion Sort works well for small or nearly sorted data
● Bubble and Selection Sort are less efficient but useful for understanding
basic concepts
Overall, the program provides both practical experience and performance
analysis of sorting algorithms in C.