Exp 8 - NLL
Exp 8 - NLL
Name of Student:
Marks: Sign:
Conquer means sorting the two sub-arrays recursively using merge sort.
Combine means merging the two sorted sub-arrays of size n/2 to produce the sorted array of n
elements.
Merge sort algorithm focuses on two main concepts to improve its performance (running time):
● A smaller list takes fewer steps and thus less time to sort than a large list.
● As number of steps is relatively less, thus less time is needed to create a sorted list from two
sorted lists rather than creating it using two unsorted lists.
The merge sort algorithm uses a function Merge which combines the sub-arrays to form a sorted
array. While the merge sort algorithm recursively divides the list into smaller lists, the merge
algorithm conquers the list to sort the elements in individual lists. Finally, the smaller lists are merged
to form one list.
Its worst-case running time has a lower order of growth than insertion sort.
To understand the merge algorithm, consider the figure below which shows how we merge two lists
to form one list. For ease of understanding, we have taken two sub-lists each containing four elements.
The same concept can be utilized to merge four sub-lists containing two elements, or eight sub-lists
having one element each.
Compare ARR[I] and ARR[J], the smaller of the two is placed in TEMP at the location specified by
INDEX and subsequently the value I or J is incremented.
When I is greater than MID, copy the remaining elements of the right sub-array in TEMP.
The running time of merge sort in the average case and the worst case can be given as O(n log n).
Although merge sort has an optimal time complexity, it needs an additional space of O(n) for the
temporary array TEMP.
CODE:
#include <stdio.h>
#include <conio.h>
#define size 100
void main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i= 0;i < n; i++)
{
scanf("%d", &arr[i]);
}
merge_sort(arr, 0, n-1);
printf("\n The sorted array is: \n");
for(i = 0; i < n; i++)
printf(" %d\t", arr[i]);
getch();
}
for(k=beg;k<index;k++)
arr[k] = temp[k];
}
CONCLUSION:
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________