Sort an Array in Java Without Using the sort() Method

Haider Ali Jan 30, 2023 Nov 22, 2021
  1. Sort an Array in Java Without Using the sort() Method - Bubble Sort
  2. Sort an Array in Java Without Using the sort() Method - Selection Sort
  3. Sort an Array in Java Without Using the sort() Method - Insertion Sort
Sort an Array in Java Without Using the sort() Method

This guide will teach us to sort an array in Java without using the sort() method. There are generally three ways to sort an array. These three algorithms are quite efficient in sorting any array. Let’s learn more about these algorithms.

Sort an Array in Java Without Using the sort() Method - Bubble Sort

Let’s understand the concept of bubble sort, which is the simplest sorting algorithm. Basically, in this algorithm, you repeatedly swap the adjacent elements in the array. If they are in the incorrect order, the algorithm will swap the two elements. Take a look at the presentation.

Sort an Array in Java without Using the sort() Method - Bubble Sort

If you look at the picture provided above, you can see the swapping of the two adjacent elements. In the end, we have the sorted array. If there’s any confusion, take a look at the code of this bubble sort algorithm.

public class Main
{
public static void main(String[] args) 
{
    int[] arr = new int[] { 5, 4, 3, 8, 32, 768, 564, 95, 172, 1500, 859, 754 };
   //Example Array To sort...
    for (int i = 0; i < arr.length; i++) 
    {      //Loop over java Array  outer Loop use
        for (int j = i + 1; j < arr.length; j++) 
        {  // Loop over java array
            int tmp = 0;                            //tempraory variable in order to compare.
            if (arr[i] > arr[j]) 
            {          //compare outer loop object with inner loop 
                tmp = arr[i];               // if greater than swapping.
                arr[i] = arr[j];            // Swaping code here.
                arr[j] = tmp;
            }
        }
    }
           // After Sorting Printing The Value.............

           for (int i = 0; i < arr.length; i++) 
           {
              System.out.println(arr[i]);  
           }
}
}

Output:

3
4
5
8
32
95
172
564
754
768
859
1500

The outer loop is just for passing over the array while the inner loop swaps the adjacent unordered elements in each pass. At the end of the last pass, we are left with the sorted array.

Sort an Array in Java Without Using the sort() Method - Selection Sort

The selection sort algorithm works by searching for the minimum element in an array and placing it at the start of the said array. With each pass, we find the next smallest element and place it next. Eventually, we get an array with two sub-arrays. One sub-array is a sorted array that is at the beginning of the array. The other sub-array is an unsorted one.

In each pass, we move in ascending order. If we were to find the largest element first, then we would move it in descending order. Take a look at the illustration down below.

Sort an Array in Java without Using the sort() Method - Selection Sort

In the above illustration, the movement is in descending order, and we are finding the largest elements first and placing it at the end. The presentation shows the array divided into two sub-arrays, as explained above.

public class Main
{

    public static void main(String[] args) 
    {
		int[] arr1 = new int[] { 5, 4, 3, 8, 32, 768, 564, 95, 172, 1500, 859, 754 };
    	int small= -1;   //at start there is no small  number....
    	for (int i = 0; i <arr1.length; i++)
    	{
      		small = i;
      		for (int j = i ; j <= arr1.length-1; j++)
      		{
        		if (arr1[j] < arr1[small])
        		{
           			small = j;
        		}
        		//swap values
      	}
           int temp = arr1[i];
           arr1[i] = arr1[small];
           arr1[small] = temp; 
    	}
  
      	for (int i = 0; i < arr1.length; i++) 
      	{
      		System.out.println(arr1[i]);  
   		}

    }
}

In the above code example, the outer loop works as passing over the array. The inner loops swap the smallest element with the element on the current index.

Sort an Array in Java Without Using the sort() Method - Insertion Sort

Insertion sort is yet another simple sorting algorithm that we can use to sort an array. In this algorithm, the given array is divided into two sub-arrays too. But unlike selection sort, the element is picked from the unsorted sub-array and is placed at its correct position. Take a look at the picture explanation down below.

Sort an Array in Java without Using the sort() Method - Insertion Sort

As you can see, it’s not swapping between the adjacent elements. It’s simply finding the smallest element and placing it in its right position. We iterate from 1st element and move our way towards the end. The element is compared with its predecessor and then the element next to it until they find its right place. In the example that was provided, you can see that 2 is compared four times before it was placed at the correct position. Here’s the code example.

public class Main
{
public static void main(String[] args) 
{     // Insertion Sort....
      int[] arr2 = new int[] { 5, 4, 3, 8, 32, 768, 564, 95, 172, 1500, 859, 754 };
      int number = arr2.length;
      for(int a = 1; a < number; a++)
      {
         int keyValue = arr2[a];
         int b = a - 1;
         while(b >= 0 && arr2[b] > keyValue)
         {
            arr2[b + 1] = arr2[b];
            b = b - 1;
         }
         arr2[b + 1] = keyValue;
      }
         //printing inserion sort............................
         for (int i = 0; i < arr2.length; i++) 
         {
           System.out.println(arr2[i]);  
         }
}
}           

These are the three methods you can use to sort an array in Java without using the sort function.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java Sort

Related Article - Java Array