AP JAVA: Searching & Sorting

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following algorithms should stop if no swaps of elements have taken place on a pass through the list?

Bubble sort

Which will cause the shortest execution of a binary search looking for a value in an array of integers?

The value is in the middle of the array

In which of these cases will an ascending order (from smallest to largest) insertion sort have the fastest run time? I. An array that is in reverse order (from largest to smallest). II. An array that is in sorted order already (from smallest to largest). III. An array that is in random order (not already sorted). (A) II only (B) I only (C) I and II only (D) II and III only (E) III only

A

Under what condition will an insertion sort execute faster? (A) If the data is already sorted in the correct order (B) It will always take the same amount of time to execute

A

Insertion Sort

1) Looks at one item at a time finding largest/smallest value 2) Looks at next item to place in sorted list **One item at a time** Separates the list into sorted and unsorted sections (sorted section initially empty). Compares an item in the unsorted section with the items in the sorted section, and places it in the correct position in the sorted section. After each step, the sorted section gets bigger, and the unsorted section gets smaller, until the entire list is sorted. Orders a value by repetitively inserting a particular value into a sorted subset of the list - Consider the first item to be a sorted sublist of length 1 - Insert the second item into the sorted sublist, shifting the first item if needed - Insert the third item into the sorted sublist, shifting the other items as needed - Repeat until all values have been inserted into their proper positions

A list of 60 names has been sorted in alphabetical order. Using a binary search method, what is the minimum number of passes needed to find a specified name or confirm that it is not in the list?

6

A database containing 500 sorted integers must be searched using a binary search algorithm. What is the maximum number of iterations of the binary search method that must occur in order to find a specified value or guarantee that it is not in the database?

9

How many times would the while loop execute if you first do int[] arr = {6, 12, 19, 54, 76, 99} and then call binarySearch(arr,76)? (A) 2 (B) 1 (C) 3

A

Which of the following correctly shows the iterations of an ascending (from left to right) insertion sort on an array with the following elements: {7,3,8,5,2}? (A) {3,7,8,5,2}, {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8} (B) {2,3,8,5,7}, {2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8} (C) {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8} (D) {2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8} (E) {2,7,3,8,5}, {2,3,7,8,5}, {2,3,5,7,8}

A

Which will cause the shortest execution of a sequential search looking for a value in an array of integers? (A) The value is the first one in the array (B) The value is in the middle of the array (C) The value is the last one in the array (D) The value isn't in the array

A

array list

A Java class that implements a dynamically growable array of objects

Merge Sort

A list is split into individual lists, these are then combined (2 lists at a time). Uses the divide and conquer strategy. Recursively breaks the values to be sorted in half, until there is only one value left to be sorted. Then, it merges the two sorted lists back into one list. Orders values by recursively dividing the list in half until each sub-list has one element, then recombining - divide the list into two roughly equal parts - recursively divide each part in half, continuing until a part contains only one element - merge the two parts into one sorted list - continue to merge parts as the recursion unfolds DIVIDING ITSELF IN HALF REPEATEDLY OR MERGING SORTED ELEMENTS*

The elements in a long list of integers are roughly sorted in decreasing order. No more than 5% of the elements are out of order. Which of the following is a valid reason for using an insertion sort rather than a selection sort to sort this list into decreasing order 1. There will be fewer comparisons of elements for insertion sort 2. There will be fewer changes of position of elements for insertion sort 3. There will be less space required for insertion sort. (A) I only (B) 2 only (C) 3 only (D) I and 2 only (E) 1, 2 and 3

