Pendulum Arrangement Program in Java and Python
Pendulum arrangement program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
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 should come in the centre position of the array. The next higher number goes to the right of the minimum, the next higher number goes to the left of the minimum, and the process continues alternately on both sides.
Algorithm:
Step 1: Start.
Step 2: Accept the number of elements n and input all elements into array A.
Step 3: Sort A in ascending order using nested loops and swapping.
Step 4: Declare result array B of size n.
Step 5: Calculate the centre index as mid = (n - 1) / 2.
Step 6: Place the smallest element A[0] at B[mid].
Step 7: Set x = 1 to point to the next sorted element.
Step 8: For distance d from 1 while elements are still left, first check the right position mid + d.
Step 9: If the right position is inside the array, store A[x] there and increase x.
Step 10: Then check the left position mid - d; if it is inside the array, store A[x] there and increase x.
Step 11: Display the sorted array and the pendulum arrangement.
Step 12: Stop.
Explanation:
The first important step is sorting the input array. Pendulum arrangement depends on increasing order, because values are placed around the centre from the smallest to the largest.
The result array B is filled separately so that the sorted order in A is not disturbed. The centre index is calculated as (n - 1) / 2, which works for both odd and even lengths using integer division.
The smallest value is placed at the centre. The variable x then points to the next value to be placed. For every distance from the centre, the program tries the right side first and then the left side.
Boundary checks are necessary because for even-sized arrays one side may run out before the other. The checks mid + d < n and mid - d >= 0 prevent invalid array positions.
Pendulum arrangement is based on placing sorted numbers alternately around a central position so that the final array spreads values in a balanced manner. The program first sorts the input because arrangement depends on ordered values. After sorting, positions are chosen to the left and right of the centre according to the required pattern. The main challenge is not sorting but deciding where each sorted value should be placed. This makes index control important, especially for arrays with even and odd lengths.
Java Program:
Minimum Element at Centre:
/**
* The class Pendulum_Array inputs a set of integers in an Array and arranges them in Pendulum Fashion
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Pendulum_Array
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter number of elements: "); // Inputting the number of elements
int n = sc.nextInt();
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] = sc.nextInt();
}
/*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");
}
}
}Maximum Element at Centre Variation:
/**
* The class Pendulum_Array inputs a set of integers in an Array and arranges them in Pendulum Fashion
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Pendulum_Array
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter number of elements: "); // Inputting the number of elements
int n = sc.nextInt();
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] = sc.nextInt();
}
/*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");
}
}
}Equivalent Python Program:
def sort_ascending(A):
for i in range(len(A) - 1):
for j in range(i + 1, len(A)):
if A[i] > A[j]:
t = A[i]
A[i] = A[j]
A[j] = t
def print_array(A):
for i in range(len(A)):
print(A[i], end=" ")
print()
n = int(input("Enter number of elements: "))
A = []
for i in range(n):
value = int(input("Enter element " + str(i + 1) + ": "))
A.append(value)
sort_ascending(A)
B = [0] * n
# mid is the centre position where the smallest element is stored.
mid = (n - 1) // 2
x = 1
B[mid] = A[0]
d = 1
while x < n:
# Try placing the next element on the right side.
if mid + d < n:
B[mid + d] = A[x]
x = x + 1
# Then try placing the next element on the left side.
if x < n and mid - d >= 0:
B[mid - d] = A[x]
x = x + 1
d = d + 1
print("The Sorted Array is:")
print_array(A)
print("The Pendulum Arrangement is:")
print_array(B)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.