Sorting Partially Sorted Data
- 54Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which aspect of the textbook-merging problem can be generalized without relying on the data being email addresses?
Which aspect of the textbook-merging problem can be generalized without relying on the data being email addresses?
54 questions
- 1
Which aspect of the textbook-merging problem can be generalized without relying on the data being email addresses?
- A
Merging one sorted collection and one unsorted collection into sorted output
- B
Sorting email addresses by their domain names
- C
Identifying duplicate domains in the input files
- D
Determining which email addresses belong to textbook buyers
Correct answer
AMerging one sorted collection and one unsorted collection into sorted output
Explanation
The central operation is merging sorted and unsorted data into sorted output. It can be described generically unless the sorting criterion itself depends on email-specific properties such as domain names.
- A
- 2
Why does sorting by domain name make the email-specific nature of the data more important?
- A
A domain-sorted list cannot be represented as an array
- B
The sorting criterion then depends on a property specific to email addresses
- C
Email addresses cannot be compared using any sorting algorithm
- D
Domain sorting guarantees that the two input lists have no duplicates
Correct answer
BThe sorting criterion then depends on a property specific to email addresses
Explanation
Sorting by domain requires using a feature of email addresses, whereas sorting by a generic value criterion can be framed as a more general data-merging problem.
- A
- 3
In the example, how are the two input lists ordered before they are merged?
- A
Both lists are sorted by email address
- B
The larger list is sorted by email address, and the smaller list is sorted by domain
- C
Neither list is sorted
- D
The larger list is sorted by domain, and the smaller list is sorted by email address
Correct answer
BThe larger list is sorted by email address, and the smaller list is sorted by domain
Explanation
TextBooksAreUs has the larger list sorted by email address, while LittleTexts has the smaller list sorted by domain.
- A
- 4
Why should the possibility of duplicate email addresses be considered before choosing a merging strategy?
- A
The file format automatically removes repeated addresses
- B
Customers may buy multiple items online, so the lists are likely to overlap
- C
A sorted list cannot contain repeated values
- D
Duplicates make it impossible to compare email addresses
Correct answer
BCustomers may buy multiple items online, so the lists are likely to overlap
Explanation
The same person may buy multiple items online, making repeated addresses across the lists very likely and requiring an explicit duplicate-handling policy.
- A
- 5
What is one proposed way to avoid duplicates before sorting the merged data?
- A
Write both files unchanged and rely on the file system to remove repeats
- B
Sort the sorted list by domain and compare only the first values
- C
Delete every value from the unsorted list before reading it
- D
Search the sorted list for each value in the unsorted list and remove matches
Correct answer
DSearch the sorted list for each value in the unsorted list and remove matches
Explanation
One option is to perform a binary search on the sorted data for each unsorted value and exclude any value already present.
- A
- 6
Why might checking every unsorted value against a very large sorted list be an unattractive duplicate-removal strategy?
- A
Searching changes the order of the sorted list
- B
Even binary searches take time, and repeating them for many values can be costly
- C
The sorted list must be copied before each search
- D
Binary search works only on unsorted data
Correct answer
BEven binary searches take time, and repeating them for many values can be costly
Explanation
Binary search is cheaper than linear search, but it still requires work for each searched value. Repeated searches can be expensive when the sorted data set is large.
- A
- 7
Why can removing duplicates while writing the output be a poor choice if there are very many duplicates?
- A
The output file cannot contain fewer values than the input arrays
- B
The file-writing method cannot compare strings
- C
Writing duplicates makes the sorted array lose its order
- D
The program may do substantial sorting work on values that the output later discards
Correct answer
DThe program may do substantial sorting work on values that the output later discards
Explanation
If many values are duplicates, sorting all of them and removing repeats only at output time may waste substantial work. The best choice depends partly on how common duplicates are.
- A
- 8
If a test case includes the same value in both input arrays, what should it verify when duplicates are removed only from the output file?
- A
The merged array contains one copy, but the output file contains both copies
- B
The merged array contains both copies, but the output file contains one copy
- C
Both the merged array and the output file contain no copies
- D
Both the merged array and the output file contain exactly two copies
Correct answer
BThe merged array contains both copies, but the output file contains one copy
Explanation
The example deliberately places a shared value in both inputs. The merged array should retain both occurrences, while duplicate removal during file writing should emit that value only once.
- A
- 9
Why can a file begin with the number of data items followed by one item per line?
- A
The count makes it possible to omit all line breaks from the file
- B
The count allows an unsorted list to be treated as already sorted
- C
The count guarantees that every value in the file is unique
- D
Knowing the item count lets the program allocate the array before reading its values
Correct answer
DKnowing the item count lets the program allocate the array before reading its values
Explanation
Reading the count first lets the program construct an appropriately sized array and then read the values once, instead of first scanning the file to discover its size.
- A
- 10
Under the described file format, how should a file with no data values be represented?
- A
As an empty file
- B
With a repeated copy of the previous file's count
- C
With one blank line in place of the count
- D
With a count of zero
Correct answer
DWith a count of zero
Explanation
A valid file must follow the specified format, so a file with no data contains the count zero rather than being empty.
- A
- 11
Which set of cases is especially useful for testing this merge program's boundaries?
- A
Only files whose values are already in reverse order
- B
Only two large files with no shared values
- C
Only files containing alphabetically late email addresses
- D
No data in either input, no data in one input, and inputs containing only one item
Correct answer
DNo data in either input, no data in one input, and inputs containing only one item
Explanation
Boundary testing should include empty and singleton cases, including no sorted data, no unsorted data, and cases where one or both inputs have only one element.
- A
- 12
What is the main drawback of placing all values into one array and calling the general array-sorting utility?
- A
It automatically removes every duplicate
- B
It requires the output to preserve the original order
- C
It cannot sort strings
- D
It ignores the fact that one input is already sorted and may do unnecessary work
Correct answer
DIt ignores the fact that one input is already sorted and may do unnecessary work
Explanation
A general sort is simple, but it does not take advantage of the existing sorted order. For enormous data sets, exploiting that order may improve efficiency.
- A
- 13
When might sorting the combined array with a general sorting utility still be a reasonable choice?
- A
When the input arrays are too large to fit in memory
- B
When the program must avoid using any array
- C
When minimizing the time needed to write the program matters most
- D
When duplicate values must remain in the output file
Correct answer
CWhen minimizing the time needed to write the program matters most
Explanation
Combining the data and calling a sort is the shortest implementation. It can be appropriate when development time, rather than runtime efficiency, is the main constraint.
- A
- 14
How does adapting selection sort find the next value to place in the result order?
- A
It searches for the minimum among the remaining unsorted values and the relevant sorted-data value
- B
It repeatedly swaps each value with the value immediately after it
- C
It inserts every value at the beginning of the array
- D
It selects the maximum value from the entire array on every pass
Correct answer
AIt searches for the minimum among the remaining unsorted values and the relevant sorted-data value
Explanation
Selection sort places the next minimum value. In this adaptation, that value may come from the remaining part of the sorted input or from the unsorted input.
- A
- 15
What happens to the original sorted region during the adapted selection-sort process?
- A
Swaps can mix its values with unsorted values, though the remaining ordering information can still be useful
- B
It is discarded as soon as the first swap occurs
- C
It remains completely unchanged throughout the entire algorithm
- D
It must be sorted again by domain before each comparison
Correct answer
ASwaps can mix its values with unsorted values, though the remaining ordering information can still be useful
Explanation
After the first swap, values from the sorted region can move into other positions. The sorted data is mixed with the unsorted data, but the process can still exploit the ordering information.
- A
- 16
Why can adapted selection sort be harder to implement than a small modification to insertion sort?
- A
It cannot use loops
- B
It must use two result arrays
- C
It has distinct search behavior depending on whether the current position is in the original sorted region
- D
It requires comparing email domains rather than values
Correct answer
CIt has distinct search behavior depending on whether the current position is in the original sorted region
Explanation
The selection-sort adaptation must handle two phases: searching the remaining unsorted input while positions are in the sorted region, and searching later positions once in the unsorted region.
- A
- 17
How does insertion sort naturally incorporate the unsorted values in this problem?
- A
It first discards the sorted input and sorts only the unsorted input
- B
It alternates between reversing the two input lists
- C
It starts with a sorted group and inserts unsorted values into their appropriate positions one at a time
- D
It compares each value only with the value at the end of the array
Correct answer
CIt starts with a sorted group and inserts unsorted values into their appropriate positions one at a time
Explanation
Insertion sort maintains a sorted group and adds unsorted elements to it one at a time, which fits the structure of the problem.
- A
- 18
What is a key implementation advantage of adapting insertion sort for this task?
- A
It removes the need to compare data values
- B
It guarantees that no elements will be moved
- C
It eliminates the need to know the sorted input's size
- D
It may require only a change to the loop's starting condition
Correct answer
DIt may require only a change to the loop's starting condition
Explanation
The adaptation can be viewed as starting insertion sort partway through its usual process, so relatively little existing code may need to change.
- A
- 19
What is the basic two-finger merge strategy after the smaller input has been sorted?
- A
Alternate values from the two inputs without comparing them
- B
Compare the current values at the fronts of the inputs and copy the smaller one into a result array
- C
Copy the larger current value into the sorted input and discard the smaller one
- D
Compare every value in one input against every value in the other
Correct answer
BCompare the current values at the fronts of the inputs and copy the smaller one into a result array
Explanation
Two-finger merge advances through the sorted inputs by comparing their current smallest remaining values and placing the smaller one in the result.
- A
- 20
Why does two-finger merge not need to compare the larger remaining values in one input after finding that input's current smallest value?
- A
Because the input is sorted, its current smallest remaining value is no larger than its later values
- B
Because the algorithm copies all remaining values without checking them
- C
Because the larger values are always duplicates
- D
Because all values in the two inputs are guaranteed to be equal
Correct answer
ABecause the input is sorted, its current smallest remaining value is no larger than its later values
Explanation
Once the smaller input has been sorted, its current value is the smallest remaining value there. Its later values cannot precede that value, so the merge need not inspect them yet.
- A
- 21
What makes the two-finger merge approach potentially unacceptable for enormous arrays in this example?
- A
It requires the input files to contain identical values
- B
It needs a separate result array, which can double the storage requirement
- C
It writes the result in reverse order
- D
It cannot handle a sorted input
Correct answer
BIt needs a separate result array, which can double the storage requirement
Explanation
The merge creates a second array to hold the result. For enormous inputs, doubling the required storage may prevent the program from working at all.
- A
- 22
Why was selection sort favored over insertion sort in the final algorithm choice described?
- A
Insertion sort always requires two arrays
- B
Selection sort was guaranteed to require no comparisons
- C
Efficiency was the priority, and selection sort appeared potentially more efficient despite being harder to implement
- D
The general sorting utility could not sort the input types
Correct answer
CEfficiency was the priority, and selection sort appeared potentially more efficient despite being harder to implement
Explanation
The decision was a tradeoff: insertion sort appeared easier to adapt, but selection sort might be more efficient. Since efficiency motivated avoiding a general sort, selection sort was chosen.
- A
- 23
Why would reading each input file into a separate array and then combining them be inefficient here?
- A
It would require storing the input data twice before or during combination
- B
It would cause all string comparisons to fail
- C
It would make the sorted input lose its file count
- D
It would prevent either file from being read line by line
Correct answer
AIt would require storing the input data twice before or during combination
Explanation
Using a separate array for each file and then combining them would duplicate the data in memory, conflicting with the goal of minimizing storage.
- A
- 24
What information does the method that reads both files need in addition to the two file readers?
- A
The number of values in each file
- B
The final sorted position of every value
- C
The number of duplicate values in the output
- D
The domain of every email address
Correct answer
AThe number of values in each file
Explanation
The method needs each file's size to allocate the combined array and to know where the sorted and unsorted sections of the input are.
- A
- 25
Why was it considered awkward for the read method to return only the combined array?
- A
The caller also needs the size of the first file to know the boundary between the two sections
- B
The array would not contain the values from the unsorted file
- C
A method cannot return an array of strings
- D
Returning an array closes both input files automatically
Correct answer
AThe caller also needs the size of the first file to know the boundary between the two sections
Explanation
The combined array alone does not identify where the sorted input ends and the unsorted input begins. The program also needs the first file's size, and finding that afterward could require extra work.
- A
- 26
How does reading file sizes in the main method help the overall design?
- A
It eliminates the need for a combined array
- B
It guarantees that the files are not corrupted
- C
It removes the need to read the file contents
- D
It makes the sizes available when other methods are called without needing an extra search
Correct answer
DIt makes the sizes available when other methods are called without needing an extra search
Explanation
The main method reads the counts first and passes them to the method that reads the data, avoiding an additional operation to determine the boundary later.
- A
- 27
How should the read method place the two files' values into one combined array?
- A
Read the unsorted file first and overwrite its values with the sorted file
- B
Read both files into the same array starting again at index zero
- C
Alternate one line from each file regardless of their sizes
- D
Read the sorted file first, then continue from the same index while reading the unsorted file
Correct answer
DRead the sorted file first, then continue from the same index while reading the unsorted file
Explanation
A shared index advances through the combined array: the first loop fills it with sorted-file values, and the second continues from that index with unsorted-file values.
- A
- 28
Why can the same index variable be declared before both file-reading loops?
- A
The second loop must continue at the position immediately after the first loop's data
- B
Java requires every loop in a method to share one index
- C
The two loops must always execute the same number of times
- D
The index records the number of duplicate values
Correct answer
AThe second loop must continue at the position immediately after the first loop's data
Explanation
Keeping one index allows the second loop to continue filling the array where the first loop stopped, rather than restarting at zero.
- A
- 29
What should the program do if a file has fewer lines than its declared count?
- A
Continue as if the declared count were always correct
- B
Silently fill the missing array slots with duplicate values
- C
Detect the mismatch and warn the user that the file may be corrupted
- D
Treat the missing lines as a valid empty file
Correct answer
CDetect the mismatch and warn the user that the file may be corrupted
Explanation
The file may be corrupted if its contents do not match its declared size. The program should detect the mismatch and notify the user because the result may otherwise be incorrect.
- A
- 30
Why should the input files be closed after the reading method finishes?
- A
Closing files removes duplicates from memory
- B
Closing files is an important part of managing file resources correctly
- C
The output file cannot be opened until all input files are closed
- D
Closing files sorts the values in the arrays
Correct answer
BClosing files is an important part of managing file resources correctly
Explanation
The program explicitly closes both input files after reading them. Closing files is an important resource-management practice.
- A
- 31
What kind of object should a method use to write the output file?
- A
A scanner
- B
A string comparison object
- C
A print writer
- D
A binary-search result
Correct answer
CA print writer
Explanation
A scanner is used for input, while a print writer is appropriate for writing the output file.
- A
- 32
In the adapted selection-sort loop, what should the minimum index initially be for each pass?
- A
The first position of the unsorted file
- B
The last position in the array
- C
The current position being filled
- D
The position after the current minimum
Correct answer
CThe current position being filled
Explanation
Selection sort begins each pass by treating the current position as the minimum. The search then checks the relevant remaining values for a smaller one.
- A
- 33
When the current position is still within the original sorted region, where does the adapted selection-sort search begin?
- A
At the start of the unsorted region
- B
At the final position of the array
- C
At the current position itself
- D
At the beginning of the array
Correct answer
AAt the start of the unsorted region
Explanation
While processing positions in the original sorted region, the search examines the unsorted region, which begins at the sorted input's size.
- A
- 34
When the current position is already in the unsorted region, where does the adapted selection-sort search begin?
- A
At the next position after the current one
- B
At the first position of the sorted region
- C
At the current position itself
- D
At the end of the sorted region
Correct answer
AAt the next position after the current one
Explanation
Once the algorithm is in the unsorted region, it searches the remaining positions beginning just after the current position.
- A
- 35
Why should the adapted selection-sort search not compare the current position with itself?
- A
Self-comparisons would change the array's length
- B
The current position is always the largest value
- C
A string cannot be compared with itself
- D
The current position is already the initial candidate for the minimum
Correct answer
DThe current position is already the initial candidate for the minimum
Explanation
The minimum index starts at the current position, so the search only needs to compare later candidates against it.
- A
- 36
How should the program determine whether one string is less than another in Java?
- A
Use equals and check whether the result is false
- B
Use compareTo and check whether the result is negative
- C
Use array length and compare the two lengths
- D
Use the less-than operator directly on the strings
Correct answer
BUse compareTo and check whether the result is negative
Explanation
Strings are compared with compareTo rather than the less-than operator. A negative result means the receiver string is less than the compared string.
- A
- 37
When should the selection-sort adaptation swap two values?
- A
Only when the two values are equal
- B
After every search pass, even if the minimum is already in place
- C
Whenever the current value is a string
- D
When the minimum index differs from the current position
Correct answer
DWhen the minimum index differs from the current position
Explanation
If the minimum is already at the current position, no swap is needed. Otherwise, the two values are exchanged.
- A
- 38
What is the purpose of a temporary variable during the selection-sort swap?
- A
To store the number of values in the unsorted file
- B
To remember whether two strings are duplicates
- C
To preserve one value while the two array positions are overwritten
- D
To convert the strings into sortable numbers
Correct answer
CTo preserve one value while the two array positions are overwritten
Explanation
A temporary variable holds one value during the exchange so it is not lost when the other position is assigned.
- A
- 39
Why were comments considered especially useful in the adapted selection-sort code?
- A
Comments prevent the algorithm from swapping values
- B
Its different search starting points can make correct logic look mistaken to a reader
- C
Comments are required for string comparisons to work
- D
The loops cannot run unless they contain comments
Correct answer
BIts different search starting points can make correct logic look mistaken to a reader
Explanation
The algorithm has more involved control flow than a basic sort, and its behavior can look incorrect without explanation. Comments make the reasoning easier to follow.
- A
- 40
Why can a sorted array be checked for duplicates by comparing each value with its previous value?
- A
Equal values are adjacent in a sorted array
- B
A sorted array contains no values other than duplicates
- C
Every pair of duplicate values has the same index
- D
Sorting moves all duplicate values to the beginning only
Correct answer
AEqual values are adjacent in a sorted array
Explanation
In sorted order, repeated values occur next to one another, so comparing neighboring values is sufficient to identify repeated entries.
- A
- 41
Why must the output-writing loop handle the first array value separately?
- A
The first value is always a duplicate
- B
The file format does not permit the first value to be written in a loop
- C
There is no previous array value to compare with at index zero
- D
The first value has not been sorted yet
Correct answer
CThere is no previous array value to compare with at index zero
Explanation
A comparison with the previous element would refer to index negative one for the first value. The program therefore writes the first value separately when the array is nonempty.
- A
- 42
What does the program need to check before writing the first array value separately?
- A
That the output file already contains one line
- B
That the first value is equal to the last value
- C
That the data array is not empty
- D
That the array contains an even number of elements
Correct answer
CThat the data array is not empty
Explanation
If the array has length zero, there is no first value to write. A nonempty check prevents an invalid access.
- A
- 43
Why can the duplicate-checking loop start at index one after handling the first value?
- A
Each later value can be compared with the preceding value without an index-zero special check
- B
Index zero is never part of the sorted array
- C
The first value is always removed from the output
- D
The array becomes shorter after every comparison
Correct answer
AEach later value can be compared with the preceding value without an index-zero special check
Explanation
Starting at index one ensures every value examined by the loop has a valid previous neighbor, simplifying the duplicate test.
- A
- 44
Which string operation should be used to test whether adjacent values are equal?
- A
The less-than operator
- B
The array length property
- C
The equals method
- D
The file's has-next-line method
Correct answer
CThe equals method
Explanation
String values should be compared for equality with equals. The program uses that comparison to identify adjacent duplicates.
- A
- 45
If the output file should contain each value only once, how should its declared item count be calculated?
- A
The number of lines in the unsorted input file
- B
The sorted input size alone
- C
The array length minus the number of duplicate occurrences omitted
- D
The array length plus the number of duplicate occurrences
Correct answer
CThe array length minus the number of duplicate occurrences omitted
Explanation
Because duplicate values are omitted from the output, the declared count must equal the array length minus the number of duplicate occurrences that will not be written.
- A
- 46
When should the duplicate counter be incremented in the output process?
- A
When the current sorted value equals the preceding value
- B
Only when the combined array has length zero
- C
Whenever the current value differs from the preceding value
- D
Once for every value read from the sorted file
Correct answer
AWhen the current sorted value equals the preceding value
Explanation
The duplicate counter should increase when adjacent sorted strings are equal, because that identifies a value that will not be written again.
- A
- 47
What did the incorrect output count reveal when the file declared thirteen values but contained twelve?
- A
The input files had been read in the wrong order
- B
The output file had included an extra blank line
- C
The sorted array contained too few values
- D
The count had not been adjusted to account for a duplicate omitted from the data lines
Correct answer
DThe count had not been adjusted to account for a duplicate omitted from the data lines
Explanation
The file's declared count must agree with the number of data lines. The mismatch showed that duplicate values had been omitted from the lines but not subtracted from the count.
- A
- 48
Why did changing the duplicate test from inequality to equality correct the duplicate count?
- A
The file format requires every unequal pair to be removed
- B
Unequal neighboring strings are always omitted from the output
- C
Equality checks determine the length of the array
- D
Equal neighboring strings are the duplicates that need to be counted
Correct answer
DEqual neighboring strings are the duplicates that need to be counted
Explanation
The counter must increase for repeated neighboring values, which are equal. Counting unequal values instead would count ordinary distinct entries.
- A
- 49
Why is it useful to test the file-reading method by writing its unsorted result to a file before implementing sorting?
- A
It removes the need to test boundary cases
- B
It verifies that the input files contain no duplicates
- C
It isolates whether the values were read and combined correctly
- D
It proves that the final sorting algorithm is correct
Correct answer
CIt isolates whether the values were read and combined correctly
Explanation
Saving the combined data before sorting provides a way to check that file reading and array construction work independently of the sorting logic.
- A
- 50
If a program runs without an error message but its output has not been inspected, what can be concluded?
- A
The program handled every duplicate correctly
- B
The program's output is definitely sorted
- C
The program did not visibly fail, but correctness has not yet been confirmed
- D
The input files were certainly uncorrupted
Correct answer
CThe program did not visibly fail, but correctness has not yet been confirmed
Explanation
A run that does not fail is encouraging but does not establish correctness. The output still needs to be inspected or checked against expected results.
- A
- 51
Why should a programmer understand sorting algorithms even when a library provides sorting methods?
- A
Library methods never work on real data
- B
Algorithm knowledge helps select and adapt efficient building blocks for a problem
- C
A programmer must rewrite every library method before using it
- D
Understanding algorithms removes the need to test programs
Correct answer
BAlgorithm knowledge helps select and adapt efficient building blocks for a problem
Explanation
Knowing how algorithms work helps programmers adapt them to a problem's constraints. Without that knowledge, they might rely on a general sort even when a more efficient approach is available.
- A
- 52
Why might an algorithm that is more complex to implement be a poor practical choice even if it is efficient?
- A
Complex algorithms are harder to write correctly and maintain
- B
Efficiency and implementation complexity are always identical
- C
Complex algorithms cannot process large arrays
- D
A complex algorithm necessarily uses no comparisons
Correct answer
AComplex algorithms are harder to write correctly and maintain
Explanation
More complicated algorithms are harder to implement correctly. Algorithm selection must balance efficiency with implementation difficulty.
- A
- 53
What does the example with a sorted array and a duplicate in both inputs illustrate about testing?
- A
Testing is useful only after every method has been completed
- B
Test data should avoid all repeated values
- C
A single successful run proves that boundary cases work
- D
Test data should include cases that expose known requirements, such as duplicate handling
Correct answer
DTest data should include cases that expose known requirements, such as duplicate handling
Explanation
Once duplicates were recognized as likely, a duplicate was added to the test data so the expected behavior could be checked directly.
- A
- 54
Why should the program warn users about corrupted input files rather than treating the resulting error as necessarily a sorting bug?
- A
The user is responsible for sorting the output manually
- B
Incorrect file contents can make a correct program produce an incorrect result
- C
Corrupted files always contain duplicate values
- D
Sorting algorithms automatically repair missing file lines
Correct answer
BIncorrect file contents can make a correct program produce an incorrect result
Explanation
If files are missing or contain extra data, the program may fail or produce incorrect results even when its sorting logic is correct. The user should be informed about the input problem.
- A

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