Skip to main content

Practice · 4 of 5

Count insertion shifts

CHALLENGE
Difficulty: beginner+25 XP

Implement int insertionShifts(int[] arr): the total number of shifts insertion sort would perform on arr — for each element, the number of earlier elements greater than it. Pure trace arithmetic, no sorting needed: for each i, count j < i with arr[j] > arr[i].

Back to lesson: Practice: Search & sort reps