import java.util.Scanner; public class Selection { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int n = scanner.nextInt(); int arr[] = new int[n]; System.out.println("Enter the array elements:"); for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } for (int i = 0; i < arr.length - 1; i++) { int minimum = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[minimum]) { minimum = j; } } int temp = arr[minimum]; arr[minimum] = arr[i]; arr[i] = temp; } System.out.println("Sorted array in descending order:"); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } scanner.close(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter