Results 1 to 3 of 3
Thread: Recursive method to sort array
- 03-19-2012, 01:48 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
Recursive method to sort array
Hi. I have to write a method to sort an array of objects alphabetically. The method has to :
1) Determine largest item in the list.
2) Swap largest item in the list with the last item in the list.
3) Call method recursively to sort the sublist of size n-1.
My code, finds the largest value and swaps it with the last value. But when I add recursive call the program gives me the stackoverflowerror null.
Here is my code.
Thank you for comments.Java Code:public static void selectionSortRecursive(Comparable[] list, int n){ if(n <= 1){} else { Comparable temp; Comparable largest = list[0]; int index=0; for(int i = 0; i < n; i++){ if(largest.compareTo(list[i]) < 0){ largest = list[i]; index = i; } } temp = list[n-1]; list[n-1] = list[index]; list[index] = temp; } selectionSortRecursive(list,n-1); }
- 03-19-2012, 12:35 PM #2
Re: Recursive method to sort array
Try debugging the code by adding some println statements to see why it doesn't stop recursive calling.
If you don't understand my response, don't ignore it, ask a question.
- 03-19-2012, 12:49 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
merge sort with recursive method (need help badlly!!)
By zetalore in forum Advanced JavaReplies: 0Last Post: 01-08-2011, 07:10 PM -
Array out of bound- Recursive Method
By hpayandah in forum New To JavaReplies: 2Last Post: 11-12-2010, 08:02 PM -
Recursive method using int array, help needed
By chupalo17 in forum New To JavaReplies: 4Last Post: 09-07-2009, 11:15 PM -
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02-16-2008, 09:10 PM -
Recursive Method ==> find how many times a value is repeated in an array
By NatNat in forum New To JavaReplies: 2Last Post: 02-16-2008, 08:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks