Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For Questions 1 6 1 9 refer to the insertionSort method and the private instance variable a , both in a Sorter class. private Integer

For Questions 1619 refer to the insertionSort method and the private instance variable a, both in a Sorter class.
private Integer[] a;
/** Precondition: a[0],a[1]...a[a.length-1] is an unsorted array
* of Integer objects.
* Postcondition: Array a is sorted in descending order.
*/
public void insertionSort()
{
for (int i =1; i < a.length; i++)
{
Integer temp = a[i];
int j = i -1;
while (j >=0 && temp.compareTo(a[j])>0)
{
a[j+1]= a[j];
j--;
}
a[j+1]= temp;
}
}
16. An array of Integer is to be sorted biggest to smallest using the insertionSort
method. If the array originally contains
1795412
what will it look like after the third pass of the for loop?
(A)9715412
(B)9751412
(C)1297154
(D)1297541
(E)9712541
17. When sorted biggest to smallest with insertionSort, which list will need the
fewest changes of position for individual elements?
(A)5,1,2,3,4,9
(B)9,5,1,4,3,2
(C)9,4,2,5,1,3
(D)9,3,5,1,4,2
(E)3,2,1,9,5,4
18. When sorted biggest to smallest with insertionSort, which list will need the
greatest number of changes in position?
(A)5,1,2,3,4,7,6,9
(B)9,5,1,4,3,2,1,0
(C)9,4,6,2,1,5,1,3
(D)9,6,9,5,6,7,2,0
(E)3,2,1,0,9,6,5,4
19. While typing the insertionSort method, a programmer by mistake enters
while (temp.compareTo( a[j])>0)
instead of
while (j >=0 && temp.compareTo( a[j])>0)
Despite this mistake, the method works as intended the first time the programmer enters an array to be sorted in descending order. Which of the following
could explain this?
I The first element in the array was the largest element in the array.
II The array was already sorted in descending order.
III The first element was less than or equal to all the other elements in the array.
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) II and III only

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

Understand why customers are loyal to a particular service firm.

Answered: 1 week ago