Question 1

OCR Computer Science June 2017 Paper 2A level22 marks

On the specification

(a)(i)
2.3 Algorithms
(a)(ii)
2.3 Algorithms
(b)(i)
2.3 Algorithms
(b)(ii)
2.3 Algorithms
(c)
2.3 Algorithms
Question text

1 A programmer needs to sort an array of numeric data using an insertion sort. (a) (i) The following, incomplete, algorithm performs an insertion sort. Complete the algorithm. procedure sortit(dataArray, lastIndex) for x = 1 to lastIndex currentData = dataArray[ ] position = x while (position > 0 AND dataArray[position-1] > currentData) dataArray[position] = dataArray[ ] position = position - 1 endwhile dataArray[position] = next x endprocedure [3] (ii) Show how an insertion sort would sort the following data: 61 15 12 5 6 9 [6] (b) (i) Using Big-O notation state the best case complexity of insertion sort. [1] (ii) Explain what your answer to part (b)(i) means. [3] (c*) The number of data items in the array is continually increasing. 2 Insertion sort has a worst case time complexity of O(n ) and space complexity of O(1). An alternative sorting algorithm that could be used is bubble sort which also has a worst case 2 time complexity of O(n ) and space complexity of O(1). Briefly outline how the bubble sort algorithm works. Discuss the relationship between the complexities and the two sorting algorithms and justify which of the two algorithms is best suited to sorting the array. [9]