← Quiz: Searching and Sorting in C: all 10 questions
Quiz: Searching and Sorting in C · Question 3 of 10
What does this print? (code)
What does this print?
#include <stdio.h>
int main(void) {
int a[] = {3, 1, 2};
int n = 3, swaps = 0;
for (int p = 0; p < n - 1; p++)
for (int j = 0; j < n - 1 - p; j++)
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
swaps++;
}
printf("%d %d%d%d", swaps, a[0], a[1], a[2]);
return 0;
}
- A.3 123
- B.2 123
- C.1 123
- D.2 312
Answer & explanation
Correct answer: 2 123
Pass 1: swap 3,1 → 1 3 2, swap 3,2 → 1 2 3. Pass 2 needs no swap. Total 2.
Can you get the rest right without peeking?
Take the timed Quiz: Searching and Sorting in C quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →