// Print the highest 5 numbers from an array of numbers
package abc;
import java.util.Arrays;
public class HighestNumbers {
public static void main(String[] args) {
int[] num = {5,4,6,3,23,8,1,0,44,25,50,7,9};
int numLength = num.length;
int startPoint = numLength-1 ;
int endPoint = numLength-6;
Arrays.sort(num);//To
sort Arrays in acending order
//To sort just first 5
Arrays.sort(num,1,5),
//To ReverseOrder Arrays.sort(num,1,5,Collections.reverseOrder())
//To sort in decending
order Arrays.sort(num, Collections.reverseOrder())
System.out.println("Sorted Array : "+Arrays.toString(num));//This
shows output like this= Sorted Array : [0, 1, 3, 4, 5, 6, 7, 8, 9, 23, 25, 44,
50]
int count =1;
for(int i=startPoint;i>endPoint;i--)
{
System.out.println("Top 5 values_"+count+" "+num[i]);
count++;
}
}
}
OutPut:
Sorted Array : [0, 1, 3, 4, 5, 6, 7,
8, 9, 23, 25, 44, 50]
Top 5 values_1 50
Top 5 values_2 44
Top 5 values_3 25
Top 5 values_4 23
Top 5 values_5 9
No comments:
Post a Comment