0% found this document useful (0 votes)
11 views

Sorting Assignment ADS

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)
11 views

Sorting Assignment ADS

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
You are on page 1/ 17

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 < pivot.roll) { // 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

You might also like