Exam 12: Searching and Sorting

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

Write a method that accepts an integer and an array of integers, and returns true if the integer is contained in the array. You may assume that the array is sorted, and your method should use a binary search..

Free
(Essay)
4.9/5
(35)
Correct Answer:
Verified

public boolean binarySearch(int a, int[] array) {
int first = 0;
int last = array.length-1;
int mid;
while(first <= last) {
mid = (first+last)/2;
if(array[mid] == a)
return true;
else if(array[mid] > a)
last = mid - 1;
else
last = mid + 1;
}
return false;
}

____________________ is the process of arranging a group of items into a defined order.

Free
(Multiple Choice)
4.9/5
(38)
Correct Answer:
Verified

B

How many times will the following code print out Hello World (in terms of the length of the array)? for(int i = 0; i < array.length; i++) { for(int j = 0; j < array.length; j++) { for(int k = 0; k < array.length; k++) { System.out.println("Hello World!"); } } }

Free
(Essay)
4.9/5
(38)
Correct Answer:
Verified

Write a method that accepts an integer and an array of integers, and returns true if the integer is contained in the array. The method should use a linear search.

(Essay)
4.8/5
(43)

Explain why a faster computer can never make an exponential time algorithm run faster than a polynomial time algorithm on all inputs.

(Essay)
4.9/5
(33)

_____________________ is the process of finding a designated target element within a group of items.

(Multiple Choice)
4.8/5
(44)

What is the complexity of the following code (in terms of the length of the array), assuming someMethod has a complexity of O(1)? for(int i = 0; i < array.length; i++) for(int j = 0; j < array.length; j++) someMethod(array[j]);

(Short Answer)
4.9/5
(42)

As the number of items in a search pool grows, the number of comparisons required to search _______________ .

(Multiple Choice)
4.8/5
(33)

Write out the state while being sorted using the insertion sort algorithm: Write out the state while being sorted using the insertion sort algorithm:

(Essay)
4.8/5
(38)

Which of the following is not a sorting algorithm?

(Multiple Choice)
4.9/5
(32)

Explain what O(1) means.

(Essay)
4.8/5
(35)

Which of the following algorithms has a time complexity of O(log2 n)?

(Multiple Choice)
4.8/5
(42)

A linear search is more efficient than a binary search.

(True/False)
4.9/5
(41)

Bubble sort is the most efficient searching algorithm.

(True/False)
4.9/5
(35)

Suppose we have algorithms that solve a particular problem that have the following complexities. Which one is most efficient?

(Multiple Choice)
4.8/5
(44)

In the binary search algorithm, if the number of elements in the search pool is even, which value is used as the midpoint? Explain.

(Essay)
4.7/5
(35)

A _______________ search is more efficient than a linear search.

(Multiple Choice)
5.0/5
(29)

Suppose we would like to swap the elements at index i and index j in an array. Does the following code work? If so, explain how. If not, explain why it fails. array[i] = array[j]; array[j] = array[i];

(Essay)
4.9/5
(39)

A __________________ search looks through the search pool one element at a time.

(Multiple Choice)
4.7/5
(39)

The ___________________ algorithm sorts values by repeatedly comparing neighboring elements in the list and swapping their position if the are not in order relative to each other.

(Multiple Choice)
4.8/5
(33)
Showing 1 - 20 of 40
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)