A. E.g consider the array 10 8 9 6 2 For insertion sort - need 4 passes through this array 1st pass compares 8 and 10 - 1 comparison, no moves 2nd pass compares 9 and 8 and then 9 and 10. The array becomes 10 9 8 6 2 - 2 comparisons, 2 moves The 3rd and 4th pass compares 6 and 8 and 2 and 6 - no moves In summary there are approximately 1 or 2 comparisons per pass and no more then 2 moves per pass For selection sort, there are 4 passes too. The 1st pass finds the biggest element in the array and swaps it into the first position. The array is still 10 8 9 6 2 - four comparisons. There are 2 moves if your algorithm makes the swap in this case, otherwise no moves. The 2nd pass finds the biggest element from a[1] to a[4] and swaps it into the second position: 10 9 8 6 2 - 3 comparisons, 2 moves For the 3rd pass there are 2 comparisons and 1 for the 4th. There are 0 or 2 moves each time. Summary: 4+3+2+1 total comparisons and a possible 2 moves per pass Selection sort makes the same number of comparisons irrespective of the state of the array. Insertion sort does far fewer comparisons if he array is almost sorted. There are roughly the same number of data movements for insertion and selection. Insertion may have more changes depending on how far from their insertion points the unsorted elements are. Insertion and selection sorts have the same space requirements

A database containing 2,000 sorted integers must be searched using a binary search algorithm. What is the maximum number of iterations of the binary search method that must occur in order to find a specified value or guarantee that it is not in the database? (A) 8 (B) 11 (C) 20 (D) 100 (E) 2000

B

A list of 120 names has been sorted in alphabetical order. Using a binary search method, what is the minimum number of passes needed to find a specified name or confirm that it is not in the list? (A) 5 (B) 7 (C) 10 (D) 12 (E) 128

B

How many times would the while loop execute if you first do int[] arr = {2, 10, 23, 31, 55, 86} and then call binarySearch(arr,2)? (A) 1 (B) 2 (C) 3

B

Under what condition will an ascending insertion sort execute the slowest? (A) If the data is already sorted in ascending order (B) If the data is already sorted in descending order (C) It will always take the same amount of time to execute

B

Which will cause the shortest execution of a binary search looking for a value in an array of integers? (A) The value is the first one in the array (B) The value is in the middle of the array (C) The value is the last one in the array (D) The value isn't in the array

B

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

Bubble sort

A worst case situation for insertion sort would be 1. A list in correct sorted order 2. A list sorted in reverse order 3. A list in random order. (A) I only (B) II only (C) III only (D) I and II only (E) II and III only

B. If the list is sorted in reverse order, each pass through the array will involve the maximum possible number of comparisons and the maximum possible number of element movements if an insertion sort is used

Array unsortedArr contains an unsorted list of integers. Array sortedArr contains a list of integers sorted in increasing order. Which of the following operations is more efficient for sortedArr than unsortedArr? Assume the mist efficient algorithms are used. 1. Inserting a new element 2. Searching for a given element. 3. Computing the mean of the elements A. 1 only B. 2 only C. 3 only D. 1 and 2 only E. 1, 2 and 3

B. Inserting a new element is quick and easy in an unsorted array - just add it to the end of the list Computing the mean involves finding the sum of the elements and dividing by n, the number of elements. The execution is same whether the list is sorted or not. Operation 2, searching for a given element is inefficient for an unsorted list, since a sequential search must be used. In sortedArr, the efficient binary search algorithm, which involves a fewer comparisons could be used. In fact, in a sorted list, even a sequential search would be more efficient than for an unsorted list: If the search item were not in the list, the search could stop as soon as the list elements were greater than the search item.

Which search is a divide and conquer method?

Binary

A list of integers containing [12, 8, 7, 30, 62, 45, 10, 3] is sorted from largest to smallest using a selection sort method. After three passes, what does the list look like? (A) [62, 45, 30, 12, 7, 8, 10, 3] (B) [30, 12, 8, 7, 62, 45, 10, 3] (C) [62, 45, 30, 7, 12, 8, 10, 3] (D) [62, 45, 30, 12, 7, 8, 10, 3] (E) [12, 8, 30, 7, 62, 45, 10, 3]

C

Under what condition will a merge sort execute faster? (A) If the data is already sorted in ascending order (B) If the data is already sorted in descending order (C) It will always take the same amount of time to execute

C

Under what condition will a selection sort execute faster? (A) If the data is already sorted in ascending order (B) If the data is already sorted in descending order (C) It will always take the same amount of time to execute

C

