Challenge: Sort and Show
Completed
Read n and then n whole numbers. Print them sorted from smallest to largest using Arrays.toString.
Input
First n, then n whole numbers.
Output
The sorted array, e.g. [1, 2, 5, 9].
Example
Input
4 5 2 9 1
Output
[1, 2, 5, 9]
Editor
Java
Hints
import java.util.Arrays;
Arrays.sort(a) sorts the array in place.
Solution explanation
Arrays.sort sorts the array itself, then Arrays.toString prints it with brackets and commas.
Show solution
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module