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

Merge Sort:: Name: Avinash Tiwari ROLL NO.: 2100290110041

The document contains code for implementing merge sort. It includes functions for merging sorted arrays and recursively sorting arrays. It takes in the size of an array, inputs elements, calls the merge sort function to sort the array, and prints out the sorted output.

Uploaded by

saurabh tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Merge Sort:: Name: Avinash Tiwari ROLL NO.: 2100290110041

The document contains code for implementing merge sort. It includes functions for merging sorted arrays and recursively sorting arrays. It takes in the size of an array, inputs elements, calls the merge sort function to sort the array, and prints out the sorted output.

Uploaded by

saurabh tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

NAME : AVINASH TIWARI

ROLL NO. : 2100290110041

MERGE SORT :
#include <stdio.h> #include
<stdlib.h>
void Merge(int arr[], int left, int mid, int right)
{ int i, j, k; int size1 = mid
- left + 1; int size2 = right -
mid; int Left[size1],
Right[size2]; for (i = 0; i <
size1; i++)
Left[i] = arr[left + i];

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


Right[j] = arr[mid + 1 + j]; i=
0;
j = 0;
k = left;
while (i < size1 && j < size2)
{
if (Left[i] <= Right[j])
{
arr[k] = Left[i];
i++; } else
{
arr[k] = Right[j];
j++;
}
k++;
}

while (i < size1)


{ arr[k] =
Left[i]; i++;
k++;
}

while (j < size2)


{
arr[k] = Right[j];
j++;

k++;
}
}
void MergeSort(int arr[], int left, int right)
{ if (left <
right)
{

int mid = (left + right )/ 2;

MergeSort(arr, left, mid);


MergeSort(arr, mid + 1, right);

Merge(arr, left, mid, right);


}
}

int main()
{ int
size;
printf("En
ter the
size\n");
scanf("%
d",
&size);

int arr[size];
printf("Enter the elements of array\n");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

MergeSort(arr, 0, size - 1);

printf("The sorted array is\n");


for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

OUTPUT :

You might also like