Java.util.rt(int[]) Method - Tutorialspoint
Maybe your like
Description
The Java Arrays sort(int[] a) method sorts the specified array of ints into ascending numerical order. This method using a Dual-Pivot Quicksort algorithm which breaks the array into subarrays, sorted them and then merged to give a sorted array.
Declaration
Following is the declaration for java.util.Arrays.sort(int[] a) method
public static void sort(int[] a)Parameters
a − This is the array to be sorted.
Return Value
This method does not return any value.
Exception
NA
Java Arrays sort(int[] a, int fromIndex, int toIndex) MethodDescription
The Java Arrays sort(int[] a, int fromIndex, int toIndex) method sorts the specified range of given array of ints into ascending numerical order. This method using a Dual-Pivot Quicksort algorithm which breaks the array into subarrays, sorted them and then merged to give a sorted array.
Declaration
Following is the declaration for java.util.Arrays.sort(int[] a, int fromIndex, int toIndex) method
public static void sort(int[] a, int fromIndex, int toIndex)Parameters
a − This is the array to be sorted.
fromIndex − This is the index of the first element, inclusive, to be sorted.
toIndex − This is the index of the last element, exclusive, to be sorted
Return Value
This method is not returning anything.
Exception
IllegalArgumentException − if fromIndex > toIndex
ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex > array.length
Sorting an Array of ints Example
The following example shows the usage of Java Arrays sort(int[]) method. First, we've created an array of ints, printed the original array. Using sort() method, array is sorted and printed thereafter.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // initialize unsorted array int arr[] = { 11, 54, 23, 32, 15, 24, 31, 12 }; System.out.print("Original Array: ["); // print the array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); // sort the array Arrays.sort(arr); System.out.print("Sorted Array: ["); // print the array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); } }Output
Let us compile and run the above program, this will produce the following result −
Original Array: [11 54 23 32 15 24 31 12 ] Sorted Array: [11 12 15 23 24 31 32 54 ]Sorting an Array of ints using Range Example
The following example shows the usage of Java Arrays sort(int[], int, int) method. First, we've created an array of ints, printed the original array. Using sort() method, array is sorted and printed thereafter.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // initialize unsorted array int arr[] = { 11, 54, 23, 32, 15, 24, 31, 12 }; System.out.print("Original Array: ["); // print the array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); // sort the array Arrays.sort(arr, 0, arr.length); System.out.print("Sorted Array: ["); // print the array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); } }Output
Let us compile and run the above program, this will produce the following result −
Original Array: [11 54 23 32 15 24 31 12 ] Sorted Array: [11 12 15 23 24 31 32 54 ]Sorting a Sub-Array of ints Example
The following example shows the usage of Java Arrays sort(int[], int, int) method. First, we've created an array of ints, printed the original array. Using sort() method, a sub-array is sorted and printed thereafter.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // initialize unsorted array int arr[] = { 11, 54, 23, 32, 15, 24, 31, 12 }; System.out.print("Original Array: ["); // print the array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); // sort first five elements of the array Arrays.sort(arr, 0, 5); System.out.print("Sorted Array: ["); // print the array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); } }Output
Let us compile and run the above program, this will produce the following result −
Original Array: [11 54 23 32 15 24 31 12 ] Sorted Array: [11 15 23 32 54 24 31 12 ] java_util_arrays.htmTag » How To Sort Java Array
-
rt() In Java With Examples - GeeksforGeeks
-
How To Sort An Array In Java - Javatpoint
-
How To Sort An Array In Java - Tutorial With Examples
-
How To Sort An Array In Java - Video & Lesson Transcript
-
Sort An Array In Java - Stack Overflow
-
What Is Sort Array In Java: Everything You Need To Know | Simplilearn
-
Arrays (Java Platform SE 7 ) - Oracle Help Center
-
Arrays (Java SE 11 & JDK 11 ) - Oracle Help Center
-
Java Sort Array – How To Reverse An Array In Ascending Or ...
-
How To Sort An Array In Descending Order In Java? Example Tutorial
-
How To Use The rt() Method In Java
-
How To Sort An Array In Java - Code |Interviewkickstart
-
How To Sort An Array In Java Without Using The Sort() Method
-
Java - How To Sort An Array Only In Specific Index Range?