You are on page 1of 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment: 4

Student Name: Aman Kumar UID: 21BCS5218


Branch: CSE Section/Group: 617/B
Semester: 3rd Date of Performance: 14/09/2022
Subject Name: DATA STRUCTURES Subject Code: 21CSH-211

1. Aim of the practical:

To sort an array using merge sort technique.

2. Objective:

Write a program to sort an array of integers in ascending/descending order using:

a) Merge sort.

3. Introduction

The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquer
paradigm. In this algorithm, the array is initially divided into two equal halves and then they
are combined in a sorted manner.

4. Software Used

Online C Compiler

5. Hardware requirements:

Processor= AMD RYZEN 3


RAM= 8.00 GB
SSD= 512 GB
Keyboard and Mouse.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Algorithm:

Step 1: Start
Step 2: Declare array and left, right, mid variable
Step 3: Perform merge function.
   if left > right
     return
   mid= (left+right)/2
   mergesort(array, left, mid)
   mergesort(array, mid+1, right)
   merge(array, left, mid, right)
Step 4: Stop

7. Program code:

#include<stdio.h>

void mergesort(int a[],int i,int j);

void merge(int a[],int i1,int j1,int i2,int j2);

int main()

printf("Aman Kumar 21BCS5218 ");;

int a[30],n,i;

printf("\nEnter no of elements:");

scanf("%d",&n);

printf("Enter the elements:\n");

for(i=0;i<n;i++)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

scanf("%d",&a[i]);

mergesort(a,0,n-1);

printf("\nSorted array:\n");

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

printf("%d\n",a[i]);

return 0;

void mergesort(int a[],int i,int j)

int mid;

if(i<j)

mid=(i+j)/2;

mergesort(a,i,mid);

mergesort(a,mid+1,j);

merge(a,i,mid,mid+1,j);

void merge(int a[],int i1,int j1,int i2,int j2)

int temp[10];

int i,j,k;

i=i1;

j=i2;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

k=0;

while(i<=j1 && j<=j2)

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

temp[k++]=a[i++];

else

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

while(i<=j1)

temp[k++]=a[i++];

while(j<=j2)

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

for(i=i1,j=0;i<=j2;i++,j++)

a[i]=temp[j];

8. Output:

9. Additional Creative Inputs (If Any):


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Took input from the user.

10. Learning Outcomes:

1. I learned how to break an array into two halves.

2. I learned how to call a function again and again using recursion.

3. I lerned how to merge two arrays into a single array.

Evaluation Grid :

Sr. No. Parameters Marks Obtained Maximum Marks

1. Student Performance 12
(Conduct of experiment)
objectives/Outcomes.

2. Viva Voce 10

3. Submission of Work 8
Sheet

(Record)

Total 30

You might also like