Reputation: 1
I have an array which consists of 10 random integers, what will be the fastest way to sort it from the largest to the smallest?
public class Main {
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));
for (int i = 0; i < 10; i++){
System.out.println(i + ") " + array[i]);
}
}
}
The only thing that comes to mind is bubble sort and the insertion sort
Upvotes: 0
Views: 238
Reputation: 199215
For ascending order it would be as easy as:
Unfortunately due to the unboxing feature, it's a bit more complicated but still doable.
array = Arrays.stream(array) // turn into an IntStream
.boxed() // Then into a Stream<Integer>
.sorted((a,b) -> b - a) // Sort descending
.mapToInt(i -> i) // Convert Integer back to int
.toArray(); // Convert back to int[]
Upvotes: 1
Reputation:
You can flip the bits of integers to sort in ascending order, then flip the bits again to sort in descending order.
array = IntStream.of(array).map(i -> ~i).sorted().map(i -> ~i).toArray();
for (int i = 0; i < 10; i++) {
System.out.println(i + ") " + array[i]);
}
output:
0) 100
1) 92
2) 85
3) 77
4) 64
5) 62
6) 44
7) 37
8) 22
9) 9
Since everything is positive in this case, you can do the same thing with sign reversal instead of bit reversal.
array = IntStream.of(array).map(i -> -i).sorted().map(i -> -i).toArray();
Note that numbers containing Integer.MIN_VALUE
can't be sorted this way, because they are themselves when they are sign-reversed.
Upvotes: 0
Reputation: 9192
Although there are several ways to do this, this is one way. Read the comments in code:
int [] array = new int[10];
/* Generate an int[] Array with 10 elements consisting
of 10 elements holding a random number from 1 to 100
(duplicates allowed). */
System.out.println("Generated Array (Random Order):");
// Display the generated Array...
for (int i = 0; i < array.length; i++){
array[i] = ((int)(Math.random()*100+1));
System.out.printf("%2d) %-6d%n", (i+1), array[i]);
}
System.out.println();
// Sort the Array in Ascending Order 'first'!
System.out.println("Sorted Array (Ascending Order):");
Arrays.sort(array); // Sort in ascending order
// Display the sorted Array...
for (int i = 0; i < array.length; i++){
System.out.printf("%2d) %-6d%n",(i+1), array[i]);
}
System.out.println();
// Sort the Array in Ddescending Order 'second'!
System.out.println("Sorted Array (Descending Order):");
// Create a new int[] Array (arr[]) and place array[] into it in Descending order.
int[] arr = IntStream.rangeClosed(1, array.length).map(i -> array[array.length - i]).toArray();
// Copy the arr[] array into the array[] Array to overwrite it.
System.arraycopy(arr, 0, array, 0, arr.length);
// Display the sorted Array...
for (int i = 0; i < arr.length; i++){
System.out.printf("%2d) %-6d%n",(i+1), array[i]);
}
When the code is run, you could see something similar to this:
Generated Array (Random Order):
1) 80
2) 54
3) 100
4) 96
5) 71
6) 87
7) 22
8) 60
9) 67
10) 18
Sorted Array (Ascending Order):
1) 18
2) 22
3) 54
4) 60
5) 67
6) 71
7) 80
8) 87
9) 96
10) 100
Sorted Array (Descending Order):
1) 100
2) 96
3) 87
4) 80
5) 71
6) 67
7) 60
8) 54
9) 22
10) 18
Upvotes: 0