The algorithm looks like it's sorting the array in descending order.
But for some reason, even though the displayed numbers are the biggest five. It fails testing?
Is the algorithm wrong? or maybe something else?
Kindly let me have your answers.
Thank you
package com.codegym.task.task08.task0826;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Five winners
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] array = new int[10];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(reader.readLine());
}
sort(array);
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
}
public static void sort(int[] array) {
//write your code here
int temp;
for (int i = 0; i < array.length; i++) {
for (int j = i +1; j < array.length -1; j++) {
if (array[i] < array[j+1]) {
temp = array[i];
array[i] = array[j+1];
array[j+1] = temp;
}
}
}
}
}