Sorting Partially Sorted Data
- 73Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
73 questions
- 1To generalize the email-list task, treat the entries as_____rather than building a program that depends specifically on email addresses.
Hint
Think of the broader category that includes the email addresses.
Answer
data
Explanation
The task can be abstracted as merging data, so the program need not depend on the entries being email addresses.
- 2In the textbook-company example, the larger input list is already sorted by_____.
Hint
This is the ordering required for the final list.
Answer
email address
Explanation
TextBooksAreUs's larger list is sorted by email address, which is also the required ordering of the merged result.
- 3LittleTexts's input list is sorted by_____rather than by email address.
Hint
This is the part of an email address after the at sign.
Answer
domain
Explanation
LittleTexts's list is ordered by domain, so it is not already in the order required for the final list.
- 4The desired output is the two input lists combined and ordered by_____.
Hint
Use the ordering specified for the final list.
Answer
email address
Explanation
The result must contain entries from both lists in email-address order.
- 5Because people may buy from both companies, the input lists are likely to contain_____values.
Hint
These are repeated entries appearing in more than one place.
Answer
duplicate
Explanation
Customers can purchase from both companies, so the same email address may occur in both input lists.
- 6A duplicate email address can appear twice in the merged array while appearing only once in the output_____.
Hint
This is the saved result, where duplicates are removed.
Answer
file
Explanation
The combined array retains both input occurrences, but the file-writing method is designed to omit repeated values.
- 7A useful test case for duplicate handling places the same value in both inputs; the merged array should then contain two_____entries.
Hint
The example uses this repeated letter as its test value.
Answer
j
Explanation
The test data includes a j in each input, so the merged array should contain two j values before duplicate removal during file writing.
- 8Testing cases such as an empty input or a one-element input helps check an algorithm's_____cases.
Hint
These are edge conditions at the limits of the input.
Answer
boundary
Explanation
Boundary cases include no sorted data, no unsorted data, and inputs containing only one element.
- 9If the sorted input has no entries, its file should contain the count_____rather than be empty.
Hint
The file format begins with the number of entries.
Answer
zero
Explanation
An empty file would violate the specified format, so a file with no entries must still contain a count of zero.
- 10When designing a solution, seeking opportunities to make the program as_____as possible makes it useful beyond one specific dataset.
Hint
This describes a program that works for a broad class of inputs.
Answer
general
Explanation
A general program focuses on the underlying data problem rather than unnecessary details of one application.
- 11One proposed way to detect duplicates in unsortedData is to use_____on sortedData.
Hint
This search method works by repeatedly narrowing a sorted search range.
Answer
binary search
Explanation
Because sortedData is already ordered, binary search can be used to look for each unsorted value in it.
- 12In Java, the utility method mentioned for searching an array is_____.
Hint
It is the search method belonging to the Arrays class.
Answer
Arrays.binarySearch
Explanation
Arrays.binarySearch provides binary-search functionality for arrays.
- 13The Arrays class provides utilities for_____, whereas the Collections class provides utilities for collections.
Hint
The class name itself indicates the structure it supports.
Answer
arrays
Explanation
Arrays is the utility class used for operations on arrays, including binary search and sorting.
- 14Although binary search is cheaper than linear search, searching a very large sorted list for many values can still consume significant_____.
Hint
This is the computational resource that repeated searches use.
Answer
time
Explanation
Binary search is more efficient than linear search, but the searches still take time, especially when performed against a large dataset.
- 15An alternative to searching for duplicates before sorting is to remove them while_____the output file.
Hint
This happens when the final results are saved.
Answer
writing
Explanation
The method can compare adjacent sorted values and write only the values that are not duplicates.
- 16Removing duplicates during file writing is simple, but may be a poor choice if the data contains_____duplicates.
Hint
Consider the case in which repeated values are extremely common.
Answer
lots of
Explanation
If many input values are duplicates, sorting and then discarding most of them may waste work, so the choice of strategy can be wrong.
- 17Putting both lists into one array and calling_____is simple, but it ignores the fact that part of the data is already sorted.
Hint
This is the Java array-sorting utility used in the straightforward approach.
Answer
Arrays.sort
Explanation
Sorting the entire combined array is easy to program, but it does not take advantage of the existing sorted input.
- 18Sorting the entire combined array may be reasonable when the main constraint is the_____required to write the program.
Hint
This is the development resource that a short implementation can save.
Answer
time
Explanation
Calling Arrays.sort is a short way to write the program, so it can be suitable when implementation time matters more than efficiency.
- 19The course named as developing better ways to compare algorithm choices is_____.
Hint
It is a later computer-programming course focused on organizing and processing data.
Answer
Data Structures
Explanation
The lecture notes that more systematic tools for comparing strategies are developed in Data Structures.
- 20In selection sort, each pass first finds the_____value among the candidates.
Hint
Selection sort chooses the value that belongs next in ascending order.
Answer
smallest
Explanation
Selection sort finds the smallest remaining value and places it in the next position of the result order.
- 21When adapting selection sort, the candidates for the next smallest value can include the next value in sortedData and values in_____.
Hint
These are the values not yet incorporated into the sorted portion.
Answer
unsortedData
Explanation
The next selection can be found by comparing the current sorted-data candidate with the remaining unsorted values.
- 22After selection sort finds the next smallest value, it places that value at the current_____.
Hint
This is the position the outer pass is currently filling.
Answer
index
Explanation
The selected minimum is swapped into the current index, extending the correctly ordered prefix.
- 23In the adapted selection-sort method, the initial minimum candidate for each pass is the value at the current_____.
Hint
The outer loop uses this position to track the next place to fill.
Answer
index
Explanation
Whether the current position lies in the original sorted portion or the unsorted portion, the minimum search begins with that position as its initial candidate.
- 24When selection sort is processing a position still within the original sorted portion, its search through the remaining candidates begins at_____.
Hint
This is the boundary between the original sorted and unsorted portions.
Answer
sorted size
Explanation
For an index less than sorted size, the remaining candidates to examine begin at the start of the unsorted portion.
- 25When the current selection-sort position is already in the unsorted portion, the remaining search begins at the_____position.
Hint
The current index has already been used as the initial minimum candidate.
Answer
next
Explanation
The algorithm does not need to compare the current index to itself again, so it begins examining candidates at the next index.
- 26After the first swap in adapted selection sort, values from the original sorted portion become mixed with values from the_____portion.
Hint
This portion contains the entries not yet placed in final order.
Answer
unsorted
Explanation
Swapping a selected minimum into the current position moves values between the original sorted and unsorted regions.
- 27Although selection sort mixes the original regions after swapping, it can preserve the useful ordering because displaced values are still larger than the positions the algorithm has already_____.
Hint
The algorithm has already completed these earlier positions.
Answer
processed
Explanation
The swaps move larger values into later positions, so the earlier completed part remains correctly ordered.
- 28In Java, strings should be ordered using_____rather than the less-than operator.
Hint
This String method returns a negative value when its receiver is lexicographically smaller.
Answer
compareTo
Explanation
Strings are objects, so the algorithm uses compareTo; a negative result indicates that the compared string is smaller.
- 29In the selection-sort implementation, a swap is unnecessary when the minimum index is already equal to the current_____.
Hint
This is the position where the selected minimum would be placed.
Answer
index
Explanation
If minIndex equals index, the smallest value is already in its correct position, so no swap is needed.
- 30Insertion sort begins with a group of_____data and inserts unsorted elements into it one at a time.
Hint
The group is already in the desired order before new values are added.
Answer
sorted
Explanation
Insertion sort grows a sorted region by inserting each new unsorted value into its proper position.
- 31When adapting insertion sort to the two-input problem, the algorithm can start at a_____point in the combined data.
Hint
The starting point is at the boundary between the initial sorted and unsorted regions.
Answer
midway
Explanation
The original sorted list supplies the initial sorted portion, so insertion sort can begin inserting at the point where the unsorted data starts.
- 32A major advantage of adapting insertion sort is that it requires only a minor change to an existing_____.
Hint
The change is described as adjusting one loop's starting condition.
Answer
algorithm
Explanation
The adaptation can largely reuse insertion sort, changing the starting condition of a loop rather than rewriting the method.
- 33Both adapted selection sort and adapted insertion sort can require many_____, which are expensive for large arrays.
Hint
This operation exchanges the values in two positions.
Answer
swaps
Explanation
The lecture observes that either adaptation may move many values by swapping, an important cost when the datasets are enormous.
- 34A two finger merge first sorts the smaller input array, for example by calling_____.
Hint
This is the array utility method used to order the smaller list.
Answer
Arrays.sort
Explanation
The two finger approach sorts the smaller array and then merges it with the array that was already sorted.
- 35During a two finger merge, the algorithm repeatedly chooses the smaller of the two current candidate values and places it in the_____.
Hint
This is the destination that holds the merged ordering.
Answer
result array
Explanation
Comparing the current values from both sorted inputs lets the algorithm append the next smallest value to the result array.
- 36Once the smallest remaining value in one input is known, a two finger merge does not need to compare it with later values in that same input until that current value is_____.
Hint
The current candidate leaves its position when it is selected for the output.
Answer
moved
Explanation
Because each input is sorted, its current value is no larger than its later values, so the algorithm can continue comparing the other input against that current candidate.
- 37The key storage disadvantage of a two finger merge is that it requires a_____to hold the merged results.
Hint
The output cannot be stored solely in the original input array in this approach.
Answer
second array
Explanation
The merge writes results to an additional array, increasing the storage needed.
- 38For enormous input arrays, a two finger merge's extra result array may_____, making its storage cost unacceptable.
Hint
The added array is comparable in size to the data being merged.
Answer
double the amount of storage
Explanation
The extra array can double storage requirements, which may prevent the program from working at all.
- 39Selection sort was favored over insertion sort in the example because it appeared potentially more_____.
Hint
This quality concerns how economically the algorithm uses computational resources.
Answer
efficient
Explanation
The final choice leaned toward selection sort because efficiency was the reason for avoiding the simple full-array sort.
- 40Insertion sort was considered easier to implement because it required only a_____to an existing algorithm.
Hint
The lecture contrasts this with writing a more complex adaptation.
Answer
minor change
Explanation
Insertion sort's loop can start at a different point, making its adaptation comparatively straightforward.
- 41Calling a file-reading method twice to create separate arrays would use more storage because the data would later need to be_____.
Hint
The separate arrays must ultimately be brought together.
Answer
combined
Explanation
Reading each file into its own array and then creating a combined array duplicates storage, contrary to the efficiency goal.
- 42To avoid holding separate arrays for the two inputs, the proposed read method reads both files into_____.
Hint
The method returns a single combined data structure.
Answer
one array
Explanation
The design reads entries from both files into one string array, avoiding separate input arrays that would later need to be combined.
- 43A method that returns only the combined array does not also return the sorted portion's size, creating a_____problem.
Hint
The concern is how the method communicates both the array and its boundary information.
Answer
return value
Explanation
The caller needs the array and the size of the first file, but a simple single return value does not conveniently supply both.
- 44The chosen design reads the file sizes in the_____method and reads the entries in another method.
Hint
This is the program's starting method, where the file-reading setup is performed.
Answer
main
Explanation
Reading sizes in main makes those values available when calling the other methods, while a separate method reads the data.
- 45The read-files method takes a scanner for each input and the number of entries in each file, then returns a_____.
Hint
It is a single array containing the entries read from both files.
Answer
string array
Explanation
The method accepts the two scanners and their file sizes, reads the entries into one array, and returns that array.
- 46The save-to-file method needs the output file name and the_____to be saved.
Hint
This is the collection of values produced by the merge and sort.
Answer
data array
Explanation
The saving method uses the destination name and the data array whose entries it writes.
- 47In the program design, the sorting method receives the combined data array and the_____.
Hint
This tells the algorithm where the originally sorted portion ends.
Answer
sorted size
Explanation
The sorting method uses the sorted size to distinguish the initial sorted portion from the unsorted portion when selecting candidates.
- 48The input file format begins with the number of email addresses, followed by one email address per_____.
Hint
Each address occupies its own row of text.
Answer
line
Explanation
Writing a count first and then one value per line is a common file format.
- 49Including the entry count at the start of a file allows the program to construct an array directly without first reading the file once just to determine its_____.
Hint
This is the amount of data that determines the array's length.
Answer
size
Explanation
The known count lets the program allocate the correctly sized array before reading the entries, avoiding a preliminary pass.
- 50When reading both files into one array, the implementation shares one_____across the two loops.
Hint
This variable tracks the next array position to fill.
Answer
index
Explanation
The first loop fills positions with sorted-file entries, and the second continues from the same index for unsorted-file entries.
- 51The first read loop stores sorted-file entries until the index reaches the_____.
Hint
This count is the length of the first portion of the combined array.
Answer
sorted size
Explanation
The first loop fills the array's initial section with the number of entries recorded for the sorted file.
- 52The second read loop continues until the index reaches the sum of the sorted and unsorted_____.
Hint
Together, the two counts give the total array length.
Answer
sizes
Explanation
The second loop reads the unsorted entries into the remaining positions, stopping at the combined total.
- 53Declaring the shared index inside the first for loop would limit its_____to that loop.
Hint
This programming concept determines where a variable can be accessed.
Answer
scope
Explanation
The index must also be available to the second loop, so it is declared before the first loop rather than inside it.
- 54The program uses Scanner's_____method to read each address as a line of text.
Hint
This method reads through the end of the current line.
Answer
nextLine
Explanation
Each address is stored one per line, so nextLine reads each entry as a string.
- 55A file-reading loop can check_____to detect a file that contains fewer lines than its declared count.
Hint
This Scanner method indicates whether another line is available.
Answer
hasNextLine
Explanation
Checking hasNextLine can reveal missing data when the file's contents do not match the count it declares.
- 56After reading the sorted file, a mismatch can be detected by checking whether the index equals the declared_____.
Hint
The first file is expected to contribute exactly this many entries.
Answer
sorted size
Explanation
If the final index is not sorted size, the sorted file may have missing or unexpected data relative to its declared count.
- 57After both input files are read, the expected final index is the sum of the sorted and unsorted_____.
Hint
The combined array must contain the declared count from each file.
Answer
sizes
Explanation
The total number of entries expected after both loops is sorted size plus unsorted size.
- 58If the files are corrupted, the program should_____the user because the resulting data may not be processed correctly.
Hint
The program should communicate that an error has occurred.
Answer
warn
Explanation
A file-format or data mismatch can make the program's result unreliable, so the user should be informed.
- 59A method that writes data to a file needs a_____rather than a Scanner.
Hint
This class is used for output, while Scanner is used for input.
Answer
PrintWriter
Explanation
PrintWriter provides methods such as println for writing values to a file.
- 60The save method writes the number of array elements before writing the individual values by using the array's_____property.
Hint
This property gives the number of elements in an array.
Answer
length
Explanation
Writing data.length first records the number of entries so the output follows the specified file format.
- 61After finishing file output, the program should_____the file writer.
Hint
This releases the file resource after writing is complete.
Answer
close
Explanation
Closing files is an important part of safely managing file resources.
- 62To remove adjacent duplicate strings while writing sorted data, the program compares strings with the_____method.
Hint
This method checks whether two string values are equal.
Answer
equals
Explanation
For strings, value equality should be checked with equals rather than with a primitive comparison operator.
- 63A duplicate check for the current output value compares data at index i with data at index_____.
Hint
In sorted data, a repeated value will be next to the previous value.
Answer
i−1
Explanation
Because the array is sorted, duplicates are adjacent, so each value can be checked against the preceding element.
- 64The first array element must be handled separately because comparing it with the previous element would use index_____.
Hint
Subtracting one from the first valid index produces an invalid position.
Answer
−1
Explanation
When the current index is zero, there is no preceding element, so the first value needs special handling.
- 65The first output value should be printed only if the array's length is not equal to_____.
Hint
This is the length of an empty array.
Answer
0
Explanation
Checking that the array is nonempty prevents the program from trying to print an element that does not exist.
- 66The condition that checks whether the current index is greater than zero should come before the comparison with the previous element, relying on_____evaluation.
Hint
With this behavior, the second condition is skipped if the first condition is false.
Answer
short-circuit
Explanation
Short-circuit evaluation prevents access to data at index −1 when the current index is zero.
- 67Because duplicate entries are omitted from the file, the output count must equal the array length minus the number of_____.
Hint
These are the repeated values that will not be written a second time.
Answer
duplicates
Explanation
The file's leading count must describe the number of values actually printed, so duplicate occurrences are subtracted from the array length.
- 68To calculate the correct output count, the program increments the duplicate counter when adjacent strings are_____.
Hint
This is the condition that identifies a repeated value.
Answer
equal
Explanation
The duplicate counter should increase when neighboring sorted values match; nonmatching values are not duplicates.
- 69If the array contains thirteen entries and one repeated occurrence is omitted, the output file should report_____entries.
Hint
Subtract the one duplicate occurrence from the total array length.
Answer
twelve
Explanation
The file count must match the number of values written, so thirteen array entries with one duplicate occurrence produce twelve output entries.
- 70A successful run should be checked by inspecting the output file, since a program that does not crash may still produce_____results.
Hint
Not breaking is not the same as producing the intended answer.
Answer
incorrect
Explanation
The initial run produced sorted values but still wrote a duplicate, illustrating why output must be validated rather than assuming success.
- 71Understanding algorithm details gives programmers better building_____for constructing programs.
Hint
These are reusable components that can be combined into larger solutions.
Answer
blocks
Explanation
Algorithms are building blocks for programs; understanding them expands the strategies available for solving new problems.
- 72If a programmer knows only the general array-sorting API method, the obvious approach may be less_____than adapting a suitable algorithm to exploit existing order.
Hint
The issue is how well the approach uses time and storage.
Answer
efficient
Explanation
Knowing selection sort or insertion sort provides alternatives to sorting the entire combined array without using the already sorted input.
- 73The main reason for studying algorithm details, even when APIs are available, is to gain more informed choices about how to_____a problem.
Hint
The choices concern selecting and adapting methods to meet program constraints.
Answer
solve
Explanation
Understanding algorithms lets programmers choose and adapt appropriate building blocks instead of relying only on a general-purpose API call.

Ask anything
We've read this whole study set, so questions about it get the best answers.