Write a program to sort a given array
(Passing Array to a Method in Java- get an array from user input
Returning Array from the Method-sort and then return the sorted array)
import java.util.Scanner; public class SortClass { public int Ascending(int [] myarray) { int n=0,temp; int a[] = new int[n]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] < a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } System.out.println("Array elements in descending order:"); for ( i=n; i < n - 1; i--) { System.out.println(a[i]); } return (a[n - 1]); } } public static void main(String[] args) { int n, temp; Scanner s = new Scanner(System.in); SortClass SC=new SortClass(); System.out.print("Enter the number of elements: "); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter the elements of the array: "); for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } for (int i = 0; i < n; i++) { SC.Ascending(a); } System.out.println("Array elements in Ascending order:"); System.out.println("Array elements in Ascending order:"+SC.Ascending(a)); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] < a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } System.out.println("Array elements in descending order:"); for (int i = 0; i < n - 1; i++) { System.out.println(a[i]); } System.out.print(a[n - 1]); } }
Error
--- exec-maven-plugin:3.0.0:exec (default-cli) @ mavenproject1 ---
Enter the number of elements: 4
Enter the elements of the array:
50
6
70
2
Exception in thread "main" java.lang.RuntimeException: Uncompilable code - missing return statement
at SortClass.Ascending(SortClass.java:1)
at SortClass.main(SortClass.java:56)
Command execution failed.
Thanks in advance.