Sorting is a fundamental operation in programming, enabling us to arrange data in a specified order for efficient retrieval and analysis. In the realm of Java programming, sorting lists is a common task, and fortunately, Java offers a range of methods and algorithms to accomplish this.

Whether you're a beginner programmer seeking to understand the basics or an experienced developer looking to refresh your skills, this article will provide you with a comprehensive guide on how to sort list in Java.

We'll explore both built-in methods and custom sorting algorithms, each with its advantages and use cases.

## How to Sort List in Java?

Our exploration will encompass both a built-in method and three distinct custom sorting algorithms. Specifically, we will cover the utilization of** ****Arrays.sort()** along with the implementation of bubble sort, selection sort, and insertion sort algorithms.

**Sorting an Array using the Arrays.sort() Method**

The** Arrays.sort()** method is a static method in the **java.util.Arrays** class that is used to sort an array. The syntax of the **Arrays.sort() **method is as follows:

```
public static void sort(Object[] a)
public static void sort(Object[] a, int fromIndex, int toIndex)
```

The first parameter, a, is the array that you want to sort. The second parameter, **fromIndex**, is the index of the first element in the array that you want to sort. The third parameter, toIndex, is the index of the last element in the array that you want to sort.

The **Arrays.sort()** method sorts the array in ascending order by default. However, you can also sort the array in descending order by passing the Collections.reverseOrder() method as a parameter.

For example, the following code sorts the array array in ascending order:

```
import java.util.Arrays;
public class SortArray
{
public static void main(String[] args)
{
int[] array = {50, 15, 2, 17, 23, 91, 48, 9, 16};
// Sort the array in ascending order
Arrays.sort(array);
// Print the sorted array
System.out.println(Arrays.toString(array));
}
}
```

Output:

`[2, 9, 15, 16, 17, 23, 48, 50, 91]`

For example, the following code sorts the array array in descending order:

```
import java.util.Arrays;
import java.util.Collections;
public class SortArray {
public static void main(String[] args) {
int[] array = {10, 5, 2, 7, 3, 1, 8, 9, 6};
// Sort the array in descending order
Arrays.sort(array, Collections.reverseOrder());
// Print the sorted array
System.out.println(Arrays.toString(array));
}
}
```

The **Collections.reverseOrder()** method returns a comparator that imposes the reverse of the natural ordering of elements of the collection. So, when we pass **Collections.reverseOrder() **as a parameter to the **Arrays.sort()** method, the method will sort the array in descending order.

**Pros:**

**Easy to use.**You only need to pass the array that you want to sort, and the method will sort it for you.**Efficient.**It uses a divide-and-conquer algorithm to sort the array, which is a very efficient way to sort large arrays.**Thread-safe.**This means that you can call the method from multiple threads without worrying about race conditions.

**Cons:**

**Not customizable.**You cannot choose the sorting algorithm that the method uses, and you cannot specify the sorting order.**Not efficient for small arrays.**For small arrays, it is faster to sort the array using a simpler sorting algorithm, such as insertion sort.

Overall, the Arrays.sort() method is a good choice for sorting arrays. It is easy to use, efficient, and thread-safe. However, it is not very customizable, and it is not efficient for small arrays.

Here are some additional things to consider when using the **Arrays.sort()** method:

The

**Arrays.sort()**method uses the Dual-Pivot Quicksort algorithm by default. This algorithm is very efficient for large arrays. However, it can be less efficient for small arrays.The

**Arrays.sort()**method is not stable. This means that the relative order of equal elements in the array may not be preserved after the array is sorted.

**Sorting an Array using a Sorting Algorithm**

Three sorting algorithms are available for sorting lists in Java:

Bubble sort algorithm

Insertion sort algorithm

Selection sort algorithm

**1. **__Bubble Sort__

__Bubble Sort__

Bubble sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements in an array and swapping them if they are in the wrong order.

The below code illustrates how to sort list in Java using the bubble sort algorithm:

```
public static void bubbleSort(int[] array)
{
int n = array.length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
```

**Pros:**

Easy to understand and implement.

Performs well on small arrays.

**Cons:**

Slow for large arrays.

Not stable, meaning that the relative order of equal elements in the array may not be preserved after the array is sorted.

Insertion sort is a simple sorting algorithm that works by repeatedly inserting elements into a sorted subarray.

The below code describes how to sort list in Java using the insertion sort algorithm:

```
public static void insertionSort(int[] array)
{
int n = array.length;
for (int i = 1; i < n; i++)
{
int current = array[i];
int j = i - 1;
while (j >= 0 && array[j] > current)
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = current;
}
}
```

**Pros:**

Easy to understand and implement.

Performs well on small and nearly sorted arrays.

Stable, meaning that the relative order of equal elements in the array is preserved after the array is sorted.

**Cons:**

Slow for large arrays.

Selection sort is a simple sorting algorithm that works by repeatedly finding the smallest element in the unsorted subarray and swapping it with the first element of the subarray.

The below code** **describes how to sort list in Java using the Selection sort algorithm:

```
public static void selectionSort(int[] array)
{
int n = array.length;
for (int i = 0; i < n - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (array[j] < array[minIndex])
{
minIndex = j;
}
}
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
```

**Pros:**

Easy to understand and implement.

Performs well on small arrays.

**Cons:**

Slow for large arrays.

Not stable, meaning that the relative order of equal elements in the array may not be preserved after the array is sorted.

### Conclusion

This guide has provided you with a range of sorting techniques for lists in Java. From built-in methods like **Arrays.sort()** to custom algorithms including bubble, selection, and insertion sorts, you now possess a versatile toolkit. Tailoring your approach based on data size, stability, and efficiency considerations, you're well-prepared to navigate the intricacies of sorting in Java. This skill is not only essential for effective data management but also a cornerstone in your programming journey.

## Comments