Algorithms Worksheet 4 Merge Sort and Quicksort
Algorithms Worksheet 4 Merge Sort and Quicksort
Unit 12 Algorithms
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
isla
lily
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
isla
lily
1
jessica
sophie
amelia
poppy
olivia
ava
isla
lily
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms
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
1 3 4 6 8 9
k=0
len
len lefthalf[i
(lefthalf i j k righthalf[j] alist[k]
(righthalf) ]
)
3 3 0 0 0 1 4 [1]
1 1 3 6 13
2 2 9 8 134
1 3 1346
2 4 13468
3 5 134689
procedure mergeSort(alist)
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
2
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms
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
3
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms
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.
34 56 23 81 28 66 35 17 88 37 18 50
P L R
34 56 23 81 28 66 35 17l 88 37 18 50
P L R
34 18 23 81 28 66 35 17 88 37 56 50
34 18 23 17 28 66 35 81 88 37 56 50
34
17 18 23 28 34 35 37 50 56 66 81 88
4
Worksheet 4 Merge sort and quick sort
Unit 12 Algorithms
Task 4
https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
or
https://www.youtube.com/watch?v=ZZuD6iUe3Pc