Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MERGE SORT

Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It works by
recursively dividing the input array into smaller subarrays and sorting those subarrays then merging
them back together to obtain the sorted array.

In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort
each half, and then merge the sorted halves back together. This process is repeated until the entire
array is sorted. Example

Merge sort is the sorting technique that follows the divide and conquer approach.
Merge sort is similar to the quick sort algorithm as it uses the divide and conquer
approach to sort the elements. It is one of the most popular and efficient sorting
algorithm. It divides the given list into two equal halves, calls itself for the two halves
and then merges the two sorted halves. We have to define the merge() function to
perform the merging.

The sub-lists are divided again and again into halves until the list cannot be divided
further. Then we combine the pair of one element lists into two-element lists, sorting
them in the process. The sorted two-element pairs is merged into the four-element
lists, and so on until we get the sorted list.

Merge sort algorithm.


arr is the given array, beg is the starting element, and end is the last element of the
array.

MERGE_SORT(arr, beg, end)

if beg < end


set mid = (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
end of if

END MERGE_SORT

The important part of the merge sort is the MERGE function. This function performs
the merging of two sorted sub-arrays that are A[beg…mid] and A[mid+1…end], to
build one sorted array A[beg…end]. So, the inputs of the MERGE function are A[],
beg, mid, and end.

PROGRAM:
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high){
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high){
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
} }
int main(){
int i;
printf("Array before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nArray after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
OUTPUT:

Array before sorting

10 14 19 26 27 31 33 35 42 44 0

Array after sorting

0 10 14 19 26 27 31 33 35 42 44

Advantages of Merge Sort:

 Stability: Merge sort is a stable sorting algorithm, which means it maintains the relative
order of equal elements in the input array.
 Guaranteed worst-case performance: Merge sort has a worst-case time complexity of O(N
logN), which means it performs well even on large datasets.
 Simple to implement: The divide-and-conquer approach is straightforward.

Disadvantage of Merge Sort:

 Space complexity: Merge sort requires additional memory to store the merged sub-arrays
during the sorting process.
 Not in-place: Merge sort is not an in-place sorting algorithm, which means it requires
additional memory to store the sorted data. This can be a disadvantage in applications
where memory usage is a concern.

Applications of Merge Sort:

 Sorting large datasets


 External sorting (when the dataset is too large to fit in memory)
 Inversion counting (counting the number of inversions in an array)
 Finding the median of an array
QUICK SORT

Algorithm QSORT (m,n)

Example:

To implement the Quicksort algorithm in a programming language, we need:

1. An array with values to sort.


2. A quickSort method that calls itself (recursion) if the sub-array has a
size larger than 1.
3. A partition method that receives a sub-array, moves values around,
swaps the pivot element into the sub-array and returns the index
where the next split in sub-arrays happens.

def partition(array, low, high):

pivot = array[high]
i = low - 1

for j in range(low, high):

if array[j] <= pivot:

i += 1

array[i], array[j] = array[j], array[i]

array[i+1], array[high] = array[high], array[i+1]

return i+1

def quicksort(array, low=0, high=None):

if high is None:

high = len(array) - 1

if low < high:

pivot_index = partition(array, low, high)

quicksort(array, low, pivot_index-1)

quicksort(array, pivot_index+1, high)

my_array = [64, 34, 25, 12, 22, 11, 90, 5]

quicksort(my_array)

print("Sorted array:", my_array)

You might also like