java.util.Arrays sort(float[] a, int fromIndex, int toIndex)
Description
The < relation does not provide a total order on all float values: -0.0f == 0.0f is true and a Float.NaN value compares neither less than, greater than, nor equal to any value, even itself. This method uses the total order imposed by the method Float.compareTo(java.lang.Float): -0.0f is treated as less than value 0.0f and Float.NaN is considered greater than any other value and all Float.NaN values are considered equal.
Notes:
- The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
- This method throws IllegalArgumentException if fromIndex is greather than toIndex
- This method will also throws an ArrayIndexOutOfBoundsException if fromIndex is less than 0 or toIndex is greater than a.length
- This method will yield the same result as sort(float[] a) method on such cases that the fromIndex is 0 and endIndex is equal to length. In short Arrays.sort(float[] a) is equals to Arrays.sort(float[] a, 0,a.length).
Method Syntax
public static void sort(float[] a, int fromIndex, int toIndex)
Method Argument
Data Type | Parameter | Description |
---|---|---|
float[] | a | the array to be sorted |
int | fromIndex | the index of the first element, inclusive, to be sorted |
int | toIndex | the index of the first element, inclusive, to be sorted |
Method Returns
The sort(float[] a, int fromIndex, int toIndex) method returns void.
Compatibility
Requires Java 1.2 and up
Java Arrays sort(float[] a, int fromIndex, int toIndex) Example
Below is a java code demonstrates the use of sort(float[] a, int fromIndex, int toIndex) method of Arrays class. The example presented might be simple however it shows the behavior of the sort(float[] a, int fromIndex, int toIndex) method.
package com.javatutorialhq.java.examples; import java.util.Arrays; /* * A java example source code to demonstrate * the use of sort(float[] a, int fromIndex, int toIndex) * method of Arrays class */ public class ArraysSortFloatRangeExample { public static void main(String[] args) { // initialize a new float array float[] a = new float[]{1.12f,7.32f,3.21f,1.07f,9.2f,1.7f,10.12f}; // print the contents of array of float System.out.println("Original:"+Arrays.toString(a)); // sort our array of float from index 1 to 7 Arrays.sort(a,1,7); // print the sorted array of float System.out.println("Sorted:"+Arrays.toString(a)); } }
The above java example source code demonstrates the use of sort(float[] a, int fromIndex, int toIndex) method of Arrays class. We simply declare a new double array and we printed the values using the toString() method of Arrays class. Then we sorted the float array from index 1 to 7 using the sort() method and the sorted values were also printed the same way as what we did on the original/unsorted array.