I have a program here that calculated the median of a certain amount of elements entered by a user; however I want to display the sorted list at the end of the program. You can see that I have sorted the elements but I can't think of any ways to display the sorted array at the end. Thanks in advance.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How many numbers do you want a median of?");
int num = sc.nextInt();
System.out.println("Enter the numbers: ");
int [] array = new int [num];
for (int i = 0 ; i < array.length; i++ ) {
array[i] = sc.nextInt();
}
int median = median(array);
System.out.println(median+" is a median of entered numbers.");
}
public static int median (int [] array) {
int n = array.length;
int temp;
for (int i=0; i < n-1; i++) {
for(int j=1; j < n-i; j++) {
if (array [j-1] > array [j]) {
temp = array [j-1];
array [j-1] = array [j];
array [j] = temp;
}
}
}
int median;
if (n % 2 == 0) {
median = (array [n/2-1]);
}
else {
median = array [(n/2)];
}
return median;
}
}