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

Algorithms Worksheet 4 Merge Sort and Quickso

Uploaded by

17adarao
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)
23 views

Algorithms Worksheet 4 Merge Sort and Quickso

Uploaded by

17adarao
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/ 5

Worksheet 4 Merge sort and quick sort

Unit 12 Algorithms

Worksheet 4 Merge sort and quick sort


Task 1

1. Use 8 name cards for the merge activity in this activity.


 Lay the cards out in a row, face up, in the following unsorted order:
Jessica

Sophie

Amelia
Poppy
Olivia
Ava

Lily
Isla
 Read a card from each pair and write the lower value name to the merged list of 2 items
 Then write the other value to the merged list
 Do this for each pair
 Fill in the names below. The first two pairs have been done for you.
Jessica

Sophie

Amelia
Poppy
Olivia
Ava

Lily
Isla

 You are now merging Ava, Jessica, Olivia, Sophie.


 Pick up the first card from each list, Ava and Olivia.
 Compare, and move Ava to the new merged list
 Pick up another card (Jessica) from the same sublist as Ava
 Compare Jessica with Olivia
 Move Jessica to new merged list
 Olivia and Sophie are in the correct sequence so write them to the merged list.
 Merge the second sublist in the same way. Fill in the boxes below
Jessica

Sophie

Amelia

Poppy
Olivia
Ava

Lily
Isla

 Finally merge the two remaining lists in the same way.


 Fill in the boxes below

1
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms

Am Jes So
Oli Po
Ava Isla sic Lily phi
elia va ppy
a e

Task 2
An algorithm for the Merge sort is given on the next page.

In this task you will focus on the merge phase of the process (enclosed by a border in the
pseudocode below) in which the two sorted sublists are merged into one sorted list.

Complete the trace table below to show how two lists called lefthalf and righthalf are merged
into the sorted list alist.
lefthalf = [1, 3, 9] righthalf = [4, 6, 8]

1 3 9 4 6 8

i=0 j=0

k=0

len
len lefthalf[i
(lefthalf i j k righthalf[j] alist[k]
(righthalf) ]
)
3 3 0 0 0 1 4 [1]

procedure mergeSort(alist)
if len(alist) > 1
mid = len(alist) div 2 # performs integer division
lefthalf = alist[:mid] # left half of alist put into
lefthalf
righthalf = alist[mid:] # right half of alist put into
righthalf
mergeSort(lefthalf)
mergeSort(righthalf)

2
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms
i = 0
j = 0
k = 0
while i < len(lefthalf) and j < len(righthalf)
if lefthalf[i] < righthalf[j]
alist[k] = lefthalf[i]
i = i + 1
else
alist[k] = righthalf[j]
j = j + 1
endif
k = k + 1
endwhile
#check if the left half still has elements not merged
#if so, add them to alist
while i < len(lefthalf)
alist[k] = lefthalf[i]
i = i + 1
k = k + 1
endwhile
#check if the right half still has elements not merged
#if so, add them to alist
while j < len(righthalf)
alist[k] = righthalf[j]
j = j + 1
k = k + 1
endwhile
print("Merged sublist ",alist)
endif
endprocedure

#*********** MAIN PROGRAM ***************


alist = [3, 1, 9, 8, 4, 6]
print("Unsorted list: ",alist)
mergeSort(alist)
print("Sorted list: ",alist)

3
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms

Task 3 Quick sort


This task uses the quick sort algorithm. Show the state of the list and pointers as they
change while finding the place where the pivot should go in the sorted list. Only the first
iteration should be shown, on the whole list.

The left pointer moves right until it finds an item larger than the pivot, then stops.

The right pointer moves left until it finds an item smaller than the pointer, and then
stops.

The value at the left pointer is swapped with the value at the right pointer.

The pointers resume moving, swapping items until the left pointer crosses the right
pointer.

The pivot is then swapped with with the value at the right pointer and is now in the
correct position.

The first two steps are given.

P = pivot, L =left pointer, R = right pointer

34 56 23 81 28 66 35 17 88 37 18 50
P L R

34 56 23 81 28 66 35 17 88 37 18 50
P L R

34 50

34 50

34

4
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms

Task 4

Look up a web site such as

http://www.sorting-algorithms.com/ to compare different sorts for 50 items. Which sort is


fastest? Which is slowest?

You can also look at

https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

or

https://www.youtube.com/watch?v=ZZuD6iUe3Pc

to watch a visualisation of the different sorting algorithms.

You might also like