java

Sort Array in Descending Order()

Parameters: int[] arr

an array of integers to be sorted

Returns: sorted array in descending order

This Java function demonstrates how to organize an array (i.e., a data structure that contains a group of elements) in a descending order using basic programming principles and structure.

Array
Sorting Algorithm
Looping
Conditionals
Variables
Medium dificulty

Creating a Function in Java to Sort an Array in Descending Order

Hello there, fellow programmer! In this post, we're going to show you how to construct a function in Java that sorts an array in descending order. As developers, we often encounter situations where we need to rearrange data in various ways, and sorting arrays is a common task. With this guide, you'll be able to understand and apply this sorting technique in your own projects. Happy programming!

Step 1: Import Required Modules

First and foremost, we'll need to import the necessary Java utility collection for sorting. We'll be using the Arrays utility from Java which provides static methods to dynamically create and access arrays.

import java.util.Arrays;

Step 2: Initialize the Array

Next, we're going to initialize the array that we want to sort. In this case, we'll define an integer array named arr.

int[] arr = {1, 3, 2, 5, 4};

Step 3: Sort the Array

Next we have to sort the array in ascending order by using Arrays.sort() function.

Arrays.sort(arr);

Step 4: Reverse the Array

As we want to sort the array in descending order, after sorting array in ascending order, we'll need to reverse it. There are several ways to do this, but a simple approach is to iterate through the array from back to front, swapping the elements.

int start = 0, end = arr.length - 1;
while(start < end)
{
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    start++;
    end--;
}

Step 5: Print the Sorted Array

Finally, we print our sorted array to verify it is indeed sorted in descending order. Loop through the array and print out each element.

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

Conclusion

In conclusion, the Java built-in array sorting utility is simple to use, and by combining it with the traditional method of swapping array elements, we can easily get a sorted array in descending order.

Below is the complete code:

import java.util.Arrays;

class Main
{
    public static void main(String[] args)
    {
        int[] arr = {1, 3, 2, 5, 4};
        Arrays.sort(arr);
        int start = 0, end = arr.length - 1;
        while(start < end)
        {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
        for(int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i] + " ");
        }
    }
}

Above code when executed will print 5 4 3 2 1 which is the sorted version of the initial array but in descending order.

Learn function in:

Array Sorting in Descending Order

Rearranging array elements from highest to lowest value

Learn more

Mathematical principle

The function applies the comparison sort mathematical principle. Comparison sort includes algorithms like bubble sort and insertion sort, which compare each element in the dataset and perform swaps to arrange them in a specified order. In this case, it's used to sort an array in descending order. It follows the logic where: `if(array[j] < array[i])` then the elements are swapped.

Learn more