wood burning stoves 2.0*
The moose likes Java in General and the fly likes Recursive Call in Merge Sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursive Call in Merge Sort" Watch "Recursive Call in Merge Sort" New topic
Author

Recursive Call in Merge Sort

Vid Srini
Ranch Hand

Joined: Aug 17, 2006
Posts: 35
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
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Consider Paul's rocket mass heater.
 
subject: Recursive Call in Merge Sort