Tut 08 - Searching and Sorting

Problem Set 21

Problem 21.1

The Loop version of Binary Search

long search_loop(const long list[], long i, long j, long q) {
    while(i<=j) {
        long mid = (i+j)/2;
        if (list[mid] == q) {
            return mid;
        }
        if (list[mid] > q) {
            j = mid - 1;
            continue;
        }
        i = mid + 1;
    }
    return -1;
}

Problem 21.2*

Question: Instead of returning -1 if the query q is not found, modify the binary search algorithm in Problem 21.1 such that it returns either:

  • a position k, such that list[k] <= q <= list[k+1].

  • -1 if q < list[0]

  • n - 1 if q > list[n-1]

The reason to return mid is easy to understand. But return j at last needs some pattern recognition skill. 😂

Problem Set 22

Problem 22.1*

Optimize the Bubble Sort.

Basically, when there is not swap, which means our list is sorted already.

Problem 22.2*

Insertion sort by doing:

  • take the first element X from the unsorted partition

  • use binary search to find the correct position to insert X

  • insert X into the right place

In Line 20, curr-1 means we exclude our current number from the search range first. And the +1 at last is because we want to insert at the position behind what we find using Problem 21.2*

Last updated