Java: float[] Array sorting with example

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

float:

  • Size is 4 bytes
  • That’s is 32 bits
  • Its range is 3.4e−038 to 3.4e+038

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

 

Sorting float[] array:

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

Method Signature:

public static void sort(float[] a);

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

 

float[] array methods for sorting:

Sort method

Description

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

 

Let us move forward to discuss both methods for sorting float[] array

Method 1: complete sorting of float[] array

  • Here, complete array will be sorted

Method Signature:

public static void sort(float[] a);

 

SortingCompleteFloatArray.java

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

import java.util.Arrays;

public class SortingCompleteFloatArray {

	public static void main(String[] args) {

		// sample float[] array
		float[] flArray = {12.11f, 70.30f, 30.23f, 100.12f, 90.99f, 10.01f, 80.75f};

		// before sorting
		System.out.println("Before sorting : ");
		for(float fValue : flArray) {
			System.out.print("|" + fValue + "|");
		}

		// sorting full float[] array
		Arrays.sort(flArray);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(float fValue : flArray) {
			System.out.print("|" + fValue + "|");
		}
	}
}

Output:

Before sorting : 
|12.11||70.3||30.23||100.12||90.99||10.01||80.75|

After sorting : 
|10.01||12.11||30.23||70.3||80.75||90.99||100.12|

 

Method 2: partial sorting of float[] array

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

Method Signature:

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

 

SortingPartialFloatArray.java

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

import java.util.Arrays;

public class SortingPartialFloatArray {

	public static void main(String[] args) {

		// sample float[] array
		float[] flArray = {12.11f, 70.30f, 30.23f, 100.12f, 90.99f, 10.01f, 80.75f};

		// before sorting
		System.out.println("Before sorting : ");
		for(float fValue : flArray) {
			System.out.print("|" + fValue + "|");
		}

		// sorting partial float[] array
		Arrays.sort(flArray, 1, 6);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(float fValue : flArray) {
			System.out.print("|" + fValue + "|");
		}
	}
}

Output:

Before sorting : 
|12.11||70.3||30.23||100.12||90.99||10.01||80.75|

After sorting : 
|12.11||10.01||30.23||70.3||90.99||100.12||80.75|

 

Explanation:

  • Here, there are 7 float elements in float[] array
  • But, we have sorted float[] 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/Float.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Float.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

Happy Coding !!
Happy Learning !!