0% found this document useful (0 votes)
16 views10 pages

Assignment 8

Uploaded by

Aryan Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Assignment 8

Uploaded by

Aryan Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT 8: (10.05.

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:

 ar[]: an integer array to store the input numbers.

 n: an integer to store the length of the array.

2. Method acceptLength()
 Purpose: Accept and validate the length of the array.

 Steps:

1. Create a Scanner object to read input.


2. Prompt the user to enter the length of the array.
3. Read the input value into n.
4. Check if n is less than or equal to 50:
 If true, return true.

 If false, return false.

3. Method accept()
 Purpose: Accept the elements of the array from the user.

 Steps:

1. Create a new Scanner object (though unnecessary since one is


already created in acceptLength()).
2. Print a message to prompt the user to enter the numbers for the
array.
3. Initialize ar with size n.
4. Use a loop to read n integers from the user and store them in ar.
4. Method pendulumSort()
 Purpose: Rearrange the array elements using a pendulum sorting

method.
 Steps:

1. Sorting:
 Use a nested loop to sort the array ar in descending order:

 For each element i, compare adjacent elements j and j

+ 1.
 If ar[j] < ar[j + 1], swap the elements.
2. Rearranging:
 Initialize a new integer array result of the same length n.

 Set left to (n - 1) / 2 (the midpoint of the array).

 Set right to left + 1 (the index just after the midpoint).

 Use a loop to assign sorted elements alternately to the left

and right sides of the midpoint:


 If the index i is even, assign the element to result[left--

].
 If i is odd, assign the element to result[right++].

3. Copying Rearranged Array:


 Copy the elements from result back to the original array ar.

5. Method display()
 Purpose: Display the rearranged array after pendulum sorting.

 Steps:

1. Print "Pendulum Sort:".


2. Use a loop to print each element of the array ar.
6. Main Method
 Purpose: Execute the program.

 Steps:

1. Create an instance ob of the Pendulum class.


2. Call the acceptLength() method:
 If it returns true:

 Call the accept() method to read array elements.

 Call the pendulumSort() method to rearrange the array.

 Call the display() method to show the sorted result.

 If it returns false, print "Invalid Length!".


Variable Listing Table:

Variable Type Scope Purpose


ar int[] Instance Array to store integer
(Pendulum) elements of the array
that will be sorted
using the Pendulum
sort.
n int Instance Stores the length of
(Pendulum) the array, initialized
by user input to
determine the size of
ar.
sc Scanner Local Facilitates input
(acceptLength, reading from the
accept methods) console.
i, j int Local (for loops) Loop counters used
within loops for
iteration purposes.
temp int Local Temporary variable
(pendulumSort used for swapping
method) elements during
sorting.
result int[] Local Temporary array to
(pendulumSort store the rearranged
method) elements after
sorting.
left, right int Local Indices used to track
(pendulumSort positions for
method) alternating
placement of
elements in result
array.
Method Listing Table:

Method Prototype Purpose

acceptLength() boolean Prompts user to enter the


acceptLength() length of the array (n). Returns
true if n is <= 50, otherwise
false.
accept() void accept() Accepts integer elements into
the ar array based on user input
after n is determined.
pendulumSort() void pendulumSort() Sorts the ar array in descending
order and rearranges elements
alternately using Pendulum sort
technique.

display() void display() Displays the elements of the ar


array after sorting using the
Pendulum sort technique.

main(String[]) public static void Entry point of the program.


main(String[] args) Creates an instance of
Pendulum, executes methods
to perform Pendulum sort, and
displays the sorted array.
Class Listing Table:

Class Description
Pendulum Implements Pendulum sort algorithm to rearrange an
array.
OUTPUT:

You might also like