ProgrammingVery hard
How many inversions does the array {2, 4, 1, 3, 5} contain?
- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: A. 3
The inverted pairs are (2,1), (4,1) and (4,3). Merge sort counts exactly these during merges.
More C Programming questions
- Records (A, 3, x), (B, 3, a), (C, 1, m) are sorted by score descending and then by the name field ascending. What is the order of the…
- A struct P ps[] holds records, and an array of pointers pp[] is sorted with qsort. What happens to the records in ps?
- Which argument is correct as the size parameter in qsort for an array struct P recs[] of n records?
- A comparator for qsort is written as int cmp(const void a, const void b) { return ((const struct P )a)->k > ((const struct P )b)->k; }…
- For a struct array sorted by an int key, which bsearch call is correct to find a record with key k?
- A bsearch on a struct array returns NULL for a key that is actually present. Which is the most likely cause?