Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not.
//PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns true if csEmployees are sorted by their salaries in // ascending order, and returns false otherwise. public boolean isSortedRec(Employee[] csEmployees, int n) { } |
Solution:-
public boolean isSortedRec(Employee[] csEmployees, int
n){
if(n ==0 || n ==1 ){ // An empty array and an array with
single element in it, are sorted then it will return true.
return true;
}
else{
// Method isSortedRec returns true if csEmployees
are sorted by salaries in ascending order.
if (csEmployees.getSalary() > isSortedRec(Employee[]
csEmployees, int n-1)){ // recursive call
return true; // sorted salaries in ascending order
return ture.
}
else{
return false;
}
}
return false; // returns false otherwise.
}
======================END======================
Comment if any query.
Get Answers For Free
Most questions answered within 1 hours.