In this article, we will discuss how to sort short[] array with example
short:
- Size is 2 bytes
- Its range is -32,768 to 32,767
Arrays class has various sort methods for sorting different primitive data-types
Sorting short[] array:
- To sort short[] array, we have 2 variant of sort methods from Arrays class
Method Signature:
public static void sort(short[] a); public static void sort(short[] a, int fromIndex, int toIndex);
Sorting method for short[] array :
Sort method | Description |
sort(short[]); | sorts complete short[] array |
sort(s[], sIndex, eIndex); | sorts partial short[] array, as per limits start-index & end-index specified in the method arguments |
Let us move forward to discuss both methods for sorting short[] array
Method 1: complete sorting of short[] array
- Here, complete array will be sorted
Method Signature:
public static void sort(short[] a);
SortingCompleteShortArray.java
package in.bench.resources.java.arrays.sorting; import java.util.Arrays; public class SortingCompleteShortArray { public static void main(String[] args) { // sample short[] array short[] shArray = {13, 31, 97, 83, 11, 19, 17}; // before sorting System.out.println("Before sorting : "); for(short shValue : shArray) { System.out.print(shValue + " "); } // sorting full short[] array Arrays.sort(shArray); // after sorting System.out.println("\n\nAfter sorting : "); for(short shValue : shArray) { System.out.print(shValue + " "); } } }
Output:
Before sorting : 13 31 97 83 11 19 17 After sorting : 11 13 17 19 31 83 97
Method 2: partial sorting of short[] array
- This is the another variant to sort array
- where we can specify start & end limits within short[] array
Method Signature:
public static void sort(short[] a, int fromIndex, int toIndex);
SortingPartialShortArray.java
package in.bench.resources.java.arrays.sorting; import java.util.Arrays; public class SortingPartialShortArray { public static void main(String[] args) { // sample short[] array short[] shArray = {13, 31, 97, 83, 11, 19, 17}; // before sorting System.out.println("Before sorting : "); for(short shValue : shArray) { System.out.print(shValue + " "); } // sorting partial short[] array Arrays.sort(shArray, 1, 6); // after sorting System.out.println("\n\nAfter sorting : "); for(short shValue : shArray) { System.out.print(shValue + " "); } } }
Output:
Before sorting : 13 31 97 83 11 19 17 After sorting : 13 11 19 31 83 97 17
Explanation:
- Here, there are 7 short elements in short[] array
- But, we have sorted short[] 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/Short.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Short.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Happy Coding !!
Happy Learning !!