Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Homework 3 Sorting algorithms Unit 6 Algorithms Name: ........................................................................... Class:.................. Mark:................ 1. (a) What would be the state of the following list after each of the first four passes in a Bubble sort, sorting into ascending sequence? 65, 34, 28, 68, 52, 21 [4] 1: 34, 28, 64, 52, 21, 68 2: 28, 34, 52, 21, 65, 68 3: 28, 34, 21, 52, 65, 68 4: 28, 21, 34, 52, 65, 68 (b) How many passes will be made through the list used in part (a) using the Bubble sort algorithm in order to sort it? [1] 5 2. The diagram below shows an algorithm in graphical form. (a) What is the algorithm depicted? [1] Merge sort 1 Homework 3 Sorting algorithms Unit 6 Algorithms (b) Describe the process of sorting the following list using the above algorithm: 74, 36, 81, 25, 19, 14, 61, 40 [6] The list (74, 36, 81, 25, 19, 14, 61, 40) gets split into smaller parts until each has only one number. These small parts then pair up and merge, arranging themselves in ascending order: (36,74) (25,81) (14,19) (40,61). These pairs combine again, sorting in ascending order. (25, 36, 74, 81) (14, 19, 40, 61). Finally, the two groups merge together, giving the complete sorted list: (14, 19, 25, 36, 40, 61, 74, 81) . 3. An insertion sort algorithm is used to sort the following numbers: 63, 45, 73, 23, 81, 18 (a) Explain briefly how an insertion sort works. [3] Insertion sort works by taking each number and finding its spot in the growing sorted list. It keeps doing this for every number until the whole list is sorted. 2 Homework 3 Sorting algorithms Unit 6 Algorithms (b) Show the stages of the sort. The first stage is shown below: Stage 1: 45, 63, 73, 23, 81, 18 Stage 2: 45, 63, 73, 23, 81, 18 Stage 3: 23, 45, 63, 73, 81, 18 Stage 4: 23, 45, 63, 73, 81, 18 Stage 5: 18, 23, 45, 63, 73, 81 [4] (c) Give one reason why an Insertion sort may be chosen over a bubble sort for sorting a large number of items. [1] Insertion sort uses fewer comparsions than Bubble sort for sorting big lists, so is more efficient. [Total 20 Marks] 3