• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursive Call in Merge Sort

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic