SORTING IN JAVA

Tushar
3 min readJan 12, 2023

--

Sorting algorithms are a fundamental concept in computer science and programming. They are used to arrange elements in a specific order, such as ascending or descending. In this blog, we will discuss the different sorting techniques in Java and provide examples for each.

  1. Bubble sort

Bubble sort is one of the simplest sorting algorithms. It repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

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

2. selection sort

Selection sort is an in-place comparison-based sorting algorithm. It divides the input list into two parts: the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire input list.

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

3. insertion sort

Insertion sort is an in-place comparison-based sorting algorithm. It builds the final sorted list one item at a time.

public static int[] insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
return arr;
}

4. merge sort

Merge sort is a divide and conquer algorithm. It divides the input list into two sublists, recursively sort the sublists, and then merge the sorted sublists to produce the final sorted list.

public static int[] mergeSort(int[] arr) {
if (arr.length < 2) {
return arr;
}
int mid = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, mid);
int[] right = Arrays.copyOfRange(arr, mid, arr.length);
return merge(mergeSort(left), mergeSort(right));
}

private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int i = 0;
int j = 0;
int k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result[k] = left[i];
i++;
} else {
result[k] = right[j];
j++;
}
k++;
}
while (i < left.length) {
result[k] = left[i];
i++;
k++;
}
while (j < right.length) {
result[k] = right[j];
j++;
k++;
}
return result;
}

This code uses the merge sort algorithm to sort an input array of integers. The mergeSort() method takes in an array and divides it into two subarrays, recursively calling itself on each subarray until they are of length 1. The merge() method then takes in the two sorted subarrays and compares the first elements of each, adding the smaller element to the result array and moving the pointer on that subarray forward. The result array is then returned.

In conclusion, sorting algorithms are an essential concept in computer science and programming. They allow one to efficiently arrange elements in a specific order and can have a significant impact on the performance of a program. In this blog, I discussed four popular sorting algorithms in Java: bubble sort, selection sort, insertion sort, and merge sort. Each algorithm has its own strengths and weaknesses, and the choice of which algorithm to use depends on the specific requirements of the problem at hand.

I hope that this blog provided a clear understanding of these sorting algorithms and their implementation in Java. I welcome any suggestions or feedback on how to improve the discussion of these algorithms and provide more valuable information to my readers.

--

--