Algorithm exercises: brute-force#

Learning objectives

By the end of this lab, you will be able to:

  • Better understand the insertion sort algorithm

  • Implement in-place sorting algorithms using swap and shift techniques


Part 1: Brute-force exercises#

In Brute-force algorithms, you learned about brute-force algorithms that solve problems by examining all possible solutions. The two main algorithms introduced were linear search and insertion sort. In this section, you will implement in-place variants of insertion sort and solve other problems with the brute-force approach.

Reviewing insertion sort#

Do you remember the insertion sort algorithm from Brute-force algorithms? Here is the implementation presented in the textbook:

def insertion_sort(input_list):
    result = list()  # A new empty list where to store the result

    # iterate all the items on the input list
    for item in input_list:

        # initialise the position where to insert the item
        # at the end of the result list
        insert_position = len(result)

        # iterate, in reverse order, all the positions of all the
        # items already included in the result list
        for prev_position in reversed(range(insert_position)):

            # check if the current item is less than the one in
            # prev_position in the result list
            if item < result[prev_position]:
                # if it is so, then the position where to insert the
                # current item becomes prev_position
                insert_position = prev_position

        # the item is inserted into the position found
        result.insert(insert_position, item)

    return result  # the ordered list is returned

print(insertion_sort([5, 3, 8, 1]))  # [1, 3, 5, 8]
[1, 3, 5, 8]

This implementation is intuitive and was useful for introducing several Python features: the insert() method, the range() function, and the reversed() function. However, it does more work than it needs to:

  1. Extra memory: It creates a new list (result) to store the sorted elements, using additional memory proportional to the input size.

  2. Unnecessary comparisons: The inner loop scans every position of result, even after it has found where the item belongs. On a list that is already sorted, each item is still compared with all the items before it.

  3. Hidden cost of insert(): The list.insert() method looks simple, but internally it must shift all elements after the insertion point to make room for the new element. For example, if we insert an element at position 0 of a list with 100 elements, Python must move all 100 elements one position to the right. This happens “behind the scenes” but still takes time.

In the following exercises, you will write versions that sort the list in place, without creating a new list, and stop scanning as soon as the item reaches its position. You will also move the elements yourself instead of calling insert(). The shifting no longer happens behind the scenes, which lets you see how much it costs and find a way to keep that cost low.


Exercise 1.1: In-place insertion sort with swap#

Implement a function insertion_sort_swap(items) that sorts a list in place using element swapping.

Requirements:

  • Do not use the list.insert() method

  • Do not create a second list (modify the input list directly)

  • Use element swapping to move elements to their correct positions

numbers = [5, 3, 8, 1]
insertion_sort_swap(numbers)
print(numbers)  # Expected: [1, 3, 5, 8]

words = ["banana", "apple", "cherry"]
insertion_sort_swap(words)
print(words)  # Expected: ["apple", "banana", "cherry"]

Exercise 1.2: In-place insertion sort with shift#

The swap-based approach works, but each swap requires 3 operations. When moving an element several positions, we can do better.

Consider moving the element 1 from position 3 to position 0 in [3, 5, 8, 1]:

  • With swap: 3 swaps, each requiring 3 operations = 9 operations total

  • With shift: Save 1 once (1 operation), shift 3 elements right (3 operations), place 1 (1 operation) = 5 operations total

Implement a function insertion_sort_shift(items) that uses shifting instead of swapping.

The idea: Instead of swapping adjacent elements repeatedly, save the current element in a variable, shift all larger elements one position to the right, then place the saved element in the correct position.


Exercise 1.3: Sort by string length#

Modify the insertion sort algorithm to sort strings by their length (shortest first) instead of alphabetically.

Implement sort_by_length(strings).

print(sort_by_length(["cat", "elephant", "dog", "a"]))  # Expected: ["a", "cat", "dog", "elephant"]
print(sort_by_length(["ab", "abc", "a"]))              # Expected: ["a", "ab", "abc"]
print(sort_by_length(["same", "size", "word"]))        # Expected: ["same", "size", "word"] (same length, original order)

Exercise 1.4: Find second largest#

Implement a function find_second_largest(numbers) that returns the second largest value in a list with at least two distinct elements.

print(find_second_largest([3, 1, 4, 1, 5, 9, 2, 6]))  # Expected: 6
print(find_second_largest([10, 20]))                   # Expected: 10
print(find_second_largest([5, 5, 5, 3, 3]))           # Expected: 3

Next lab

The next lab, Sets, dictionaries, and data organization, explains when to choose a list, a set or a dictionary.