Java

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

In Java, sorting organizes values in descending or ascending order. Arrays are the simplest data structure containing a similar type of data stored at a contiguous memory location. Its elements can be accessed directly using the index numbers. In arrays, the indexes start with 0. Instead of the predefined “sort()” method, there are also several other methods that can be used for sorting arrays.

This article will explain the other procedures for sorting arrays without utilizing the sort() method.

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

For sorting an array in Java without the “sort()” method, you can use:

  • Selection sort
  • Insertion sort
  • Bubble sort

Let’s discuss these sorting methods and how they sort elements of an array.

Method 1: Sorting Array Using the Selection Sort

Finding the least array’s element and inserting it at the beginning of the array is what selection sort does. It is the most basic sorting algorithm used for in-place comparisons. This algorithm separates the array into the sorted and the unsorted part.

The array’s unsorted portion holds the specified array while the sorted portion is initially empty. The sorted part is at the start of the array, while the other part of the array is on the other side. The first smallest element from the unsorted array is then selected and placed at the start of the array, which is the part of the sorted array. Following that, the second smallest element is chosen and placed in the second slot. The process will be repeated till the array gets completely sorted.

Consider the following example to understand the stated concept more clearly.

Example

In this example, we have created an integer type array named “array” initialized with the following values:

int[] array = new int[] { 3,12,4,67,23,14,89,5 };

 

We will create an integer type variable “index” that will store the indexes of the array, initialized with “-1” because the index of array starts from 0, so in the loop it will store “0” index:

int index= -1;

 

Here, we will use two “for” loops, one will be used to iterate the array until the length of the array and the second one will be used to check the condition and swap the elements if the 1st element is less than the 2nd one:

for (int i = 0; i <array.length; i++){
index = i;
for (int j = i ; j <= array.length-1; j++){
if (array[j] < array[index]){
index = j;
}
}
int temp = array[i];
array[i] = array[index];
array[index] = temp;
}

 

Then, print the sorted array by using another “for” loop:

for (int i = 0; i < array.length; i++){
System.out.print(array[i] + ",");
}

 

The output indicates that the array is now sorted in ascending order:

Note: If you want to print the array in descending order as a sorted form, you just need to reverse the if condition for swapping the elements.

Method 2: Sorting Array Using the Insertion Sort

Another simple sorting method used to sort an array is Insertion sort. Insertion sort is a brief sorting algorithm that operates like you would arrange playing cards in your hands. It is effective when applied to a few values. Insertion sort is adaptive, and it is suited for partially sorted data sets.

Sorted and unsorted are the subcategories of the array in insertion sort. In contrast to selection sort, the insertion sort picks values from the unsorted value and inserts them into the sorted part at a particular index. In the Insertion sort, the current value is compared to the existing element. If it finds an existing element greater than the current one, the previous element is moved to the next position.

Example

Now, we will sort the same “array” using insertion sort. To do so, we will iterate the array until its length using the “for” loop. We will start the loop from the “1” index. Then, we will set the value of the key as “keyValue” and assign it the element of the array at the 1st index.

The added “while” loop will check the elements of an array by comparing it with the keyValue and swapping the elements. The while loop continues to run up until the condition is evaluated as false. If the key is greater than the previous element, then it swaps both elements:

for(int i = 1; i < array.length; i++){
int keyValue = array[i];
int j = i - 1;
while(j >= 0 && array[j] > keyValue){
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = keyValue;

}

 

For printing a sorted array, we will use another “for” loop:

for (int i = 0; i < array.length; i++){

System.out.print(array[i] + ",");
}

 

Output

Method 3: Sorting Array Using the Bubble Sort

The Bubble Sort checks two nearby elements and swaps them until they are in the desired order. It is called “Bubble sort” because every array element moves to the end of the array in each iteration, much like how air bubbles keep rising to the top of the water. It is only suitable for fewer elements due to its high average and worst-case time complexity.

In Bubble sort, we will compare the first and second elements beginning with the first index. If the first element of the array is greater, then swap the first and second elements. After that, it will perform a comparison between the next two elements. If they are not present in the specified order, this sorting method will swap them.

Example

We will use bubble sort to arrange the array in this example. As in selection sorting, two loops are used to sort the elements. One is for iterating the array until its length, and the second loop is used for checking elements and swapping them based on conditions:

for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
int temp = 0;
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

 

For printing swapped arrays, we will use the “for” loop:

for (int i = 0; i < array.length; i++){

System.out.print(array[i] + ",");
}

 

Output

Note: If you want to print the array in descending order using the bubble sort method, you just need to reverse the added if condition.

We offered all the sorting methods for sorting arrays without using the predefined sort() method.

Conclusion

For sorting an array without using the sort() method in Java, you can use the simple sorting methods, including Selection sort, Insertion sort, and Bubble sort. All of these are the simplest and most efficient sorting methods. More specifically, the Insertion sort is utilized when you want to insert an element at a certain index. All of the provided methods can be used for sorting an array in descending or ascending order.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.