In this article, we will discuss how to sort long[] array with example
long:
- Size is 8 bytes
- That’s is 64 bits
- Its range is -9,223,372,036,854,775,808 to 9,223,372,036,854,755,807
Arrays class has various sort methods for sorting different primitive data-types
Sorting long[] array:
- To sort long[] array, we have 2 variant of sort methods from Arrays class
Method Signature:
public static void sort(long[] a); public static void sort(long[] a, int fromIndex, int toIndex);
Sorting method for long[] array :
Sort method |
Description |
sort(long[]); | sorts complete long[] array |
sort(lng[], sIndex, eIndex); | sorts partial long[] array, as per limits start-index & end-index specified in the method arguments |
Let us move forward to discuss both methods for sorting long[] array
Method 1: complete sorting of long[] array
- Here, complete array will be sorted
Method Signature:
public static void sort(long[] a);
SortingCompleteLongArray.java
package in.bench.resources.java.arrays.sorting; import java.util.Arrays; public class SortingCompleteLongArray { public static void main(String[] args) { // sample long[] array long[] lngArray = {7120, 970, 330, 6100, 590, 210, 480}; // before sorting System.out.println("Before sorting : "); for(long lngValue : lngArray) { System.out.print("|" + lngValue + "|"); } // sorting full long[] array Arrays.sort(lngArray); // after sorting System.out.println("\n\nAfter sorting : "); for(long lngValue : lngArray) { System.out.print("|" + lngValue + "|"); } } }
Output:
Before sorting : |7120||970||330||6100||590||210||480| After sorting : |210||330||480||590||970||6100||7120|
Method 2: partial sorting of long[] array
- This is the another variant to sort array
- where we can specify start & end limits within long[] array
Method Signature:
public static void sort(long[] a, int fromIndex, int toIndex);
SortingPartialLongArray.java
package in.bench.resources.java.arrays.sorting; import java.util.Arrays; public class SortingPartialLongArray { public static void main(String[] args) { // sample long[] array long[] lngArray = {7120, 970, 330, 6100, 590, 210, 480}; // before sorting System.out.println("Before sorting : "); for(long lngValue : lngArray) { System.out.print("|" + lngValue + "|"); } // sorting partial long[] array Arrays.sort(lngArray, 1, 6); // after sorting System.out.println("\n\nAfter sorting : "); for(long lngValue : lngArray) { System.out.print("|" + lngValue + "|"); } } }
Output:
Before sorting : |7120||970||330||6100||590||210||480| After sorting : |7120||210||330||590||970||6100||480|
Explanation:
- Here, there are 7 long elements in long[] array
- But, we have sorted long[] 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/Long.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Long.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Happy Coding !!
Happy Learning !!