I’ve written a few short recursive programs, and am now doing sorting recursively. I’ve been using 2 inputs up to now, the array, and an index. Is there a recursive method for sorting that only needs an array as input? I was thinking Bubble Sort would work for this, but that also uses an index to keep track of position.
And in case anyone wants to know, I had a HW to make a recursive sort (which I already did, using an array and index), this is just to see if its possible to do it without the index.
I believe a recursive merge sort can be performed with only an array as a parameter:
http://en.wikipedia.org/wiki/Merge_sort
Edit:
I guess you could skip the index like this:
This is not a perfect bubble-sort as the starting index is always zero. The complexity is still O(n^2). If you’re willing to clone the array (and waste a lot of memory) then you can replace
With:
Then it’s a valid, though wasteful, way of achieving a recursive bubble sort with only an array as the parameter.