Practice

Bubble Sort and Binary Search

2024/May/June·Variant 1·Q1(d/e)
HARDAlgorithms

Adapted from Cambridge International AS & A Level Computer Science (9618), May/June 2024 Paper 41, Question 1.

The global array DataStored contains up to 20 integers. Write:

  • procedure BubbleSort() to sort the used part of DataStored into ascending order
  • iterative function BinarySearch(DataToFind) to return the zero-based index where the value is found, or -1 if it is not found

The test harness reads NumberItems, then that many values, then the search value. It calls your sort and search routines, outputs the sorted values, then outputs the search result.

Input: Count, values, search value. Output: Sorted values, then the index returned by BinarySearch().

Example:

Input:  5
        4
        2
        9
        1
        7
        7
Output: 1
        2
        4
        7
        9
        3
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Target found after sorting
Inputs: 5, 4, 2, 9, 1, 7, 7
Expected: 1 2 4 7 9 3
Test 2: Target not found
Inputs: 4, 8, 3, 8, 1, 6
Expected: 1 3 8 8 -1