Out of order pairs
Inputs
values is a list of 0 to 12 whole numbers, each from 0 to 20.
Your tasks
- Create a number
countthat starts at 0. - Use nested loops over every pair of positions
i < j. Add 1 tocountwhenvalues[i]is larger thanvalues[j]. - Complete
countInversionsso it returnscount. Return it, do not print it.
Example
With values = [3, 1, 2], countInversions returns 2.
Rules
- Equal numbers are not out of order.
Test data
TEST DATA 1: values is [3, 1, 2]
TEST DATA 2: values is [1, 2, 3]
Practice focus
Nested loops compare every pair and build rows. The slow, simple answer to most problems is two loops. Knowing it is what lets you see how to beat it.