Java Program to Rearrange Array such that Even Index Elements are Smaller and Odd Index Elements are Greater

In the previous article, we have seen Java Program to Reorder an Array According to Given indexes

In this article we are going to see how to Rearrange Array such that Even Index Elements are Smaller and Odd Index Elements are Greater than their next elements in Java Programming Language.

Java Program to Rearrange Array such that Even Index Elements are Smaller and Odd Index Elements are Greater

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see how to rearrange array such that even index elements are smaller and odd index elements are greater than their next elements

Method: Java Program to Rearrange Array such that Even Index Elements are Smaller and Odd Index Elements are Greater By Using Sorting Technique

Approach:

  1. Sort the array in descending order.
  2. Iterate over the array from second element.
  3. Swap the adjacent elements.

Program:

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        // initialize the array
        Integer[] arr = { 41, 25, 13, 17, 32, 12, 44 };
        rearrange(arr);
    }
    public static void rearrange(Integer[] arr) 
    {
        System.out.println("Array before rearranging: " + Arrays.toString(arr));
        // sort the array in decreasing order
        Arrays.sort(arr, Collections.reverseOrder());
        System.out.println("Sorted Array:"+Arrays.toString(arr));
        // swap the elements from the second index
        for (int i = 1; i < arr.length; i+=2) 
        {
            int temp = arr[i];
            arr[i] = arr[i - 1];
            arr[i - 1] = temp;
        }
        // print the array
        System.out.println("Array after rearranging: " + Arrays.toString(arr));
    }
}

Output:

Array before rearranging: [41, 25, 13, 17, 32, 12, 44]
Sorted Array:[44, 41, 32, 25, 17, 13, 12]
Array after rearranging: [41, 44, 25, 32, 13, 17, 12]

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: