Sorting Partially Sorted Data
- 65Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What general problem is illustrated by merging the two customer lists?
Combine one already-sorted data set and one unsorted data set into a single result sorted by the required key.
Or swipe the card
65 cards
What general problem is illustrated by merging the two customer lists?
Combine one already-sorted data set and one unsorted data set into a single result sorted by the required key.
Why should a solution avoid depending on the data being email addresses when sorting by email address?
If the task only requires ordering by a general key, the program can be abstracted to handle other kinds of data rather than being tied to email addresses.
When would the fact that the records are email addresses matter to the algorithm?
It would matter if the required ordering depended on email-specific information, such as the domain name.
Why are duplicate values likely in the two customer lists?
The same person may appear in both lists, for example after purchasing from both companies.
How can duplicate handling differ between the merged array and the output file?
The merged array can retain both occurrences, while the output file can contain only one occurrence of each value.
What file format is used for each input list?
The first line contains the number of data items; each following line contains one item.
Why does putting the item count first in a file make array construction easier?
The program can allocate an array of the exact size immediately, without first scanning the file to count its data.
How should a correctly formatted file represent a list with no data?
It should contain a count of zero, rather than being an empty file.
What boundary cases should be tested when merging sorted and unsorted data?
Either input can be empty, either can contain only one item, or both can contain only one item.
What is a simple way to remove duplicates from a sorted result while writing it?
Write an item only if it differs from the preceding item; duplicates are adjacent in sorted data.
How could binary search be used to prevent adding duplicates from the unsorted input?
For each item in the unsorted input, search the already-sorted input and skip the item if it is found.
Why might binary-searching the large sorted input for every unsorted item still be a poor choice?
Although binary search is cheaper than linear search, repeated searches still take time, especially when the sorted input is large.
What Java utility provides binary search for arrays?
Arrays.binarySearchprovides binary search for arrays.What does the Java
Arraysclass provide?It provides utility methods for arrays, analogous to the utilities that the
Collectionsclass provides for collections.Why is removing duplicates during output writing often a convenient strategy?
It is straightforward to do while traversing the sorted data and avoids a separate duplicate search for every input item.
What is a limitation of deciding to remove duplicates only while writing the output?
If there are very many duplicates, the program may do unnecessary sorting work before eliminating them.
Why is putting all items into one array and calling
Arrays.sortnot necessarily the best algorithm?It ignores the fact that one input is already sorted and may be inefficient for enormous data sets.
When might sorting the combined array with
Arrays.sortstill be a reasonable choice?When minimizing the time required to write the program is the main constraint.
What trade-off informed the choice between adapted selection sort and insertion sort?
Selection sort might be more efficient but harder to implement; insertion sort is easier to adapt because it requires only a small change to an existing loop.
Why did efficiency favor adapted selection sort in this particular problem?
Efficiency was the reason not to use a simple full-array sort, so the potentially more efficient selection-sort adaptation was favored over the easier insertion-sort adaptation.
What is the basic selection-sort strategy?
Repeatedly find the smallest value among the remaining items and place it in the next position of the result order.
How does adapted selection sort choose the next minimum while processing the initially sorted portion?
It compares the current sorted item with the items in the unsorted input and selects the smallest.
How does adapted selection sort proceed after it reaches the unsorted portion of the combined array?
It continues like ordinary selection sort, searching the remaining items for the next minimum.
What happens to the sorted and unsorted values when adapted selection sort swaps items?
Values from the two portions can become mixed in position, but the displaced values remain available for later comparisons.
Why does mixing positions during adapted selection sort not necessarily discard the advantage of the sorted input?
A value moved into the unsorted region can occupy a position beyond the current sorting frontier, so the existing ordering still helps determine the next minimum.
What makes adapted selection sort more complicated to implement than ordinary selection sort?
The search has two phases: while the current position is in the sorted portion, search the unsorted portion; afterward, search the remaining unsorted positions.
What is the basic insertion-sort strategy?
Start with a sorted group and insert each unsorted item into its correct position within that group.
How does insertion sort handle an unsorted item that is already larger than the sorted items before it?
The item is already in the correct position and does not need to move.
How does insertion sort place an item that is smaller than items in the sorted portion?
It moves the item into its correct position, shifting or swapping larger items out of the way.
What is the main implementation advantage of adapting insertion sort for this task?
It can be treated as starting insertion sort partway through the data, requiring only a minor change to the loop's starting condition.
What is a potential cost of adapting insertion sort to merge a large unsorted portion?
It can require many shifts or swaps, which are costly when the data sets are enormous.
What is the two-finger merge strategy?
Sort the smaller input, then compare the next available values from the two sorted inputs and repeatedly append the smaller one to a result array.
Why does a two-finger merge need not compare later items from one input after choosing its smallest remaining item?
Because that input is sorted, its current item is no larger than its later items.
What is the main disadvantage of the two-finger merge approach in this problem?
It needs a second array to hold the merged result, potentially doubling storage for enormous data sets.
Why can doubling storage be a deal breaker for a merge algorithm?
The additional memory requirement may be so large that the program cannot run.
Why is reading each input file into a separate array and then combining them inefficient here?
It requires storing both input arrays and the combined data, increasing the storage needed.
Why is a single method that reads both files and returns only the combined array insufficient by itself?
The sorting method also needs to know where the originally sorted portion ends, but the returned array alone does not provide that boundary.
What design was used to retain the sorted-input boundary without rereading the files?
Read the file sizes in the main method, then pass both sizes and both file scanners to a method that reads the data into one array.
What does the combined-file-reading method return?
A single string array containing the data from both files.
Why might an efficient method design be described as inelegant?
It may require passing extra information, such as input sizes, even though that design avoids costly rereading or duplicate storage.
What are the principal responsibilities of the output-writing method?
Write the output file's item count and data, while omitting duplicate values.
Why should input files be closed after they have been read?
Closing files releases the resources associated with them and is an important part of reliable file handling.
What is the purpose of the shared index when reading two files into one array?
It tracks the next array position across both loops, so the second file's data follows the first file's data.
Why should the array index be declared before the first of two input loops?
The same index must remain available to continue filling the array in the second loop.
What array bounds should be used when reading the sorted file into the combined array?
Start at index zero and continue while the index is less than the sorted-file size.
What array bounds should be used when reading the unsorted file after the sorted data?
Continue from the existing index until it reaches the sum of the sorted and unsorted sizes.
How can a program detect that an input file has fewer lines than its declared count?
Check whether the scanner has another line before reading each expected item, then report an error if the number read does not match the declared size.
How can a program detect that an input file has more lines than its declared count?
After reading the declared number of lines, check whether the scanner still has a next line and report a mismatch.
Why should a program warn the user when file contents do not match the declared counts?
Corrupted or inconsistent files can make the program produce incorrect results even if its algorithm is correct.
How should Java compare two
Stringvalues for ordering in the selection-sort search?Use
compareTo; a negative result means the receiver string is lexicographically smaller than the argument.How should Java test whether two strings have equal contents when removing duplicates?
Use
equals, not==.What is the purpose of the selection-sort minimum index?
It records the position of the smallest item found during the current search.
Where should the selection-sort search begin when the current index is still in the sorted portion?
At the start of the unsorted portion, because the sorted portion's current item is already the candidate minimum.
Where should the selection-sort search begin when the current index is already in the unsorted portion?
At the next index, because the current index is already the initial minimum candidate.
Why does selection sort swap only when the minimum index differs from the current index?
If they are equal, the minimum is already in the correct position and no swap is needed.
What is the standard temporary-variable method for swapping two array elements?
Save one element in a temporary variable, copy the other element into its position, then copy the saved value into the other position.
Why is a
PrintWriterappropriate for saving data to a text file, rather than aScanner?A
PrintWriterwrites output; aScanneris used to read input.How can a program count duplicates in an already-sorted array?
Compare each item after the first with its predecessor and increment a duplicate counter when their contents are equal.
How should the output file's item count account for duplicates?
Write the array length minus the number of duplicate occurrences.
Why must the first array element be handled separately when writing unique items?
It has no preceding element to compare against, so accessing the previous position would be out of bounds.
How can a loop avoid comparing an array element with an invalid negative index?
Start the duplicate-checking loop at index one, or ensure an index-greater-than-zero condition is checked before the comparison.
How does short-circuit evaluation of
&&protect an array access?If the first condition is false, the second condition is not evaluated; thus an index check can prevent an unsafe array access.
Why should the first item be written only when the array is nonempty?
An empty array has no element at index zero, so attempting to write it would cause an invalid array access.
In the example test, what should happen to a value that appears in both input arrays?
Both occurrences remain in the merged array, but only one is written to the output file; the output count excludes the duplicate.
What is the value of knowing the details of sorting algorithms even when library sorting methods are available?
Algorithm knowledge provides reusable building blocks that can be adapted to problems where a general library sort is not the most efficient solution.

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