java.util.Arrays sort(byte[] a, int fromIndex, int toIndex)

Description

On this document we will be showing a java example on how to use the sort(byte[] a, int fromIndex, int toIndex) method of Arrays Class. Basically thes ort(byte[] a, int fromIndex, int toIndex) method sorts the specified range of the array into ascending order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. The sort() method is overloaded in such a way that each primitive data type is handled. On this example we will be showing the sorting of bytes at specified index range.

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(byte[] a) method on such cases that the fromIndex is 0 and endIndex is equal to length. In short Arrays.sort(byte[] a) is equals to Arrays.sort(char[] a, 0,a.length).

Method Syntax

public static void sort(byte[] a, int fromIndex, int toIndex)

Method Argument

Data Type Parameter Description
byte[] 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(byte[] a) method returns void.

Compatibility

Requires Java 1.2 and up

Java Arrayssort(byte[] a, int fromIndex, int toIndex) Example

Below is a java code demonstrates the use of sort(byte[] a, int fromIndex, int toIndex) method of Arrays class. The example presented might be simple however it shows the behavior of the sort(byte[] 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(byte[] a, int fromIndex, int toIndex) 
 * method of Arrays class
 */

public class ArraysSortByteRangeExample {

	public static void main(String[] args) {

		// initialize a new String array
		byte[] a = new byte[]{1,7,3,9,0,11,2,4,-1};
		
		// print the contents of array of bytes
		System.out.println("Original:"+Arrays.toString(a));
		
		// sort our array of byte from index 3,6 
		Arrays.sort(a,3,6);
		
		// print the sorted array of bytes
		System.out.println("Sorted:"+Arrays.toString(a));

	}
}

The above java example source code demonstrates the use of sort(byte[] a, int fromIndex, int toIndex) method of Arrays class. We simply declare a new byte Array and we printed the values using the toString() method of Arrays class. Then we sorted the byte array from index 3 to 6 using the sort() method and the sorted values were also printed the same way as what we did on the original/unsorted array.

Sample Output

Below is the sample output when you run the above example.

Arrays sort(byte[] a, int fromIndex, int toIndex) example output