Write an application in java which includes and algorithm that takes an input ar
ID: 3785918 • Letter: W
Question
Write an application in java which includes and algorithm that takes an input array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line. Note: You can assume all integers are positive, but your code must work for an even and odd number of integers and an array of size from 5 to 30.
Example Output:
Input Array: [ 42 , 24, 7, 13, 36, 52]
Pass 1: [7, 52]
Pass 2: [7, 13, 42, 52]
Pass 3: [7, 13, 24, 36, 42, 52]
Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class ArrayProblem {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> a = new ArrayList<Integer>(6);
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter size of array");
n=sc.nextInt();
System.out.println("Enter elemets of array");
for(int i=0;i<n;i++)
{
a.add(sc.nextInt());
}
int j=0;
Collections.sort(a);
int p=1;
ArrayList<Integer> temp = new ArrayList<Integer>(6);
while(!a.isEmpty())
{
temp.add(a.get(0));
temp.add(a.get(a.size()-1));
a.remove(0);
a.remove(a.size()-1);
Collections.sort(a);
Collections.sort(temp);
System.out.println("Pass"+p+temp);
p++;
}
}
}
===================================
Output:
Enter size of array
6
Enter elemets of array
42
24
7
13
36
52
Pass1[7, 52]
Pass2[7, 13, 42, 52]
Pass3[7, 13, 24, 36, 42, 52]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.