In: Computer Science
/* Using Recursive approach for in this program */
import java.util.Scanner;
class SortTest{
static int array(int arr[], int n)
{
/* escape the recursion by checking
Array has one or no element rest are checked already.*/
if (n == 1 || n == 0)
return 1;
// Checking the values in
increasing order
if (arr[n - 1] < arr[n -
2])
return 0;
// untill the Last pair was
sorted Keep on checking
return array(arr, n - 1);
}
public static void main(String args[]){
// initialize the variables.
int n, i, j, temp;
// declare the scanner class for reading input from
the user.
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of integers:");
n = sc.nextInt();
int array[] = new int[n];
for (i = 0; i < n; i++)
array[i] = sc.nextInt();
if (array(array, n) != 0)
System.out.println("The list is already sorted");
else
System.out.println("The list is not sorted");
}
}
************Request for positive feedback pls..************