C PROGRAM FOR MERGE SORT

Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquer. Merge sort repeatedly breaks down a danh sách into lớn several sublists until each subdanh sách consists of a single element and merging those sublists in a manner that results into lớn a sorted menu.

Bạn đang xem: C program for merge sort


A merge sort works as follows:

Top-down Merge Sort Implementation:

The top-down merge sort approach is the methodology which usesrecursionmechanism. It starts at the Top & proceeds downwards, with each recursive turn asking the same question such as “What is required to lớn be done khổng lồ sort the array?” and having the answer as, “split the array into lớn two, make a recursive Gọi, and merge the results.”, until one gets lớn the bottom of the array-tree.

Example: Let us consider an example khổng lồ understand the approach better.

Divide the unsorted danh mục inlớn n sublists, each comprising 1 element (a list of 1 element is supposed sorted).

Xem thêm: For You (Cinderella And Four Knights Ost Cinderella And Four Knights Full Album

*

Top-down Implementation

Repeatedly merge sublists to lớn produce newly sorted sublists until there is only 1 subdanh sách remaining. This will be the sorted list.

Merging of two lists done as follows:

The first element of both lists is compared. If sorting in ascending order, the smaller element among two becomes a new element of the sorted danh sách. This procedure is repeated until both the smaller sublists are empty & the newly combined subcác mục covers all the elements of both the sublists.

*

Merging of two lists

Implementation Of Merge Sort


// example of merge sort in C/C++// merge function take two intervals// one from start to lớn mid// second from mid+1, khổng lồ end// and merge them in sorted ordervoid merge(int *Arr, int start, int mid, int end) // create a temp arrayint temp;// crawlers for both intervals and for tempint i = start, j = mid+1, k = 0;// traverse both arrays & in each iteration add smaller of both elements in temp while(i mid &và j end) if(Arr Arr) temp = Arr;k += 1; i += 1;else temp = Arr;k += 1; j += 1;// add elements left in the first interval while(i mid) temp = Arr;k += 1; i += 1;// add elements left in the second interval while(j end) temp = Arr;k += 1; j += 1;// copy temp to lớn original intervalfor(i = start; i end; i += 1) Arr = temp// Arr is an array of integer type// start và over are the starting and ending index of current interval of Arrvoid mergeSort(int *Arr, int start, int end) if(start end) int mid = (start + end) / 2;mergeSort(Arr, start, mid);mergeSort(Arr, mid+1, end);merge(Arr, start, mid, end);
// example of merge sort in Java// merge function take two intervals// one from start to mid// second from mid+1, lớn end// và merge them in sorted ordervoid merge(int Arr<>, int start, int mid, int end) // create a temp arrayint temp<> = new int;// crawlers for both intervals & for tempint i = start, j = mid+1, k = 0;// traverse both arrays và in each iteration add smaller of both elements in temp while(i mid &và j end) if(Arr Arr) temp = Arr;k += 1; i += 1;else temp = Arr;k += 1; j += 1;// add elements left in the first interval while(i mid) temp = Arr;k += 1; i += 1;// add elements left in the second interval while(j end) temp = Arr;k += 1; j += 1;// copy temp lớn original intervalfor(i = start; i end; i += 1) Arr = temp// Arr is an array of integer type// start và over are the starting và ending index of current interval of Arrvoid mergeSort(int Arr<>, int start, int end) if(start end) int mid = (start + end) / 2;mergeSort(Arr, start, mid);mergeSort(Arr, mid+1, end);merge(Arr, start, mid, end);
# example of merge sort in Python# merge function take two intervals# one from start lớn mid# second from mid+1, to lớn end# and merge them in sorted orderdef merge(Arr, start, mid, end) :# create a temp arraytemp<> = <0> * (kết thúc - start + 1)# crawlers for both intervals & for tempi, j, k = start, mid+1, 0# traverse both lists & in each iteration add smaller of both elements in temp while(i mid and j end) :if(Arr Arr) :temp = Arrk += 1; i += 1else :temp = Arrk += 1; j += 1# add elements left in the first interval while(i mid) :temp = Arrk += 1; i += 1# add elements left in the second interval while(j end) :temp = Arrk += 1; j += 1# copy temp to lớn original intervalfor i in range (start, end+1) :Arr = temp# Arr is an array of integer type# start and end are the starting & ending index of current interval of Arrdef mergeSort(Arr, start, end) {if(start end) :mid = (start + end) / 2mergeSort(Arr, start, mid)mergeSort(Arr, mid+1, end)merge(Arr, start, mid, end)

Bottom-Up Merge Sort Implementation:

The Bottom-Up merge sort approach uses iterative methodology. It starts with the “single-element” array, and combines two adjacent elements and also sorting the two at the same time. The combined-sorted arrays are again combined and sorted with each other until one single unit of sorted array is achieved.