Last Updated:

Sorting algorithms in Java for beginners

Array sorting is the process of ordering the elements of an array in descending or ascending order. Below we will give the most common sorting methods and algorithms that even a beginner can cope with.

Selectionsort in Java

Example of the Sort by Selection algorithm in Java:

publicstaticvoidselectionSort(int[] arr){
/*We will look at all subsets of
the array elements in turn (0 - last, last 1,
2-last,...) */
for (int i = 0; i <arr.length; i++) {
/*Assume that the first element (in each
subset of elements) is the minimum */
intmin = arr[i];
intmin_i = i;
/*In the remainder of the subset,
we look for an element that is less than the assumed minimum*/
for (int j = i+1; j <arr.length; j++) {
//If we find, we remember its index
if (arr[j] <min) { {
min = arr[j];
min_i = j;
}
}
/*If there is an element smaller than the current position,
swap them*/
if (i != min_i) {
inttmp = arr[i];
arr[i] = arr[min_i];
arr[min_i] = tmp;

}
}
}

Bubblesort in Java

 

This method is quite common: the algorithm passes the array completely, pairwise comparing neighboring elements. If they are in the wrong order, then they will be swapped. So, after the first pass, the maximum element is located at the end of the array (sorting in ascending order).

Then the array is repeatedly passed, as a result of which another element appears in the last place, the largest after the maximum, and so on. As a consequence, the smallest element is at the beginning of the array (pops up like a bubble in water).

Example of the Sort bubble algorithm (ascending):

publicstaticvoidbubbleSort(int[] arr){
/* The outer loop reduces the array fragment each time,
since the inner loop puts at the end every time
fragment max element*/
for(int i = arr.length-1 ; i > 0 ; i—){
for(int j = 0 ; j < i ; j++){
/* Compare elements in pairs,
if they are in the wrong order,
then swap
if( arr[j] >arr[j+1] ){
inttmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}

For clarity, here are two videos that clearly demonstrate the work of sorting algorithms by selection and bubble.

Examples of how the above algorithms can be used. First of all, let's create an array:

intarr[] ={62, 84, 32, 5, 0, 14, 52, 82, 58, 71};

Or you can create an array of random numbers

intarr[] = newint[10];
for(int i = 0; i<arr.length; i++) {
// array element is assigned a random number from 0 to 99
arr[i] = (int)(Math.random() * 100);
System.out.print(arr[i] + " ");
}

Next, we will use one of the sorting algorithms presented above:

System.out.print("\n");
bubbleSort(scar);
for(int i = 0; in <arr.length; i++) {
System.out.print(arr[i] + » «);

}

or

System.out.print("\n");
selectionSort(scar);
for(int i = 0; in <arr.length; i++) {
System.out.print(arr[i] + » «);

}

Attention! Bubble and selection sorts are the simplest, but they are not suitable for large arrays. Such algorithms are more related to educational and are rarely used in life. Today, there is no need to implement algorithms for sorting on your own, since everything that is required is already in the standard java libraries.

Sort an array using the sort() method from the Arrays class

The sort() method from the Arrays class uses an advanced Quicksort algorithm To sort an array, you only need to write one line.

Arrays.sort(arr);// where arr is the array name

Attention! First at the beginning of the file, you need to connect the java.util library.

importjava.util.*;

Sort an array of integers ascending:

//Create an array of random numbers
intarr[] = newint[10];
for(int i = 0; i<arr.length; i++) {
arr[i] = (int)(Math.random() * 100);
System.out.print(arr[i] + " ");
}
System.out.print("\nSorted: \n");
//Sort the array
Arrays.sort(arr);
//Output the sorted array to the console.
for(int i = 0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}

Sort an array of integers in descending order:

//Create an array of random numbers
Integerarr[] = newInteger[10];
for(int i = 0; i<arr.length; i++) {
arr[i] = (int)(Math.random() * 100);
System.out.print(arr[i] + " ");
}
System.out.print("\nSorted: \n");
//Sort the array
Arrays.sort(arr, Collections.reverseOrder());
//Output the sorted array to the console.
for(int i = 0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}

Importantly! When sorting an array in reverse order, you must use the Integer[] type instead of the primitive int[].

Sorting an array of strings in Java:

String[] names = newString[] {«Roman»,»Anna», «Petr», «Maria»};

Arrays.sort(names);
for(int i = 0; i <names.length; i++) {
System.out.print(names[i] + » «);

}

In this example, the name array is sorted from A to Z. To sort the array in reverse order, specify Collections.reverseOrder() in the sort() method.

Arrays.sort(names, Collections.reverseOrder());

By default, the sort() method sorts only primitive data types and strings. But on our site there are more complex methods. Don't forget to share your experience in the comments.