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

merge sort

This material provides info about the merge sort

Uploaded by

ramyasushil
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 views5 pages

merge sort

This material provides info about the merge sort

Uploaded by

ramyasushil
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/ 5

//merge sort

#include <stdio.h>

#include <string.h>

#define MAX 60

void insert(int, char[10], char[10], int);

void display();

void merge(struct node*, int, int, int);

void mergeSort(struct node*, int, int);

struct node {

int age;

char name[10];

char species[10];

int cage_no;

} s[MAX];

int top = -1;

int main() {

int n, age;

char name[10];

char species[10];

int cage_no;

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

scanf("%d", &n);

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

printf("\nEnter the age: ");

scanf("%d", &age);
printf("Enter the name: ");

scanf("%s", name);

printf("Enter the species: ");

scanf("%s", species);

printf("Enter the cage_no: ");

scanf("%d", &cage_no);

insert(age,name,species,cage_no);

printf("\nUnsorted structure: ");

display();

mergeSort(s, 0, n - 1);

printf("\nSorted array: ");

display();

return 0;

// Function to merge two subarrays of s[]

// First subarray is s[l..m]

// Second subarray is s[m+1..r]

void merge(struct node s[], int l, int m, int r) {

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

// Create temporary arrays

struct node L[n1], R[n2];

// Copy data to temporary arrays L[] and R[]


for (i = 0; i < n1; i++)

L[i] = s[l + i];

for (j = 0; j < n2; j++)

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

// Merge the temporary arrays back into s[l..r]

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2) {

if (L[i].age <= R[j].age) {

s[k] = L[i];

i++;

} else {

s[k] = R[j];

j++;

k++;

// Copy the remaining elements of L[], if any

while (i < n1) {

s[k] = L[i];

i++;

k++;

// Copy the remaining elements of R[], if any


while (j < n2) {

s[k] = R[j];

j++;

k++;

// Merge Sort Algorithm that sorts s[l..r] using merge()

void mergeSort(struct node s[], int l, int r) {

if (l < r) {

// Same as (l+r)/2, but avoids overflow for large l and r

int m = l + (r - l) / 2;

// Sort first and second halves

mergeSort(s, l, m);

mergeSort(s, m + 1, r);

// Merge the sorted halves

merge(s, l, m, r);

void insert(int a, char b[10], char c[10], int d) {

if (top == MAX - 1) {

printf("\nSorry, the list is full");

return;

++top;

s[top].age = a;
strcpy(s[top].name, b);

strcpy(s[top].species,c);

s[top].cage_no = d;

void display() {

if (top == -1) {

printf("\nSorry, the list is empty");

return;

printf("\nThe list contents are:");

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

printf("\n%d \t%s \t%s \t%d ", s[i].age, s[i].name, s[i].species, s[i].cage_no);

You might also like