0% found this document useful (0 votes)
6 views17 pages

Student Sorting Algorithms in C

Uploaded by

Vedant Ransing
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)
6 views17 pages

Student Sorting Algorithms in C

Uploaded by

Vedant Ransing
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

Quick sort

#include <stdio.h>

#include <string.h>

// Structure for student details

struct stud {

char name[10];

int roll;

int total_marks;

};

// Function to display the student details

void display(struct stud s[], int size) {

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

printf("Name: %s \n", s[i].name);

printf("Roll No.: %d \n", s[i].roll);

printf("Total Marks: %d \n", s[i].total_marks);

printf("\n");

printf("________________________________________\n");

// Function to partition the array based on roll number for quicksort

int partition(struct stud a[], int start, int end) {

struct stud pivot = a[end]; // Choose the last element as pivot

int i = start - 1;

for (int j = start; j <= end - 1; j++) {


if (a[j].roll < [Link]) { // Compare roll numbers

i++;

struct stud temp = a[i];

a[i] = a[j];

a[j] = temp;

struct stud temp = a[i + 1];

a[i + 1] = a[end];

a[end] = temp;

return (i + 1);

// Function to implement quicksort on student structure

void quick(struct stud a[], int start, int end) {

if (start < end) {

int p = partition(a, start, end); // Partitioning index

quick(a, start, p - 1); // Sort left partition

quick(a, p + 1, end); // Sort right partition

int main() {

int size;

printf("Enter the number of students: ");

scanf("%d", &size);

struct stud s[size];

// Input student details


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

printf("Enter details for student %d\n", i + 1);

printf("Name: ");

scanf("%s", s[i].name);

printf("Roll Number: ");

scanf("%d", &s[i].roll);

printf("Total Marks: ");

scanf("%d", &s[i].total_marks);

printf("\n");

printf("Before sorting student details:\n");

display(s, size);

quick(s, 0, size - 1); // Apply quicksort on student structure

printf("After sorting by roll number:\n");

display(s, size);

return 0;

output
Merge sort

#include <stdio.h>

struct stud

char name[10];

int roll;

int total_marks;

};

void display(struct stud s[], int size )

for(int i=0; i < size; i++)

printf("name is: %s \n", s[i].name);

printf("roll no. is: %d \n", s[i].roll);

printf("total marks is: %d \n", s[i].total_marks);

printf("\n");

printf("________________________________________\n");

void merge(struct stud a[], int low, int mid, int high)

int i = low;
int j = mid + 1;

int k = 0;

struct stud b[100];

while(i<=mid && j<=high)

if (a[i].roll < a[j].roll)

b[k] = a[i];

i++;

k++;

else

b[k] = a[j];

j++;

k++;

while (i <= mid)

b[k] = a[i];

k++;

i++;

while (j <= high)

b[k] = a[j];
k++;

j++;

for (int c = low; c <= high; c++)

a[c] = b[c-low];

void mergesort(struct stud 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()

int size;

printf("Give the size of array:");

scanf("%d",&size);

struct stud s[size];

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

printf("Give %dth elements name:",i+1);

scanf("%s",&s[i].name);

printf("Give %dth elements roll number:",i+1);

scanf("%d",&s[i].roll);

printf("Give %dth elements total marks:",i+1);

scanf("%d",&s[i].total_marks);

printf("Here is your data: ");

display(s,size);

mergesort(s,0, size-1);

display(s,size);

Output
Insertion sort
#include<stdio.h>

struct stud

char name[10];

int roll;

int total_marks;

};

void display(struct stud s[], int size )

for(int i=0; i < size; i++)

printf("name is: %s \n", s[i].name);

printf("roll no. is: %d \n", s[i].roll);

printf("total marks is: %d \n", s[i].total_marks);

printf("\n");

printf("________________________________________\n");

void insertion_sort(struct stud s[], int size)

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

for(int j=i+1;j>0;j--)
{

if(s[j].roll<s[j-1].roll)

struct stud swap = s[j];

s[j] = s[j-1];

s[j-1] = swap;

display(s,size);

int main()

int size;

printf("Give the size of array:");

scanf("%d",&size);

struct stud s[size];

for(int i=0;i<size;i++)

printf("Give %dth elements name:",i+1);

scanf("%s",&s[i].name);

printf("Give %dth elements roll number:",i+1);

scanf("%d",&s[i].roll);

printf("Give %dth elements total marks:",i+1);

scanf("%d",&s[i].total_marks);
}

printf("Here is your data: ");

display(s,size);

printf("Below is your bubble sort algorithm:\n");

insertion_sort(s,size);

Output
Bubble sort
#include<stdio.h>

#include<stdlib.h>

struct stud

char name[10];

int roll;

int total_marks;

};

void display(struct stud s[], int size )

for(int i=0; i < size; i++)

printf("name is: %s \n", s[i].name);

printf("roll no. is: %d \n", s[i].roll);

printf("total marks is: %d \n", s[i].total_marks);

printf("\n");

printf("________________________________________\n");

void bubble_sort(struct stud s[], int size)

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

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

if(s[j].roll>s[j+1].roll)

struct stud swap = s[j];

s[j] = s[j+1];

s[j+1] = swap;

display(s,size);

int main()

int size;

printf("Give the size of array:");

scanf("%d",&size);

struct stud s[size];

for(int i=0;i<size;i++)

printf("Give %dth elements name:",i+1);

scanf("%s",&s[i].name);

printf("Give %dth elements roll number:",i+1);

scanf("%d",&s[i].roll);

printf("Give %dth elements total marks:",i+1);

scanf("%d",&s[i].total_marks);
}

printf("Here is your data: ");

display(s,size);

printf("Below is your bubble sort algorithm:\n");

bubble_sort(s,size);

return 0;

Output

Common questions

Powered by AI

Among the provided algorithms, mergesort is a stable sorting algorithm, meaning it maintains the relative order of records with equal keys (i.e., roll numbers). This is crucial in applications where maintaining order in additional fields, such as name or total marks, is important when records with equal roll numbers exist. Quicksort, however, is not stable as it could change the relative ordering of such records, whereas insertion sort and bubble sort are stable when implemented correctly, as they only swap adjacent elements when needed. In scenarios where stability is crucial, mergesort or stabilizing adjustments to other algorithms should be preferred .

The 'display' function is used across the sorting implementations to print the details of each student, including their name, roll number, and total marks. It helps to verify the state of the data before and after the application of the sorting algorithm, facilitating the observation of sorting results. This function iteratively goes through the student array and prints each student's details, which is useful for debugging and ensuring the correctness of the sorting algorithms .

When the initial student array is already sorted by roll number, insertion sort operates with maximal efficiency, achieving a time complexity of O(n). It traverses the list only once, without making any swaps, as each element is already in its correct position. In contrast, both quicksort and mergesort would still execute their recursive procedure, resulting in a time complexity of O(n log n), as they do not have built-in efficiency for already sorted arrays. Bubble sort would also perform at O(n), similarly making minimal swaps by recognizing the sorted order through a lack of change required in passes .

Quicksort has an average and best-case time complexity of O(n log n) but degrades to O(n^2) in the worst case, particularly with poor pivot choices. Mergesort maintains a consistent time complexity of O(n log n) across all cases due to its divide-and-conquer approach. Insertion sort, which works well on small or partially sorted arrays, has a time complexity of O(n^2) in the average and worst case, but O(n) in the best case when the array is already sorted. Bubble sort also has a time complexity of O(n^2) for both average and worst cases, but it can achieve O(n) in the best case when the array is already sorted .

A potential pitfall in the current implementations is the fixed length of 10 characters for the student's name in the struct, which can lead to buffer overflow if the input exceeds this length, causing undefined behavior. To mitigate this issue, the program should incorporate proper input control, using functions like fgets, which limit the number of characters read, or increase the allocated size after careful consideration of memory usage constraints. Adjustments for dynamic memory allocation could also be employed for more flexibility in handling inputs of variable lengths .

The recursive nature of mergesort and quicksort is central to their efficiency and logarithmic complexity. Mergesort divides the problem into two halves, recursively sorting each half and merging them, achieving a predictable O(n log n) efficiency by reducing the problem size logarithmically each time. Quicksort also benefits from divide-and-conquer, partitioning the array around a pivot and recursively sorting the partitions. Both algorithms thus maximize work done per level of recursion, with the depth of recursion dictating their efficiency in handling large datasets .

The partition function in the quicksort algorithm uses the last element of the array as the pivot. The significance of the pivot is that it helps in dividing the array into two parts: elements less than the pivot to its left and elements greater than the pivot to its right. This is achieved by iterating over the array, swapping elements to ensure that all elements with smaller values than the pivot come before all elements with greater values, thus setting up the array for recursive sorting of these partitions. This process reduces the problem size each time by approximately half, leading to an average time complexity of O(n log n).

To prevent the O(n^2) worst-case scenario in quicksort, especially when sorting student records, one can adopt techniques like choosing a random pivot or using the median-of-three method, which selects the pivot as the median of the first, middle, and last elements. These approaches aim to ensure a more balanced partitioning, reducing the likelihood of encountering a degenerate case where an already sorted or reverse sorted array causes suboptimal partitioning. Additionally, implementing a hybrid sort that switches to insertion sort for small subarrays can further optimize performance .

The total_marks field in the student struct is not directly utilized in the sorting algorithms as presented, which currently sort based on the roll field. However, should there be a need for sorting by total marks (e.g., for rankings), the algorithms could be easily adapted by modifying the comparison conditions to consider total_marks instead of roll numbers. Such a change would leverage the same efficient sorting mechanisms while providing flexibility based on the specific sorting criteria needed in various educational contexts .

The current mergesort implementation uses a fixed-size temporary array 'b' with a capacity for 100 elements, which limits the handling of input arrays larger than this size. To handle arrays larger than 100 elements, one could dynamically allocate the temporary array 'b' using dynamic memory allocation functions like malloc, ensuring that 'b' is of sufficient size to accommodate all elements between 'low' and 'high' during the merge process .

You might also like