Whenever we do hear sorting algorithms come into play such as selection sort, bubble sort, insertion sort, radix sort, bucket sort, etc but if we look closer here we are not asked to use any kind of algorithms. It is as simple sorting with the help of linear and non-linear data structures present within java. So there is sorting done with the help of brute force in java with the help of loops and there are two in-built methods to sort in Java.
Ways of sorting in Java
- Using loops
- Using sort() method of Arrays class
- Using sort method of Collections class
- Sorting on a subarray
Let us discuss all four of them and propose a code for each one of them.
Way 1: Using loops
Java
class GFG {
public static void main(String[] args)
{
int arr[] = { 4 , 3 , 2 , 1 };
for ( int i = 0 ; i < arr.length; i++) {
for ( int j = i + 1 ; j < arr.length; j++) {
int temp = 0 ;
if (arr[j] < arr[i]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
System.out.print(arr[i] + " " );
}
}
}
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Way 2: Using sort() method of Arrays class
Arrays.Sort() works for arrays which can be of primitive data type also which in turn by default sorts in ascending order.
Example 1
JAVA
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
int [] arr = { 13 , 7 , 6 , 45 , 21 , 9 , 101 , 102 };
Arrays.sort(arr);
System.out.printf( "Modified arr[] : %s" ,
Arrays.toString(arr));
}
}
|
Output
Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Example 2
JAVA
import java.util.Arrays;
import java.util.Collections;
public class GFG {
public static void main(String[] args)
{
Integer[] arr = { 13 , 7 , 6 , 45 , 21 , 9 , 2 , 100 };
Arrays.sort(arr, Collections.reverseOrder());
System.out.printf( "Modified arr[] : %s" ,
Arrays.toString(arr));
}
}
|
Output
Modified arr[] : [100, 45, 21, 13, 9, 7, 6, 2]
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Way 3: Using sort() method of Collections class
Collections.sort() works for objects Collections like ArrayList and LinkedList.
Example
JAVA
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add( "Geeks For Geeks" );
al.add( "Friends" );
al.add( "Dear" );
al.add( "Is" );
al.add( "Superb" );
Collections.sort(al);
System.out.println( "List after the use of"
+ " Collection.sort() :\n" + al);
}
}
|
Output
List after the use of Collection.sort() :
[Dear, Friends, Geeks For Geeks, Is, Superb]
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Example 2
JAVA
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add( "Geeks For Geeks" );
al.add( "Friends" );
al.add( "Dear" );
al.add( "Is" );
al.add( "Superb" );
Collections.sort(al, Collections.reverseOrder());
System.out.println( "List after the use of"
+ " Collection.sort() :\n" + al);
}
}
|
Output
List after the use of Collection.sort() :
[Superb, Is, Geeks For Geeks, Friends, Dear]
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Way 4: Sorting only a subarray
JAVA
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
int [] arr = { 13 , 7 , 6 , 45 , 21 , 9 , 2 , 100 };
Arrays.sort(arr, 1 , 5 );
System.out.printf( "Modified arr[] : %s" ,
Arrays.toString(arr));
}
}
|
Output
Modified arr[] : [13, 6, 7, 21, 45, 9, 2, 100]
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Note:
- Which sorting algorithm does Java use in sort()?
Previously, Java’s Arrays.sort method used Quicksort for arrays of primitives and Merge sort for arrays of objects. In the latest versions of Java, Arrays.sort method and Collection.sort() uses Timsort.
- Which order of sorting is done by default?
It by default sorts in ascending order.
- How to sort array or list in descending order?
It can be done with the help of Collections.reverseOrder().
- How to write my own sorting function in Java?
Please see Java programs for Quick Sort, Merge Sort, Insertion Sort, Selection Sort, Heap Sort, Bubble Sort
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!