Give a complete pseudo-code description of the recursive merge-sort algorithm th
ID: 3910205 • Letter: G
Question
Give a complete pseudo-code description of the recursive merge-sort algorithm that takes an array as its input and output.
Explanation / Answer
Algorithm MergeSort(Array[] , n) START //If Array contains only One element then Just return the Array because its Sorted if ( n == 1 ) return Array //Divide the Array into Two Subarray with 0 to n/2 and left[] as Array = Array[0] ... Array[n/2] //and n/2 to n elements and right[] as array = Array[n/2+1] ... Array[n] //Call Merge Sort on left SubArray left = Mergesort( left ) //Call Merge Sort on right SubArray right = Mergesort( right ) //Merge The Sorted LEFT AND RIGHT Array return Merge( left, right ) END Algorithm Merge( LEFT[] as Array, Right[] as Array ) START //Our Result Array result[] as Array //Iterate through the Left and Right Array and Merge them in Ascending Order while ( left and right have elements and NOT EMPTY ) //Right is Smller than Left So, In the result right will be added if ( left[0] > right[0] ) add right[0] to the end of result remove right[0] from right else //Otherwise Right is Larger than Left So, In the result left will be added add left[0] to the end of result remove left[0] from left end if end while //If LEFT Array has Still Some elements left then JUST merge it to Result Array while ( left has elements ) add left[0] to the end of result remove left[0] from left end while //If RIGHT Array has Still Some elements left then JUST merge it to Result Array while ( right has elements ) add right[0] to the end of result remove right[0] from right end while return result END Thanks, PLEASE UPVOTE if helpful. Let me know your doubts/concern.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.