Count Inversions in Array
📖 Scenario: Imagine you have a list of numbers representing the order of books on a shelf. You want to find out how many pairs of books are out of order compared to their correct sorted order. This count is called the number of inversions.
🎯 Goal: Build a program that counts the number of inversions in an array of integers. An inversion is a pair of positions where the earlier number is greater than the later number.
📋 What You'll Learn
Create an array called
arr with the exact values: 2, 4, 1, 3, 5Create an integer variable called
n and set it to the size of arrWrite a function called
countInversions that takes the array arr and its size n and returns the number of inversionsPrint the number of inversions using
printf💡 Why This Matters
🌍 Real World
Counting inversions helps measure how far a list is from being sorted, useful in sorting algorithms and data analysis.
💼 Career
Understanding inversion counting is important for software engineers working on algorithms, data processing, and performance optimization.
Progress0 / 4 steps
