File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursive Call in Merge Sort" Watch "Recursive Call in Merge Sort" New topic

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.


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.

Henry Wong

Joined: Sep 28, 2004
Posts: 20373

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
wood burning stoves
subject: Recursive Call in Merge Sort
It's not a secret anymore!