No.
By adjusting the index you use to acess the list you could easily process it in ascending order if that is what you needed.
If you have a long list of integers (or any data type) and need to sort it,
use a sort()
method of the class Arrays
.
Carefully notice the "s" at the end of this class.
Here is a short demo program that shows this with integers:
import java.util.*; public class ArraysTester { public static void main ( String[] args ) { int[] values = { 23, 12, 4, 56, 10, 34, 9, 14, 4, 21, -9, 34, 78, 2, 5, 8 }; // print out the array System.out.println("Before: "); for ( int val : values ) System.out.print( val + ", " ); // sort the array Arrays.sort( values ); // print out the array System.out.println("\nAfter: "); for ( int val : values ) System.out.print( val + ", " ); System.out.println( ); } }
The Arrays
class has many methods that manipulate arrays.
It has methods to search, to copy, to copy a range, to compare arrays, and others.
In particular, it has methods to sort an array of byte
, char
, float
, double
, int
,
long
, and Object
.
The sorting methods are faster than the N2
simple sorts.
The time that Arrays.sort()
takes for an array of int
is proportional to N*log(N)
.
On my computer (five years old, home-built) Arrays.sort()
sorted an array of 500000 ints in less than a second.
Recall that it took 204 seconds with insertion sort.
Could an array of double
be sorted?