w3resource

Java: Sort an array of positive integers of a given array in the specified pattern

Java Array: Exercise-50 with Solution

Write a Java program to sort an array of positive integers from an array. In the sorted array the value of the first element should be maximum, the second value should be a minimum, third should be the second maximum, the fourth should be the second minimum and so on.

Sample Pattern:

[100, 10, 90, 20, 80, 30, 70, 40, 60, 50]

Sample Solution:

Java Code:

// Import the necessary Java utility class for working with arrays.
import java.util.Arrays;
 
// Define the Main class.
public class Main
{
    // A method to rearrange an array with alternating smaller and larger elements.
    static int[] rearrange(int[] new_arra, int n)
    {
        // Create a temporary array for the rearranged elements.
        int temp[] = new int[n];
     
        // Initialize pointers for the smallest and largest elements.
        int small_num = 0, large_num = n - 1;
        // Initialize a flag for alternating between small and large elements.
        boolean flag = true;
     
        // Iterate through the array and rearrange elements.
        for (int i = 0; i < n; i++)
        {
            if (flag)
                temp[i] = new_arra[large_num--];
            else
                temp[i] = new_arra[small_num++];
     
            // Toggle the flag to switch between small and large elements.
            flag = !flag;
        }
     
        return temp;
    }
 
    // The main method for executing the program.
    public static void main(String[] args) 
    {
        // Create an array of integers.
        int nums[] = new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
        int result[];
         
        // Print the original array.
        System.out.println("Original Array ");
        System.out.println(Arrays.toString(nums));
         
        // Rearrange the array.
        result = rearrange(nums, nums.length);
         
        // Print the rearranged array.
        System.out.println("New Array ");
        System.out.println(Arrays.toString(result));
    }
}

Sample Output:

                                                                              
Original Array 
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
New Array 
[100, 10, 90, 20, 80, 30, 70, 40, 60, 50]

Flowchart:

Flowchart: Sort an array of positive integers of a given array in the specified pattern

Java Code Editor:

Previous: Write a Java program to arrange the elements of a given array of integers where all positive integers appear before all the negative integers.
Next: Write a Java program to separate 0s on left side and 1s on right side of an array of 0s and 1s in random order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/array/java-array-exercise-50.php