Muhaiminur
2/12/2018 - 11:17 PM

Array Soritn High to Low

Write a program which reads 5 numbers into an array, sorts/arranges the numbers from high to low and prints all numbers in the array. If the user enters 7, 13, 2, 10, 6 then your program should print 13, 10, 7, 6, 2.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BASICJAVA;

import java.util.Scanner;

/**
 *
 * @author ABIR
 */
public class Array_Sorting_High_To_High {
    public static void main(String[]args){
        Scanner abir =new Scanner(System.in);
        int[] number=new int[5];
        for (int i = 0; i < number.length; i++) {
            System.out.println("Enter your "+(i+1)+" number");
            number[i]=abir.nextInt();
        }
        for (int i = 0; i < number.length; i++) {
            //saving the number as minimum
            int max=number[i];
            int maxindex=i;
            for (int j = i+1; j < number.length; j++) {
                if(number[j]>max){
                    max=number[j];
                    maxindex=j;
                }
            }
            int backup=number[i];
            number[i]=number[maxindex];
            number[maxindex]=backup;
        }
        System.out.println("After Sorting");
        for (int i = 0; i < number.length; i++) {
            System.out.println(number[i]);
        }
    }
}