Java – float[] Arrays Sorting with example

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

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

2. Sorting float[] Arrays:

  • To sort float[] arrays, 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);

3. Sorting method for float[] Arrays :

Sort method

Description

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

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

4. Examples on float[] Arrays Sorting :

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

4.1 Complete/Full Sorting of float[] Arrays

  • Here, complete arrays 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|

4.2 Partial sorting of float[] Arrays

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

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[] arrays
  • But, we have sorted float[] arrays starting from index-1 till index-5 leaving 1st & last elements
  • Therefore, 1st and last elements 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 - double[] Arrays Sorting with example
Java - int[] Array Sorting with example