Java – double[] Arrays Sorting with example

In this article, we will discuss how to sort double[] array with example

1. double:

  • Size is 8 bytes
  • That’s is 64 bits
  • Its range is 1.7e−308 to 1.7e+038

Arrays class has various sort methods for sorting different primitive data-types

2. Sorting double[] Arrays:

  • To sort double[] array, we have 2 variant of sort methods from Arrays class

Method Signature:

public static void sort(double[] a);

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

3. Sorting method for double[] Arrays :

Sort method

Description

sort(double[]);sorts complete double[] array
sort(dbl[], sIndex, eIndex);sorts partial double[] array, as per limits start-index & end-index specified in the method arguments

Let us move forward to discuss both methods for sorting double[] arrays

4. Example for Sorting double[] Arrays:

  1. Complete/Full sorting of double[] Arrays
  2. Partial sorting of double[] Arrays

4.1 Complete/Full sorting of double[] Arrays

  • Here, complete array will be sorted

Method Signature:

public static void sort(double[] a);

SortingCompleteDoubleArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingCompleteDoubleArray {

	public static void main(String[] args) {

		// sample double[] array
		double[] dblArray = {
				90.9999,
				10.0101,
				80.7525,
				100.1278,
				12.1123,
				70.3034,
				30.2356
		};

		// before sorting
		System.out.println("Before sorting : ");
		for(double dblValue : dblArray) {
			System.out.print("|" + dblValue + "|");
		}

		// sorting full double[] array
		Arrays.sort(dblArray);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(double dblValue : dblArray) {
			System.out.print("|" + dblValue + "|");
		}
	}
}

Output:

Before sorting :
|90.9999||10.0101||80.7525||100.1278||12.1123||70.3034||30.2356|

After sorting :
|10.0101||12.1123||30.2356||70.3034||80.7525||90.9999||100.1278|

4.2 Partial Soritng of double[] Arrays

  • This is the another variant to sort array
  • where we can specify start & end limits within double[] array

Method Signature:

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

SortingPartialDoubleArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingPartialDoubleArray {

	public static void main(String[] args) {

		// sample double[] array
		double[] dblArray = {
				90.9999,
				10.0101,
				80.7525,
				100.1278,
				12.1123,
				70.3034,
				30.2356
		};

		// before sorting
		System.out.println("Before sorting : ");
		for(double dblValue : dblArray) {
			System.out.print("|" + dblValue + "|");
		}

		// sorting partial double[] array
		Arrays.sort(dblArray, 1, 6);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(double dblValue : dblArray) {
			System.out.print("|" + dblValue + "|");
		}
	}
}

Output:

Before sorting :
|90.9999||10.0101||80.7525||100.1278||12.1123||70.3034||30.2356|

After sorting :
|90.9999||10.0101||12.1123||70.3034||80.7525||100.1278||30.2356|

Explanation:

  • Here, there are 7 double elements in double[] array
  • But, we have sorted double[] array starting from index-1 till index-5 leaving 1st & last element
  • Therefore, 1st and last element remains as it is after sorting and only middle elements are sorted

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - long[] Arrays sorting with example
Java - float[] Arrays Sorting with example