Results 1 to 9 of 9
 01152011, 11:51 AM #1Member
 Join Date
 Dec 2010
 Posts
 21
 Rep Power
 0
Recursive method that checks if the first n elements of an array is sorted?
Hi guys,
I'm trying to write a recursive method that checks if the first elements of an array of integers are sorted in ascending order but it didn't work for me !!
Java Code:public static Boolean isSorted(int [] a, int n){}
I tried to copy elements from array a with length n into another array b then , compare every two elements starting from b[0] and b[1] such that :
Java Code:if (b[0] < b[1] ) // that means those elements are in ascending order { return true; }
but I couldn't complete it ,, could anyone please help me to write the rest of the code :(
 01152011, 12:21 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,290
 Blog Entries
 7
 Rep Power
 24
There is no need to copy elements over to another array. Just assume that elements 0 ... i1 are in sorted order and assume the length of the array is n. If i < n1 and elements i1 and i are in sorted order call your method recursively for the next elements. Otherwise if i == n1 the entire array is sorted, otherwise the array isn't sorted.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 01152011, 08:58 PM #3Member
 Join Date
 Dec 2010
 Posts
 21
 Rep Power
 0
How can I do it recursively for the next elements without copying the array into array of length n ?
 01152011, 09:47 PM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 10
add an instance variable:
Java Code:public boolean isSorted(int[] a, int n)
Java Code:if(a[n] > a[n + 1]) { return false; }
Java Code:return isSorted(a, n + 1)
 01152011, 09:53 PM #5Member
 Join Date
 Dec 2010
 Posts
 21
 Rep Power
 0
I need to check if the first n integers are in ascending order , but you said iSorted(a,n+1) !!
How is that ?
 01152011, 10:00 PM #6Member
 Join Date
 Dec 2010
 Posts
 21
 Rep Power
 0
it worked ! but I don't know how !!
I tried many arrays and it's absolutely working 100% !!
if (a[n] > a[n1] ){
return false & isSorted(a,n1);
}
else{
return true;
}
 01152011, 10:02 PM #7
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 10
sorry, adding a 3rd variable would probable be necessary, which would basically allow you to choose a range.
Java Code:isSorted(int[] a, int i, int n)
Java Code:isSorted(a, 0, 5)
alternately you could just take
Java Code:isSorted(int[] a, n)
Java Code:if(a[i] < a[i+1]) { return false; }
 01152011, 10:14 PM #8Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 7
Are you sure you want Boolean and not boolean there? Are you going to be storing these results in a List or something?
Like Jos said, you don't need to do any array copying, and you don't need an instance variable as sunde suggested either.
Base case: n <= 1 : return true.
Other cases: if a[n2] is greater than a[n1] return false, otherwise return isSorted(a, n1).
In other words, if n is 10, I want to see if a[9] (the 10th element) and a[8] (the 9th element) are in proper order. If they're not, I should return false. If they are, then I want to call a method that checks if the first 9 elements are in order, and that method happens to be another instance of myself, with n reduced by one.
Gary
 01152011, 10:39 PM #9Member
 Join Date
 Dec 2010
 Posts
 21
 Rep Power
 0
Similar Threads

Array out of bound Recursive Method
By hpayandah in forum New To JavaReplies: 2Last Post: 11122010, 09:02 PM 
Recursive method using int array, help needed
By chupalo17 in forum New To JavaReplies: 4Last Post: 09072009, 11:15 PM 
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04222008, 03:21 AM 
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02162008, 10:10 PM 
Recursive Method ==> find how many times a value is repeated in an array
By NatNat in forum New To JavaReplies: 2Last Post: 02162008, 09:52 PM
Bookmarks