1. Home
  2. Explore
  3. Computer Science
  4. Sorting Partially Sorted Data

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Sorting Partially Sorted Data

  • 73Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Problem Modeling
To generalize the email-list task, treat the entries as
rather than building a program that depends specifically on email addresses.

73 questions

  1. 1
    To 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.

  2. 2
    In 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.

  3. 3
    LittleTexts'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.

  4. 4
    The 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.

  5. 5
    Because 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.

  6. 6
    A 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.

  7. 7
    A 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.

  8. 8
    Testing 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.

  9. 9
    If 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.

  10. 10
    When 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.

  11. 11
    One 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.

  12. 12
    In 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.

  13. 13
    The 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.

  14. 14
    Although 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.

  15. 15
    An 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.

  16. 16
    Removing 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.

  17. 17
    Putting 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.

  18. 18
    Sorting 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.

  19. 19
    The 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.

  20. 20
    In 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.

  21. 21
    When 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.

  22. 22
    After 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.

  23. 23
    In 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.

  24. 24
    When 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.

  25. 25
    When 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.

  26. 26
    After 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.

  27. 27
    Although 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.

  28. 28
    In 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.

  29. 29
    In 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.

  30. 30
    Insertion 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.

  31. 31
    When 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.

  32. 32
    A 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.

  33. 33
    Both 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.

  34. 34
    A 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.

  35. 35
    During 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.

  36. 36
    Once 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.

  37. 37
    The 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.

  38. 38
    For 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.

  39. 39
    Selection 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.

  40. 40
    Insertion 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.

  41. 41
    Calling 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.

  42. 42
    To 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.

  43. 43
    A 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.

  44. 44
    The 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.

  45. 45
    The 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.

  46. 46
    The 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.

  47. 47
    In 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.

  48. 48
    The 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.

  49. 49
    Including 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.

  50. 50
    When 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.

  51. 51
    The 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.

  52. 52
    The 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.

  53. 53
    Declaring 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.

  54. 54
    The 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.

  55. 55
    A 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.

  56. 56
    After 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.

  57. 57
    After 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.

  58. 58
    If 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.

  59. 59
    A 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.

  60. 60
    The 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.

  61. 61
    After 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.

  62. 62
    To 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.

  63. 63
    A duplicate check for the current output value compares data at index iii with data at index
    _____
    .
    Hint

    In sorted data, a repeated value will be next to the previous value.

    Answer

    i−1i - 1i−1

    Explanation

    Because the array is sorted, duplicates are adjacent, so each value can be checked against the preceding element.

  64. 64
    The 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-1−1

    Explanation

    When the current index is zero, there is no preceding element, so the first value needs special handling.

  65. 65
    The 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

    000

    Explanation

    Checking that the array is nonempty prevents the program from trying to print an element that does not exist.

  66. 66
    The 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-1−1 when the current index is zero.

  67. 67
    Because 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.

  68. 68
    To 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.

  69. 69
    If 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.

  70. 70
    A 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.

  71. 71
    Understanding 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.

  72. 72
    If 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.

  73. 73
    The 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.

1 of 73
Question 1 of 73

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent