Count writes across two ordered blocks

A data logger stores 100 distinct integer readings in this order: 61 through 100 in ascending order, followed by 1 through 60 in ascending order. It sorts them using the following insertion-sort implementation.


Python
for i in range(1, len(a)):
    key = a[i]
    j = i - 1
    while j >= 0 and a[j] > key:
        a[j + 1] = a[j]
        j = j - 1
    a[j + 1] = key


An array write is an execution of an assignment to an entry of a. Assignments to key, i, and j do not count. Count the final key assignment on every pass, even when it writes the existing value back into the same location.


The initial array contains the ascending values 61 through 100 followed by the ascending values 1 through 60.


Find the total number of array writes for this initial arrangement. Then find how many fewer array writes it needs than sorting the same readings initially arranged in descending order, from 100 down to 1. Give both answers as exact integer counts of array writes.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Insertion Sort Questions: