• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursive Call in Merge Sort

 
Vid Srini
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt in recursive call. This is the merge sort algorithm.

1. dividing the array in to 2 parts. Sorting them separateley.
2. merging left array and right array

if (low < high) {
middle = low+high/2;
mergesort(low, middle); // Call -1
mergesort(middle + 1, high); // Call - 2
merge(low, middle, high)
}
My input array is
int UnsortedArray [] = {50,10,80,90,20};

when first recursive mergeSort() method is called, it iterates 3
times for the left side of the array.

(0,4)
(0,2)
(0,1)

when low and high values decreases to (0,0) - if loop condition fails,
execution of the mergesort() terminates.

My query is how does the other 2 methods inside the if loop gets
triggered. Are they called separately by different threads ... Any
help would be appreciated.

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Calling a method recursively is no different than calling any other method -- except it looks weird to beginners because they don't expect for a method to be able to call itself.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic