Collections

Java Arrays sort() Code With Example

Table of Contents

Program

import java.util.Arrays;
public class Sort
{
	public static void main(String[] args) 
		{
		byte [] b = new byte [] {1,8,9,3,6,};
		char [] c = new char [] {'a','t','d','e','n'};
		float [] f = new float [] {(float) 8.2,(float) 6.8,(float) 4.9,(float) 2.9};
		double [] d = new double [] {923,634,845,267,786};
		int [] i = new int [] {82,74,96,68,10};
		long [] l = new long [] {122l,320l,127l,222l};
		short [] s = new short [] {1220,1000,400,2000};
		System.out.println("value of byte :" +Arrays.toString( b));
		Arrays.sort(b);
		System.out.println("sorted value of byte " +Arrays.toString(b));
		Arrays.sort(b,1,3);
		System.out.println("sorted value of byte with in index :" +Arrays.toString(b));
		System.out.println("value of char :" +Arrays.toString( c));
		Arrays.sort(c);
		System.out.println("sorted value of char " +Arrays.toString(c));
		Arrays.sort(c,1,3);
		System.out.println("sorted value of char with in index :" +Arrays.toString(c));
		System.out.println("value of float :" +Arrays.toString( f));
		Arrays.sort(f);
		System.out.println("sorted value of float " +Arrays.toString(f));
		Arrays.sort(f,1,3);
		System.out.println("sorted value of float with in index :" +Arrays.toString(f));
		System.out.println("value of double :" +Arrays.toString( d));
		Arrays.sort(d);
		System.out.println("sorted value of double " +Arrays.toString(d));
		Arrays.sort(d,1,3);
		System.out.println("sorted value of double with in index :" +Arrays.toString(d));
		System.out.println("value of int :" +Arrays.toString( i));
		Arrays.sort(i);
		System.out.println("sorted value of int " +Arrays.toString(i));
		Arrays.sort(i,1,3);
		System.out.println("sorted value of int with in index :" +Arrays.toString(i));
		System.out.println("value of long :" +Arrays.toString( l));
		Arrays.sort(l);
		System.out.println("sorted value of long " +Arrays.toString(l));
		Arrays.sort(l,1,3);
		System.out.println("sorted value of long with in index :" +Arrays.toString(l));
		System.out.println("value of short :" +Arrays.toString( s));
		Arrays.sort(s);
		System.out.println("sorted value of short " +Arrays.toString(s));
		Arrays.sort(s,1,3);
		System.out.println("sorted value of short with in index :" +Arrays.toString(s));
		
		
	}

}

Output

	sorted value of int [10, 68, 74, 82, 96]
	sorted value of int with in index :[10, 68, 74, 82, 96]
	value of long :[122, 320, 127, 222]
	sorted value of long [122, 127, 222, 320]
	sorted value of long with in index :[122, 127, 222, 320]
	value of short :[1220, 1000, 400, 2000]
	sorted value of short [400, 1000, 1220, 2000]
	sorted value of short with in index :[400, 1000, 1220, 2000]

Description

public static void sort​(byte[] a)

Sorts the specified array into ascending numerical order.

Implementation note:

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.

Parameters:

a – the array to be sorted

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

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, therange to be sorted is empty.

Implementation note:

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) Quicsort implementations.

Parameters:

a – the array to be sorted
fromIndex – the index of the first element, inclusive, to be sorted
toIndex – the index of the last element, exclusive, to be sorted

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException – if fromIndex < 0 or toIndex > a.length

public static void sort​(char[] a)

Sorts the specified array into ascending numerical order.

Implementation note:

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.

Parameters:

a – the array to be sorted

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

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.

Implementation note:

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.

Parameters:

a – the array to be sorted
fromIndex – the index of the first element, inclusive, to be sorted
toIndex – the index of the last element, exclusive, to be sorted

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException – if fromIndex < 0 or toIndex > a.length

public static void sort​(float[] a)

Sorts the specified array into ascending numerical order.
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.

Implementation note:

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.

Parameters:

a – the array to be sorted

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

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 < 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.

Implementation note:

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.

Parameters:

a – the array to be sorted
fromIndex – the index of the first element, inclusive, to be sorted
toIndex – the index of the last element, exclusive, to be sorted

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException – if fromIndex < 0 or toIndex > a.length

public static void sort​(double[] a)

Sorts the specified array into ascending numerical order.
The < relation does not provide a total order on all double values: -0.0d == 0.0d is true and a Double.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 Double.compareTo(java.lang.Double): -0.0d is treated as less than value 0.0d and Double.NaN is considered greater than any other value and all Double.NaN values are considered equal.

Implementation note:

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.

Parameters:

a – the array to be sorted

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

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 < relation does not provide a total order on all double values: -0.0d == 0.0d is true and a Double.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 Double.compareTo(java.lang.Double): -0.0d is treated as less than value 0.0d and Double.NaN is considered greater than any other value and all Double.NaN values are considered equal.

Implementation note:

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.

Parameters:

a – the array to be sorted
fromIndex – the index of the first element, inclusive, to be sorted
toIndex – the index of the last element, exclusive, to be sorted

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException – if fromIndex < 0 or toIndex > a.length

public static void sort​(int[] a)

Sorts the specified array into ascending numerical order.

Implementation note:

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.

Parameters:

a – the array to be sorted

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

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.

Implementation note:

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.

Parameters:

a – the array to be sorted
fromIndex – the index of the first element, inclusive, to be sorted
toIndex – the index of the last element, exclusive, to be sorted

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException – if fromIndex < 0 or toIndex > a.length

public static void sort​(long[] a)

Sorts the specified array into ascending numerical order.

Implementation note:

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.

Parameters:

a – the array to be sorted

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

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.

Implementation note:

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.

Parameters:

a – the array to be sorted
fromIndex – the index of the first element, inclusive, to be sorted
toIndex – the index of the last element, exclusive, to be sorted

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException – if fromIndex < 0 or toIndex > a.length

public static void sort​(short[] a)

Sorts the specified array into ascending numerical order.

Implementation note:

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.

Parameters:

a – the array to be sorted

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

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.

Implementation note:

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.

Parameters:

a – the array to be sorted
fromIndex – the index of the first element, inclusive, to be sorted
toIndex – the index of the last element, exclusive, to be sorted

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException – if fromIndex < 0 or toIndex > a.length