WAP in Java(BlueJ) to input nos. in an array and rearrange them in the same array with the largest no. in the middle position, the 2nd largest on the right side of it, the 3rd largest to the left side of it and so on
Answers
Answer:
Rearrange an array in order – smallest, largest, 2nd smallest, 2nd largest, ..
Given an array of integers, task is to print the array in the order – smallest number, Largest number, 2nd smallest number, 2nd largest number, 3rd smallest number, 3rd largest number and so on…..
Examples:
Input : arr[] = [5, 8, 1, 4, 2, 9, 3, 7, 6]
Output :arr[] = {1, 9, 2, 8, 3, 7, 4, 6, 5}
Input : arr[] = [1, 2, 3, 4]
Output :arr[] = {1, 4, 2, 3}
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
Explanation:
here is your answer mate please mark me as brilliant
Answer:
Here,I managed to do this program through a lot of hard work.I would appreciate it if you will mark it as brainliest. Open your pc or laptop if you want to see proper indentation of the program below because this post was posted in a laptop.
import java.util.*;
public class FunnyNumberSorter
{
public static void main(String[] args)
{
int n,i,j,sgn,temp;
Scanner sc=new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
n=sc.nextInt();
int[] arr= new int[n];
System.out.println("Enter "+n+" numbers of your choice:-");
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
int[] sorted = new int[n];
final int center = (n / 2) - 1 + (n % 2);
for(i = 1, sgn = -1; i <= n; i++, sgn *= -1)
{
sorted[center + (sgn*i/2)] = arr[i-1];
}
System.out.println(Arrays.toString(sorted));
}
}
Output:
How many numbers do you want to enter?
10 //this is input
Enter 10 numbers of your choice:-
56 85 45 74 28 67 13 59 48 79 //this is input
[28, 48, 59, 74, 85, 79, 67, 56, 45, 13] //i am sure that this is the answer you
//wanted