How do I sort an array in Java?
I’m working on a Java program that generates an array of 10 integers, each assigned a random value. So far, everything is working fine.
Now, I need to sort the array in ascending order and print the sorted values to the screen. What is the best way to use Java sort array methods to accomplish this?
Here’s my current code:
public static void main(String args[]) {
int [] array = new int[10];
array[0] = ((int)(Math.random()*100+1));
array[1] = ((int)(Math.random()*100+1));
array[2] = ((int)(Math.random()*100+1));
array[3] = ((int)(Math.random()*100+1));
array[4] = ((int)(Math.random()*100+1));
array[5] = ((int)(Math.random()*100+1));
array[6] = ((int)(Math.random()*100+1));
array[7] = ((int)(Math.random()*100+1));
array[8] = ((int)(Math.random()*100+1));
array[9] = ((int)(Math.random()*100+1));
System.out.println(array[0] +" " + array[1] +" " + array[2] +" " + array[3]
+" " + array[4] +" " + array[5]+" " + array[6]+" " + array[7]+" "
+ array[8]+" " + array[9]);
}
How can I modify this to sort the array and print the sorted values?
If you just need to quickly sort the array, Java sort array operations are super easy with Arrays.sort()
. No need to overcomplicate things. Here’s how you can do it:
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
int[] array = new int[10];
// Populate array with random numbers
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 100 + 1);
}
// Sort the array
Arrays.sort(array);
// Print sorted array
System.out.println(Arrays.toString(array));
}
}
Why this works well?
- Uses Java’s optimized dual-pivot QuickSort for primitives
- Clean and readable
- Just one function call to sort the array
If you just need the sorted array and don’t care about implementation details, this is your best bet.
Okay, let’s say you want something more modern and functional—maybe you’re already using Java 8+ and love working with streams.
Here’s how you can use Arrays.stream()
to achieve the same result in a more declarative way:
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
int[] array = new int[10];
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 100 + 1);
}
// Print sorted array using streams
Arrays.stream(array)
.sorted()
.forEach(num -> System.out.print(num + " "));
}
}
Why use streams?
-
More expressive than traditional loops
- Can be parallelized for large datasets (though not needed for small arrays)
- Works well in functional programming paradigms
If you like writing clean, modern Java code and prefer chaining operations, this is a great way to sort arrays.
Alright, but what if you want to implement sorting yourself instead of using Java’s built-in sorting? Maybe for learning purposes, interview prep, or just for fun?
Here’s how you can manually implement Bubble Sort, one of the simplest sorting algorithms:
public class Main {
public static void main(String args[]) {
int[] array = new int[10];
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 100 + 1);
}
// Bubble sort implementation
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
// Print sorted array
for (int num : array) {
System.out.print(num + " ");
}
}
}
Why manually sort when Java has built-in sorting?
- Good for understanding sorting algorithms
- Sometimes needed for interview questions
- Helps optimize for specific use cases where built-in sorting might not be ideal
Of course, for real-world applications, you’d stick to Arrays.sort()
or streams, but it’s always good to know how sorting actually works under the hood!