Which of the following conditions must be true in order to search for a value using binary search? I. The values in the array must be integers. II. The values in the array must be in sorted order. III. The array must not contain duplicate values. (A) I only (B) I and II (C) II only (D) II and III

C

Which sort should be the fastest most of the time? (A) selection sort (B) insertion sort (C) merge sort

C

Which of the following is a valid reason why merge sort is a better sorting algorithm than insertion sort for sorting long, randomly ordered lists ? 1. Merge sort requires less code than insertion sort 2. Merge sort requires less storage space than insertion sort. 3. Merge sort runs faster than insertion sort. (A) I only (B) 2 only (C) 3 only (D) I and 2 only (E) 2 and 3 only

C. Merge sort requires both a merge and mergeSort method - more code than the relatively short and simple code for insertion sort. The merge algorithm uses a temporary array, which means more storage space than insertion sort. For long lists, the "divide-and-conquer" approach of merge sort gives it a faster run time than insertion sort

A feature of data that is used for a binary search but not necessarily used for a sequential search is A. Length of the list B. type of data C. order of data D. smallest value in the list E. median value of the data

C. The Binary search algorithm depends on the array being sorted. The sequential search has no ordering requirement. Both depend on choice A, the length of the list, while the other choice are irrelevant to both algorithms

Bubble Sort

Compares each item with the next item in the list, and swaps them if needed. Imagine that the largest/smallest item in the array (depending on sorting order) is being raised to the top of the list. Stops when it makes a pass through the entire list without swapping. Orders a list of values by repetitively comparing neighbouring elements and swapping their positions if necessary. - scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top - scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order

Consider an array of integers that contains [12, 8, 4, 6, 13, 29, 7]. If the array is sorted from smallest to largest using an insertion sort method, what will be the order of the array after the third iteration of the sorting method? (A) [4, 6, 12, 8, 13, 29, 7] (B) [4, 6, 7, 8, 13, 29, 12] (C) [4, 8, 12, 6, 13, 29, 7] (D) [4, 6, 8, 12, 13, 29, 7] (E) [4, 6, 7, 8, 12, 13, 29]

D

The array intArray contains [8, 12, 34, 6, 10, 14, 2, 4]. What are the contents of intArray after 3 passes of insertion sort? (A) [6, 8, 12, 34, 2, 4, 10, 14] (B) [2, 4, 6, 34, 10, 14, 8, 12] (C) [2, 4, 34, 6, 10, 13, 8, 12] (D) [6, 8, 12, 34, 10, 14, 2, 4] (E) [6, 8, 10, 12, 34, 14, 2, 4]

D

Which of the following correctly shows the iterations of an ascending (from left to right) selection sort on an array with the following elements: {10, 6, 3, 2, 8}? (A) {6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,10,8}, {2,3,6,8,10} (B) {6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,8,10} (C) {2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,8,10} (D) {2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,10,8}, {2,3,6,8,10}

D

Which will cause the longest execution of a sequential search looking for a value in an array of integers? (A) The value is the first one in the array (B) The value is in the middle of the array (C) The value is the last one in the array (D) The value isn't in the array

D

Assume that merge sort will be used to sort an array arr of n integers into increasing order. What is the purpose of the merge method in the merge sort algorithm ? A. Partition arr into 2 parts of roughly equal length, then merge these parts. B. Use a recursive algorithm to sort arr into increasing order. C. Divide arr into n subarrays, each with 1 element. D. Merge 2 sorted parts of arr into a single sorted array. E. Merge 2 sorted arrays into a temporary array that is sorted.

D Recall the merge sort algorithm 1. Divide arr into 2 parts 2. Merge sort the left side 3. Merge sort the right side. 4. Merge the 2 sides into a single sorted array Merge method is used for the last step of the algorithm. It does not do any sorting or partitioning of the array - eliminates choices A,B and C. Choice E is wrong because merge starts with a single array that has 2 sorted parts.

A sorted array of integers containing 2000 elements is to be searched for key using a binary search method. Assuming key is in the array, what is the maximum number of iterations needed to find key? (A) 8 (B) 10 (C) 100 (D) 2000 (E) 11

