File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 18101
    
  39


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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursive Call in Merge Sort
 
Similar Threads
a question in stacks
Help with using mergesort to sort a list of names alphabetically?
recursive problem
Merge sort failure
JTable question....again!