Ticker

6/recent/ticker-posts

Rearrange array elements in specified order in JAVA

Question: Write a program to read integer n, one by one, and store them in an array data index from 0 to n-1. Find the maximum value in data and put it in the center of the array, find the next largest value, and put it to its right, then the next largest and place it to its left, and so on.

In this post, I will share the program to rearrange array elements in a specified order.

For example:- 

original array       =  1,2,3,4,5
rearranged array =  1,3,5,4,2
working:
Since 5 is the largest element, it is placed in the middle, and 4 is the 2nd largest element, so we place it to its right. Now the 3rd largest element is 3, so we place it to its left. And this process continues till the last element.

Sample Input and Output:

Input 1:

Enter number of Integers:5

Enter Value of Integer 1:1

Enter Value of Integer 2:2

Enter Value of Integer 3:3

Enter Value of Integer 4:4

Enter Value of Integer 5:5


Output 1:

Original array:1,2,3,4,5   

Rearranged array:1 3 5 4 2 

Input 2:


Enter number of Integers:6
Enter Value of Integer 1:1
Enter Value of Integer 2:2
Enter Value of Integer 3:3
Enter Value of Integer 4:4
Enter Value of Integer 5:5
Enter Value of Integer 6:6

Output 2:

Original array:1,2,3,4,5,6   
Rearranged array:2 4 6 5 3 1 

Code :

import java.io.*;

public class rearrange
{
    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter number of Integers:");
        int n = Integer.parseInt(in.readLine());// accepting number of Integers

        int a[] = new int[n];// SDA to store numbers
        int max = 0ijd = 0;// Initialising values
        String s = "";// Initialising String 's' for storing Rearrangement of SDA
        for (i = 0i < ni++) {
            System.out.print("Enter Value of Integer " + (i + 1) + ":");
            a[i] = Integer.parseInt(in.readLine());// accepting numbers in SDA
        }

        System.out.print("Original array:");
        for (i = 0i < n - 1i++)
            System.out.print(a[i] + ",");// Displaying Original SDA
        System.out.print(a[n - 1]);
        for (i = 0i < ni++)// loop for storing element after arrangement
        {
            for (j = 0j < nj++)// loop for sorting elements
                if (a[j] > max) {
                    max = a[j];// storing highest value of SDA
                    d = j;// storing index of highest element
                }
            if (i == 0)
                s = max + "";// arranging maximum number before 's'
            else if (i % 2 == 0)
                s = max + " " + s;
            else
                s = s + " " + max;// arranging maximum number after 's'

            a[d] = 0;// Deleting(Overwriting) element with highest value
            max = 0;// deleting value in 'max' by overwriting
        }
        s = s + " ";
        int begin = 0;
        for (int k = 0k < nk++) {
            int end = s.indexOf(' 'begin);
            a[k] = Integer.valueOf(s.substring(beginend));//replacing value at k index of array
            begin = end + 1;
        }
        System.out.print("\nRearranged array:");// Displaying numbers after rearrangement

        for (int k = 0k < nk++) {
            System.out.print(a[k] + " ");
        }
    }
}


Output Window:


output
output window

If you have any doubts/questions related to Program, or if you want to give any suggestions, or want an explanation of codefeel free to comment below. I hope that my code is understandable. Please Subscribe to Programming Chaska for the latest programming updates and also to inspire me to write such unique and different programs. 
Thank You for Visiting 😄

Post a Comment

0 Comments