Skip to main content

Practice ยท 2 of 2

Prove the sort with arithmetic

1. static long inversionCount(int[] a) โ€” count out-of-order pairs (i < j, a[i] > a[j]) with the honest O(n^2) double loop, as a long. 2. static void bubbleSort(int[] a) โ€” classic bubble sort, no early exit. 3. static long swapsFor(int[] a) โ€” return the number of swaps bubbleSort performs on a defensive copy of a WITHOUT re-counting inversions (count inside the sort or wrap it โ€” your choice) โ€” it must equal inversionCount(a) for every input. 4. static boolean isSorted(int[] a) โ€” linear scan, true iff non-decreasing.

Difficulty: advanced

Back to lesson: Practice: Measurement drills