E

Which of the following is/are true about using insertion sort versus using merge sort? I. Insertion sort requires more storage space than mergesort. II. Insertion sort is only more efficient than mergesort in the case that we have a very small and nearly sorted array. III. Insertion sort is almost always less efficient than mergesort. (A) I only (B) II only (C) III only (D) I and III (E) II and III

E

The decision to choose a particular sorting algorithm should be made based on 1. Run-time efficiency of the sort 2. size of the array 3. space efficiency of the algorithm A. 1 only B. 2 only C. 3 only D. 1 and 2 only E. 1, 2 and 3

E. The time and space requirements of sorting algorithms are affected by all three of the given factors, so all must be considered when choosing a particular sorting algorithm.

A linear search always requires more comparisons than a binary search.

False

Bubble sort is the most efficient searching algorithm

False

Linear Search

Finding a key value within a list, ++More efficient w/ sorted list Linear Search - In linear search, each element of the array is compared with a search key. A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted This approach does not assume the items in the search pool are in any particular order

sequential search algorithm

Finds the index of a targeted value in an array, regardless of if it is sorted. Starts from the beginning of the array, and goes through the entire array until it finds the target value.

Which of the following conditions must be true in order to search for a value using binary search? I. The values in the array must be integers. II. The values in the array must be in sorted order. III. The array must not contain duplicate values.

II only

The __________________ algorithm sorts a list of values by repetitively inserting a particular value into a subset of the list that has already been sorted.

Insertion sort

In a binary search, ______________________________.

It is assumed that the search pool is ordered In a binary search, ______________________________.

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

Linear

Binary Search Algorithm

Loop: 1) Must be sorted first 2) Divide list into two 3) Analyze two lists weather it is in one locationN Finds the index of a targeted value in a SORTED array. Uses a "search space" (initially the entire array) to find the target. Each step, it compares the median value in the search space to the target. Based on the comparison, it can remove half of the search space every step, until the search space consists of only the target value.

selection sort algorithm

Nested Loop 1) repeatedly finds the largest/smallest values in the unsorted section 2) Swaps to the correct position, placed in sorted section **Swap** Finds the largest/smallest item in the array (depending on sorting order), and then swaps it with the item in the next position to be filled. For example, the first step finds the largest/smallest item in the array, then swaps it with the first item in the array, and so on. Orders a list of values by repetitively putting a particular value into its final position - find the smallest value in the list - switch it with the value in the first position - find the next smallest value in the list - switch it with the value in the second position - repeat until all values are in their proper places example: 3 6 1 7 --> 1 3 6 7

Quick Sort

Orders values by partitioning the list around one element, then sorting each partition - choose one element in the list to be the partition element - organize the elements so that all elements less than the partition element are to the left and all greater are to the right -apply the quick sort algorithm (recursively) to both partitions

Which of the following algorithms is most easily expressed recursively?

Quick Sort

Quick sort works by separating a list into two lists, and recursively sorting the two lists using quick sort

True

With each comparison, a binary search eliminates approximately half of the items remaining in the search

True

T/F All the sorting algorithms have greater run times than a sequential search.

True. This is because a sequential search looks at each element once. A sorting algorithm, however, processes other elements in the array for each element it looks at.

When will a sequential search be faster than a binary search?

When it is the first one

Which will cause the longest execution of a sequential search?

When it is the last one

recursion function

a function that calls itself

for-each loop

for (Type variable: collection){ Statement }

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

increases

2D array

rows and columns, arranged in a grid format, but still stored in contiguous, or side-by-side memory, accessed using two index values.

Which of the following algorithms will have the m smallest items in correct order after the m^th pass has been completed?

selection sort


Conjuntos de estudio relacionados

Chapter 13: The Presidency Critical Thinking

View Set

obstetrics/maternity practice quiz

View Set

Obj. 1.01 Nature of Marketing and Functioning

View Set

Chapter: 18 Sterilization and Disinfection

View Set

Exam I Money and the Financial Systems: Ch4

View Set