2.3.1(f) · Data structures and algorithms

Bubble and insertion sort

The lesson for this topic

Question 11 mark

What is the list 7, 3, 9, 2, 5 after the first pass of a bubble sort (ascending)?

Question 21 mark

What is the list 4, 1, 6, 3, 8, 2 after the first pass of a bubble sort (ascending)?

Question 31 mark

What is the list 9, 8, 7, 6 after the first pass of a bubble sort (ascending)?

Question 41 mark

Insertion sort runs on 5, 2, 8, 1, 9. What is the list once the items at indexes 1 and 2 have both been inserted?

Question 51 mark

Insertion sort runs on 3, 6, 1, 4. What is the list once the items at indexes 1 and 2 have both been inserted?

Question 61 mark

A bubble sort with no early stop runs n − 1 passes of n − 1 comparisons. How many comparisons for 6 items?

Question 71 mark

What is the worst-case time complexity of bubble sort?

Question 81 mark

Why is insertion sort fast on data that is already nearly sorted?

Question 91 mark

Bubble sort and insertion sort both sort in place. What does that mean?

Structured question 19 marks

Bubble sort

The procedure sorts an array into ascending order. It is called on 5, 3, 8, 4, 2.

procedure bubble(byRef a, n)    swapped = True    while swapped        swapped = False        for j = 0 to n - 2            if a[j] > a[j + 1] then                temp = a[j]                a[j] = a[j + 1]                a[j + 1] = temp                swapped = True            endif        next j    endwhileendprocedure
(a) Complete4 marks

the table: the array and the number of swaps after each pass of the while loop.

passa[0]a[1]a[2]a[3]a[4]swaps
1354283
(b) Explain2 marks

the purpose of the variable swapped.

0 words
(c) Explain2 marks

why temp is needed.

0 words
(d) State1 mark

how many comparisons pass 1 makes.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms