Computers and Technology

Question: 4 Consider the following correct implementation of the selectionsort algorithm:

public static ArrayList selectionSort(ArrayList arr){ int currentMinIndex; //Line 3 int counter = 0; for (int i = 0; i < arr. size() - 1; i++) //Line 5 { currentMinIndex = i; for (int j = i + 1; j < arr. size(); j++) { if(arr. get(j) < arr. get(currentMinIndex)) //Line 10 { currentMinIndex = j; } } if (i != currentMinIndex) //Line 16 { int temp = arr. get(currentMinIndex); arr. set(currentMinIndex, arr. get(i)); //Line 19 arr. set(i, temp); } } return arr;}

What would be the most effective way to alter this selectionsort algorithm so that an ArrayList would be sorted from greatestto smallest value?

Change Line 3 to int currentMaxIndex;

Change Line 5 to for(int i= arr. size() -1; i > 0; i --)

Change Line 10 to if(arr. get(j) >arr. get(currentMinIndex))

Change Line 16 to if (i > currentMinIndex)

Change Line 19 to arr. set(arr. get(i), currentMinIndex);

Question: 11

Consider the following correct implementation of the insertionsort algorithm:

public static int[] insertionSort(int[] arr){ for (int i = 1; i < arr. length; i++) { int curNumber = arr[i]; int curIndex = i-1; while ( curIndex >= 0 && arr[curIndex] > curNumber) { arr[curIndex+1] = arr[curIndex]; curIndex--; //Line 13 } arr[curIndex + 1] = curNumber; } return arr;}

The following declaration and method call are made in anothermethod in the same class as insertionSort:

int[] nums = {6, 5, 4, 3, 2, 1};list = insertionSort(nums);

How many times is the statement on Line 13 executed as a resultof the call to insertionSort?

5

30

15

16

6

Question: 13

Consider the following correct implementation of the selectionsort algorithm:

1. public static ArrayList selectionSort(ArrayList arr)2. {3. int currentMinIndex;4. int counter = 0;5. for (int i = 0; i < arr. size() - 1; i++)6. {7. currentMinIndex = i;8. for (int j = i + 1; j < arr. size(); j++)9. {10. if(arr. get(j) < arr. get(currentMinIndex))11. {12. currentMinIndex = j;13. }14. }15. if (i != currentMinIndex)16. {17. int temp = arr. get(currentMinIndex);18. arr. set(currentMinIndex, arr. get(i));19. arr. set(i, temp); //Line 1920. }21. }22. return arr;23. }

Given an ArrayList initialized with the values [6, 5, 4, 3, 2,1], how many times does Line 19 execute when the ArrayList issorted using the selection sort algorithm?

6

3

5

30

4

Question: 17

A website organizes its list of contributors in alphabeticalorder by last name. The website’s new manager would prefer thecontributor list to be ordered in reverse alphabetical orderinstead. Which classic algorithm would be best suited to completethis task?

Linear/Sequential Search

Selection Sort

Insertion Sort

None of these algorithms are viable options.

Question: 18

The following method is a search method intended to return theindex at which a String value occurs within an ArrayList:

public int search(ArrayList list, String target) //Line 1{ int counter = 0; while(counter < list. size()) //Line 4 { if(list. get(counter).equals(target)) { return list. get(counter); //Line 8 } counter++; //Line 10 } return -1; //Line 12}

However, there is currently an error preventing the method towork as intended. Which of the following Lines needs to be modifiedin order for the method to work as intended?

Line 1 - The method should be returning a String value, not anint.

Line 4 - The loop should go to < list. size() - 1 so as toavoid an .

Line 8 - The return should be the counter, notlist. get(counter).

Line 10 - As written, the counter will skip every othervalue.

Line 12 - The return value should be a String, not an int.

Question: 19

A company organizes all of its client database in order of theamount of money that the account is worth. When a new account isadded, it is placed in the correct order in the database based onthe worth of the account. Which classic algorithm would be bestsuited to perform this function?

Linear/Sequential Search

Insertion Sort

Selection Sort

None of these are viable options

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 25.06.2019 06:30, twistedhyperboles
How are slides deleted from a presentation?
Answers: 1
image
Computers and Technology, 25.06.2019 08:00, india73
When date is processed into a meaningful form, i becomes
Answers: 1
image
Computers and Technology, 25.06.2019 08:10, Krazyyykiddd
What refrigerant has been approved for new household refrigerators and freezers
Answers: 1
image
Computers and Technology, 25.06.2019 16:00, eggyhz1980
What is the play area? a. the frame editor b. the area the player will see in the game screen c. the area right next to an active object d. the area that keeps track of events in a game
Answers: 1
Do you know the correct answer?
Question: 4 Consider the following correct implementation of the selectionsort algorithm:

Questions in other subjects:

Konu
Mathematics, 04.10.2020 09:01
Konu
Biology, 04.10.2020 09:01
Konu
Mathematics, 04.10.2020 09:01