ProgrammingModerate
An array of 10^6 records of 100 bytes each must be sorted. Which approach is correct and fast enough?
- A. Bubble sort on the records themselves, about 10^12 comparisons
- B. qsort on an array of pointers to the records, about 2 x 10^7 comparisons
- C. Insertion sort on the records, about 10^12 comparisons
- D. Selection sort on the records, about 10^12 comparisons
Correct answer: B. qsort on an array of pointers to the records, about 2 x 10^7 comparisons
Sorting pointers keeps swaps cheap, and n log n comparisons for n = 10^6 is about 2 x 10^7. The quadratic sorts make about 10^12 comparisons.
More C Programming questions
- struct S { int id; int v; }; struct S a[4] = {{1,9},{2,8},{3,7},{4,6}}; struct S b[4]; memcpy(b, a, sizeof a); b[0].v = 0; printf("%d %d"…
- struct Grid { int cell[2][2]; }; struct Grid g1 = {{{1, 2}, {3, 4}}}; struct Grid g2 = g1; g2.cell[1][1] = 0; printf("%d", g1.cell[1][1])…
- Which is the correct way to count distinct keys in a sorted struct array a[0..n-1] (n >= 1) by key?
- A sorted array of records is searched for the first record whose key is at least x (a lower bound). Which loop is correct?
- An array of 2 x 10^5 records must be answered with 2 x 10^5 queries, each asking how many records have key <= q. Which approach is correct…
- After removing duplicate keys from a sorted array of records with the two-index method (keep a[w] when its key differs from the last kept…