Sorting in Java

Course Curriculum

Sorting in Java

Sorting in Java

  1. Arrays.Sort() works for arrays which can be of primitive data type also.

// A sample Java program to demonstrate working of
// Arrays.sort().
// It by default sorts in ascending order.
import java.util.Arrays;

public class P_AI {
public static void main(String[] args)
{
int[] arr = { 10, 8, 7, 49, 31, 9, 109, 105 };

Arrays.sort(arr);

System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Output:

Modified arr[] : [7,8,9,10,31,49,105,109]
Collections.sort() works for objects Collections like ArrayList and LinkedList.

// Java program to demonstrate working of Collections.sort()
import java.util.*;

public class P_AI {
public static void main(String[] args)
{
// Create a list of strings
ArrayList<String> al = new ArrayList<String>();
al.add("Prutor Dot AI");
al.add("Friends");
al.add("Dear");
al.add("Is");
al.add("Superb");

/* Collections.sort method is sorting the
elements of ArrayList in ascending order. */
Collections.sort(al);

// Let us print the sorted list
System.out.println("List after the use of"
+ " Collection.sort() :n" + al);
}
}
Output:

List after the use of Collection.sort() :
[Dear, Friends, Prutor Dot AI, Is, Superb]

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().
Example:

For Arrays.sort()

// A sample Java program to sort an array
// in descending order using Arrays.sort().
import java.util.Arrays;
import java.util.Collections;

public class P_AI {
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = {10, 8, 7, 49, 31, 9, 109, 105 };

// Sorts arr[] in descending order
Arrays.sort(arr, Collections.reverseOrder());

System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Output:

Modified arr[] : [109,105,49,31,10,9,8,7]
For Collections.sort()

// Java program to demonstrate working of Collections.sort()
// to descending order.
import java.util.*;

public class P_AI {
public static void main(String[] args)
{
// Create a list of strings
ArrayList<String> al = new ArrayList<String>();
al.add("Prutor Dot AI");
al.add("Friends");
al.add("Dear");
al.add("Is");
al.add("Superb");

/* Collections.sort method is sorting the
elements of ArrayList in ascending order. */
Collections.sort(al, Collections.reverseOrder());

// Let us print the sorted list
System.out.println("List after the use of"
+ " Collection.sort() :n" + al);
}
}
Output:

List after the use of Collection.sort() :
[Superb, Is, Prutor Dot AI, Friends, Dear]

How to sort only a subarray?
Example:

// A sample Java program to sort a subarray
// using Arrays.sort().
import java.util.Arrays;

public class P_AI {
public static void main(String[] args)
{
// Our arr contains 8 elements
int[] arr = { 10, 8, 7, 49, 31, 9, 109, 105 };

// Sort subarray from index 1 to 4, i.e.,
// only sort subarray {8,7,49,31} and
// keep other elements as it is.
Arrays.sort(arr, 1, 5);

System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Output:

Modified arr[] : [10,7,8,31,49,9,109,105]

Binary Search in Java (Prev Lesson)
(Next Lesson) Classes and Objects in Java