Java: int[] Array sorting with example

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

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

 

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

 

int[] array methods for sorting:

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

Method 1: complete sorting of int[] array

  • 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

 

Method 2: partial sorting of int[] array

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

 

References:

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

 

Happy Coding !!
Happy Learning !!

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