Assignment 8
Assignment 8
2024)
Write a program to do the following:
Read an integer n (where n can be at most 50). Then read n integers one by
one and store them in an array. Now we have to rearrange the integers in
data in the following ways:
Find the max. value and in data and put it in the center of the array
(n/2).
Find the next largest value and put it to its right side and so on
alternating the right and left until all integers in the data are done.
Ex: n=6
Input:
5 1 9 8 2 4
Output:
2 5 9 8 4 1
Algorithm:
1. Class Definition and Setup
Define a class named Pendulum with:
o Data Members:
2. Method acceptLength()
Purpose: Accept and validate the length of the array.
Steps:
3. Method accept()
Purpose: Accept the elements of the array from the user.
Steps:
method.
Steps:
1. Sorting:
Use a nested loop to sort the array ar in descending order:
+ 1.
If ar[j] < ar[j + 1], swap the elements.
2. Rearranging:
Initialize a new integer array result of the same length n.
].
If i is odd, assign the element to result[right++].
5. Method display()
Purpose: Display the rearranged array after pendulum sorting.
Steps:
Steps:
Class Description
Pendulum Implements Pendulum sort algorithm to rearrange an
array.
OUTPUT: