0% found this document useful (0 votes)
40 views

T3 Homework 3

Uploaded by

arushi.agupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

T3 Homework 3

Uploaded by

arushi.agupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

(b) How many passes will be made through the list used in part (a) using the
Bubble sort algorithm in order to sort it?

2. The diagram below shows an algorithm in graphical form.

(a) What is the algorithm depicted?

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

[74,36,81,25] [19,14,61,40]

[74,36] [81,25] [19,14] [61,40]

74 36 81 25 19 14 61 40

36 74 25 81 14 19 40 61

25,36,74,81 14,19,40,61

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.
First it skips the first number and then selects the second number. It creates a new list and
inserts the second number comparing it to the first number and adding it either in front or
before the number. Then it moves onto the next number and has a O(n^2).

You select the first value in the list and the algorithm works through each item in a list till ot
reaches the end. It makes two lists- an unsorted list and a sorted list. It places the chosen
item in the correct place in the list.

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: 45, 63, 73, 23, 81, 18

Stage 4: 23, 45, 63, 73, 81, 18

Stage 5: 18, 23, 45, 63, 73, 81

(c) Give one reason why an Insertion sort may be chosen over a bubble sort for
sorting a large number of items.
Bubble sort goes through each pair over and over again

[Total 20 Marks]

You might also like