. Advertisement .
..3..
. Advertisement .
..4..
Sorting Java arrays using the Array. sort() method seems like an obvious approach. But is it possible to do so without the sort method? Our answer is a Yes. This guide will show you how to sort an array in Java without using the sort method.
What Is A Java Array?
Example 0:
int[ ] num = new int[6];
In this example, we have defined one array ‘num’ of type integer before assigning six attributes to this value. Let’s see how Java works on arrays.
Example 1:
public class Main
{
public static void main(String[] args)
{
int[] num = new int[6];
for(int i = 0; i < num.length; i++)
{
num[i] = i * 10;
}
for(int i = 0; i < num.length; i++)
{
System.out.println("Element " + i + ", value is " + num[i]);
}
}
}
Output 1:
Element 0: value = 0
Element 1: value = 10
Element 2: value = 20
Element 3: value = 30
Element 4: value = 40
Element 5: value = 50
Sometimes, an array is not readily feasible, requiring you to convert int to an array. Check this QA section for more tips and examples.
How to Sort An Array in Java Without Using The Sort Method
Method 1: Use Selection Sort
This sorting algorithm seeks the smallest array element and places that value at the array beginning. After each pass, the system identifies the next minimum element and inserts it next.
Finally, we will get one array with two smaller arrays (or sub-arrays). One sub-array is sorted at the array beginning, while the other is unsorted.
For each pass, we will move ascendingly. But if the system identifies the biggest element first, we will move descendingly.
Example 2 (Descending):
// descending order
public class WithoutUsingSortMethod
{
public static void main(String[] args)
{
int temp;
int[] arrNumbers = {14, 8, 5, 54, 41, 10, 1, 500};
System.out.println("Before sort: ");
for(int num : arrNumbers)
{
System.out.println(num);
}
for(int a = 0; a <= arrNumbers.length - 1; a++)
{
for(int b = 0; b <= arrNumbers.length - 2; b++)
{
if(arrNumbers[b] < arrNumbers[b + 1])
{
temp = arrNumbers[b];
arrNumbers[b] = arrNumbers[b + 1];
arrNumbers[b + 1] = temp;
}
}
}
System.out.println("After sort: ");
for(int num : arrNumbers)
{
System.out.println(num);
}
}
}
Example 3 (Ascending):
public class WithoutSortMethod
{
public static void main(String[] args)
{
int temp;
int[] arrNumbers = {14, 8, 5, 54, 41, 10, 1, 500};
System.out.println("Before sort: ");
for(int num : arrNumbers)
{
System.out.println(num);
}
for(int a = 0; a < arrNumbers.length; a++)
{
for(int b = a + 1; b < arrNumbers.length; b++)
{
if(arrNumbers[a] > arrNumbers[b])
{
temp = arrNumbers[a];
arrNumbers[a] = arrNumbers[b];
arrNumbers[b] = temp;
}
}
}
System.out.println("After sort: ");
for(int num : arrNumbers)
{
System.out.println(num);
}
}
}
Method 2: Use Bubble Sort
Bubble Sort (or Sinking Sort for some people) is a popular sorting algorithm using repeated steps throughout the lists. It compares adjoining elements and switches their places if these elements are organized in the incorrect order. The list pass will keep repeating until all the lists are sorted thoroughly.
Example 4:
public static void main(String[] args) {
int[] arr = new int[] { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int tmp = 0;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
Conclusion
This article has shown you how to sort an array in Java without using the sort method. Feel free to contact our support team for more help!
Leave a comment