Java – int[] Array Sorting with example

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

1. int

  • Size is 4 bytes
  • That’s is 32 bits
  • Its range is -2,147,483,648 to 2,147,483, 647

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

2. Sorting int[] array:

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

Method Signature:

public static void sort(int[] a);

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

3. Sorting method for int[] array :

Sort method

Description

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

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

4. Example for Sorting int[] Arrays:

  1. Complete int[] Arrays sorting
  2. Partial int[] Arrays sorting

4.1 Complete int[] Arrays Sorting

  • Here, complete array will be sorted

Method Signature:

public static void sort(int[] a);

SortingCompleteIntegerArray.java

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

import java.util.Arrays;

public class SortingCompleteIntegerArray {

	public static void main(String[] args) {

		// sample int[] array
		int[] intArray = {120, 70, 30, 100, 90, 10, 80};

		// before sorting
		System.out.println("Before sorting : ");
		for(int iValue : intArray) {
			System.out.print(iValue + " ");
		}

		// sorting full int[] array
		Arrays.sort(intArray);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(int iValue : intArray) {
			System.out.print(iValue + " ");
		}
	}
}

Output:

Before sorting :
120 70 30 100 90 10 80 

After sorting :
10 30 70 80 90 100 120

4.2 Partial int[] Arrays Sorting

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

Method Signature:

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

SortingPartialIntegerArray.java

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

import java.util.Arrays;

public class SortingPartialIntegerArray {

	public static void main(String[] args) {

		// sample int[] array
		int[] intArray = {120, 70, 30, 100, 90, 10, 80};

		// before sorting
		System.out.println("Before sorting : ");
		for(int iValue : intArray) {
			System.out.print(iValue + " ");
		}

		// sorting partial int[] array
		Arrays.sort(intArray, 1, 6);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(int iValue : intArray) {
			System.out.print(iValue + " ");
		}
	}
}

Output:

Before sorting :
120 70 30 100 90 10 80 

After sorting :
120 10 30 70 90 100 80

Explanation:

  • Here, there are 7 integer elements in int[] array
  • But, we have sorted int[] 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 - float[] Arrays Sorting with example
Java: short[] Array sorting with example