Java program for Sorting Elements In An Array
Sorting Elements in an Array using Java
Here on this page, we will discuss Sorting Elements in an array using Java Programming Language with different types of Sorting Techniques.
Whereas, Sorting is a fundamental operation in computer science and programming. In Java, sorting an array can be done using various algorithms, each with its own performance characteristics. This article covers multiple sorting methods in Java with complete code examples, theoretical explanations, and complexity analysis.

Sorting makes data easier to search, analyze, and visualize. Sorting algorithms differ based on performance, memory usage, and the way they handle equal elements (stability).
This problem statement can be resolved by following sorting techniques….
Methods of Sorting Elements in an Array
We will cover the following techniques:
- Bubble Sort
- Selection Sort
- Insertion Sort
- Arrays.sort() (Built-in Java utility)
- Merge Sort
- Quick Sort
1. Bubble Sort Technique for Sorting Elements
Algorithm:
- Iterate over the array multiple times.
- In each iteration, compare adjacent elements and swap them if they are in the wrong order.
- Continue the process until the array is fully sorted.
Time Complexity: 0(n2)
Code:
public class BubbleSort { public static void 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; } } } } public static void main(String[] args) { int[] array = {5, 2, 9, 1, 5, 6}; bubbleSort(array); for (int num : array) { System.out.print(num + " "); } } }
Output:
1 2 5 5 6 9
2. Selection Sort Technique for Sorting Elements
Algorithm:
Traverse the array to find the smallest element.
Swap it with the element at the beginning.
Move the boundary of the sorted subarray one element forward.
Repeat until the entire array is sorted.
Time Complexity: 0(n2)
Code:
public class SelectionSort { public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } public static void main(String[] args) { int[] array = {29, 10, 14, 37, 13}; selectionSort(array); for (int num : array) { System.out.print(num + " "); } } }
Output:
10 13 14 29 37
Prime Course Trailer
Related Banner
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
3. Insertion Sort Technique for Sorting
Algorithm:
- Start from the second element.
- Compare the current element with its predecessors.
- Shift the elements that are greater than the current element.
- Insert the current element at its correct position.
Time Complexity: 0(n2)
Code:
public class InsertionSort { public static void 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; } } public static void main(String[] args) { int[] array = {12, 11, 13, 5, 6}; insertionSort(array); for (int num : array) { System.out.print(num + " "); } } }
Output:
5 6 11 12 13
4. Arrays.sort(): Built in Method
Algorithm:
This method uses a Dual Pivot Quicksort for primitive types, offering excellent performance.
Time Complexity: 0(n log n)
Code:
import java.util.Arrays; public class BuiltInSort { public static void main(String[] args) { int[] array = {7, 3, 9, 2, 8}; Arrays.sort(array); for (int num : array) { System.out.print(num + " "); } } }
Output:
2 3 7 8 9
5. Merge Sort for Sorting elements
Algorithm:
- Divide the array into two halves.
- Recursively sort both halves.
- Merge the sorted halves.
Time Complexity: 0(n log n)
Code:
public class MergeSort { public static void mergeSort(int[] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } } public static void merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) arr[k++] = L[i++]; else arr[k++] = R[j++]; } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; } public static void main(String[] args) { int[] array = {12, 11, 13, 5, 6, 7}; mergeSort(array, 0, array.length - 1); for (int num : array) { System.out.print(num + " "); } } }
Output:
5 6 7 11 12 13
6. Quick Sort for Sorting elements
Algorithm:
- Select a pivot element.
- Partition the array around the pivot such that left side has smaller and right side has greater elements.
- Recursively apply the above steps to the subarrays.
Time Complexity: Average - O(n log n) and Worst - O(n2)
Code:
public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } public static void main(String[] args) { int[] array = {10, 7, 8, 9, 1, 5}; quickSort(array, 0, array.length - 1); for (int num : array) { System.out.print(num + " "); } } }
Output:
1 5 7 8 9 10
FAQ's related to Sorting Elements
Answer:
For small datasets, Insertion Sort or Bubble Sort is often sufficient and performs well due to low overhead and simple implementation.
Answer:
Merge Sort, Quick Sort, and Heap Sort all have an average-case time complexity of O(n log n), making them efficient for most practical use cases.
Answer:
Yes, Arrays.sort() is optimized:
- For primitive types, it uses a Dual Pivot QuickSort.
- For objects, it uses TimSort, a hybrid of Merge Sort and Insertion Sort.
Answer:
Yes, you can sort in descending order by:
- Sorting normally and then reversing the array.
- Or using Arrays.sort(array, Collections.reverseOrder()) for object arrays.
Answer:
- Stable sort maintains the relative order of equal elements (e.g., Merge Sort, Bubble Sort).
- Unstable sort does not guarantee relative ordering (e.g., Quick Sort, Heap Sort).
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
public class Sort_Element_Of_Array {
public static void main(String[] args) {
int arr[] = {98,34,56,13,45,32,54,76};
TreeSet ts = new TreeSet();
for(Object obj : arr) {
ts.add((Integer)obj);
}
System.out.println(“After Sorting : “+ts);
}
}
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int a[]={5,9,3,8,4};
int n=a.length;
Arrays.sort(a);
for(int i=0;i<n;i++){
System.out.println(a[i]);
}
}
}
Time Complexity: O(nlogn)
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scn.nextInt();
}
for(int i=0;i<n-1;i++){
int temp=0;
for(int j=i+1;jarr[j]){
int val=arr[i];
arr[i]=arr[j];
arr[j]=val;
temp=1;
}
}
if(temp==0){
break;
}
}
for(int val:arr){
System.out.print(val+” “);
}
}
}
Method3=Parallel sort Approach
package Sort_Array;
import java.util.*;
public class Sort_Array_Using_Inbuilt_Method
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter size of array “);
int n=sc.nextInt();
int []a=new int[n];
System.out.println(“Enter element in array “);
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
Arrays.parallelSort(a);
System.out.println(Arrays.toString(a));
}
}
// Rupesh Bhondave @Mumbai
ackage Sort_Array;
import java.util.*;
public class Sort_Using_Navie_Approch
{
public static void main (String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter size of array “);
int n=sc.nextInt();
int []a=new int[n];
System.out.println(“Enter element of array “);
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
int temp=0;
for(int i=0;i<a.length;i++)//first loop start element from o
{
for(int j=i+1;ja[j])// i is greater than j then swap elements
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
}
import java.util.Arrays;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println(“Enter the number of elements you want to enter:”);
int n = s.nextInt();
int[] arr = new int[n];
System.out.println(“Enter the elements: “);
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
System.out.println("The number you entered: " + Arrays.toString(arr));
for (int i=0; i< arr.length; i++)
{
for (int j =0; jarr[j+1])
{
int temp= arr[j];
arr[j]= arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println(” The sorted array is” +Arrays.toString(arr));
}
}
package com.javabasic;
import java.util.Scanner;
public class Main {
public static void prep(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i=0;i<total;i++) {
System.out.println(a[i]);
}
}
public static void main(String[] args) {
int []a=new int[50];
var sc=new Scanner(System.in);
System.out.println("Enter Array Size : ");
int size=sc.nextInt();
System.out.println("Enter the elements one by one ");
for (int i=0;i<size;i++) {
a[i]=sc.nextInt();
}
prep(a,size);
}
}
This is a correct program