Java: double[] Array sorting with example

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

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

 

Sorting double[] array:

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);

 

double[] array methods for sorting:

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[] array

Method 1: complete sorting of double[] array

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

 

Method 2: partial sorting of double[] array

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

 

References:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
https://docs.oracle.com/javase/6/docs/api/java/lang/Double.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

Happy Coding !!
Happy Learning !!

Java: long[] Array sorting with example
Java: float[] Array sorting with example