Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an Java application which includes and algorithm that takes an array of an

ID: 3788226 • Letter: W

Question

Write an Java application which includes and algorithm that takes an 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.

Example1:

Size of Array: 4

Elements: 5,7,8,2

output:

pass1[2,8]

pass2[2,5,7,8]

Example2:

size of array: 5

elements: 11,54,23,45,6

output:

pass1[6,54]

pass2[6,23,45,54]

pass3[6,11,23,45,54]

Explanation / Answer

Here is the code for you:

class SortArray
{
public static void sort(int[] elements)
{
int[] newArray = new int[elements.length];
for(int i = 0; i < elements.length; i++)
newArray[i] = -1;
for(int i = 0; i <= elements.length / 2; i++)
{
int low = 0, high = 0;
for(int j = 0; j < elements.length; j++)
if(elements[j] != -1)
{
low = high = j;
break;
}
for(int j = 0; j < elements.length; j++)
{
if(elements[j] < elements[low] && elements[j] != -1)
low = j;
if(elements[j] > elements[high] && elements[j] != -1)
high = j;
}
newArray[i] = elements[low];
newArray[elements.length - i - 1] = elements[high];
elements[low] = elements[high] = -1;
for(int j = 0; j < elements.length; j++)
if(newArray[j] != -1)
System.out.print(newArray[j] + " ");
System.out.println();
}
}
public static void main(String[] args)
{
int sizeOfArray = 4;
int[] elements1 = {5, 7, 8, 2};
for(int i = 0; i < 4; i++)
System.out.print(elements1[i] + " ");
System.out.println();
sort(elements1);
  
sizeOfArray = 5;
int[] elements2 = {11, 54, 23, 45, 6};
for(int i = 0; i < 5; i++)
System.out.print(elements2[i] + " ");
System.out.println();
sort(elements2);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote