Sunday , 15 October 2017
Home » Array Related Programs » Pendulum Arrangement of a list of Integers in an Array

Pendulum Arrangement of a list of Integers in an Array

Question:

Write a program to input a list of integers in an array and arrange them in a way similar to the to-and-fro movement of a Pendulum.

The minimum element out of the list of integers, must come in center position of array. The number in the ascending order next to the minimum, goes to the left, the next higher number goes to the right of minimum number and it continues. As higher numbers are reached, one goes to either side of the minimum value in a to-and-fro manner similar to that of a Pendulum.

Example:

INPUT – 1   2   3   4   5
OUTPUT – 5   3   1   2   4

INPUT – 11   12   31   14   5
OUTPUT – 31   12   5   11   14

Programming Code:

/**
* The class Pendulum_Array inputs a set of integers in an Array and arranges them in Pendulum Fashion
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Pendulum_Array
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("nEnter number of elements: "); // Inputting the number of elements
        int n = Integer.parseInt(br.readLine());

        int A[]=new int[n]; //original array
        int B[]=new int[n]; //array for storing the result
        
        /*Inputting the Array*/
        for(int i=0; i<n; i++)
        {
            System.out.print("Enter Element "+(i+1)+": ");
            A[i] = Integer.parseInt(br.readLine());
        }
        
        /*Sorting the Inputted Array in Ascending Order*/
        int t=0;
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(A[i]>A[j])
                {
                    t=A[i];
                    A[i]=A[j];
                    A[j]=t;
                }
            }
        }
        
        /*Printing the Sorted Array*/
        System.out.println("\nThe Sorted Array Is");
        for(int i=0; i<n; i++)
        {
            System.out.print(A[i]+"\t");
        }
        
         int mid = (n-1)/2; //finding index of middle cell
         int x = 1, lim = n-1-mid;
         /*'x' is for accessing elements of array A[] and
         'lim' is for the number of times we have to make this to-and-fro movement*/
         
         /* Pendulum Arrangement Starts Here */
         B[mid]=A[0]; //putting the minimum element in the middle cell

         for(int i=1; i<=lim; i++)
         {
             if((mid+i)<n) //going to the right side
                B[mid+i]=A[x++];
             if((mid-i)>=0) //going to the left side
                B[mid-i]=A[x++];
         }
         
        /*Printing the Result*/
        System.out.println("\n\nThe Result Is");
        for(int i=0; i<n; i++)
        {
            System.out.print(B[i]+"\t");
        }
    }
}

Output:

Example 1:
Enter number of elements: 5
Enter Element 1: 1
Enter Element 2: 2
Enter Element 3: 3
Enter Element 4: 4
Enter Element 5: 5

The Sorted Array Is
1   2   3   4   5

The Result Is
5   3   1   2   4

Example 2:
Enter number of elements: 6
Enter Element 1: 12
Enter Element 2: 34
Enter Element 3: 11
Enter Element 4: 5
Enter Element 5: 67
Enter Element 6: 20

The Sorted Array Is
5   11   12   20   34   67

The Result Is
34   12   5   11   20   67

Note: If you are told to do the same thing but instead of the minimum element coming in the middle cell, you are told to put the maximum element in the middle cell, the next highest element goes to the left, then the next to the right of the middle cell and so on.

Example:

INPUT – 1   2   3   4   5
OUTPUT – 2   4   5   3   1

Programming Code:

/**
* The class Pendulum_Array inputs a set of integers in an Array and arranges them in Pendulum Fashion
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Pendulum_Array
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("nEnter number of elements: "); // Inputting the number of elements
        int n = Integer.parseInt(br.readLine());

        int A[]=new int[n]; //original array
        int B[]=new int[n]; //array for storing the result
        
        /*Inputting the Array*/
        for(int i=0; i<n; i++)
        {
            System.out.print("Enter Element "+(i+1)+": ");
            A[i] = Integer.parseInt(br.readLine());
        }
        
        /*Sorting the Inputted Array in Descending Order*/
        int t=0;
        for(int i=0; i<n-1; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(A[i]<A[j]) // Note: We have changed the sign to '<' here
                {
                    t=A[i];
                    A[i]=A[j];
                    A[j]=t;
                }
            }
        }
        
        /*Printing the Sorted Array*/
        System.out.println("\nThe Sorted Array Is");
        for(int i=0; i<n; i++)
        {
            System.out.print(A[i]+"\t");
        }
        
         int mid = (n-1)/2; //finding index of middle cell
         int x = 1, lim = n-1-mid;
         /*'x' is for accessing elements of array A[] and
         'lim' is for the number of times we have to make this to-and-fro movement*/
         
         /* Pendulum Arrangement Starts Here */
         B[mid]=A[0]; //putting the maximum element in the middle cell

         for(int i=1; i<=lim; i++)
         {

           /* Note: Below we are first going to the left side then to the right,
              just the opposite of the above code */

             if((mid-i)>=0)
                B[mid-i]=A[x++];
             if((mid+i)<n)
                B[mid+i]=A[x++];
         }
         
        /*Printing the Result*/
        System.out.println("\n\nThe Result Is");
        for(int i=0; i<n; i++)
        {
            System.out.print(B[i]+"\t");
        }
    }
}

Output:

Example 1:
Enter number of elements: 5
Enter Element 1: 1
Enter Element 2: 2
Enter Element 3: 3
Enter Element 4: 4
Enter Element 5: 5

The Sorted Array Is
5   4   3   2   1

The Result Is
2   4   5   3   1

Example 2:
Enter number of elements: 6
Enter Element 1: 12
Enter Element 2: 34
Enter Element 3: 11
Enter Element 4: 5
Enter Element 5: 67
Enter Element 6: 20

The Sorted Array Is
67   34   20   12   11   5

The Result Is
12   34   67   20   11   5

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

4 comments

  1. Awesome way!!!!

  2. will your program for Question:

    Write a program to input a list of integers in an array and arrange them in a way similar to the to-and-fro movement of a Pendulum.

    The minimum element out of the list of integers, must come in center position of array. The number in the ascending order next to the minimum, goes to the left, the next higher number goes to the right of minimum number and it continues. As higher numbers are reached, one goes to either side of the minimum value in a to-and-fro manner similar to that of a Pendulum.
    work for duplicate elements in an array?
    reply asap. its really urgent.

  3. Respected Sir,
    This program helped me a lot in my class XII final exam.
    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *