Docsity

Merge Sort and Quick Sort Worksheet: Algorithms Explained, Exercises of Computer science

Toggle sidebar
bg1
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
Ava
Olivia
Sophie
Poppy
Isla
Lily
Amelia
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.
Ava
Jessica
Olivia
Sophie
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
Finally merge the two remaining lists in the same way.
Fill in the boxes below
1
pf3
pf4
pf5

Partial preview of the text

Download Merge Sort and Quick Sort Worksheet: Algorithms Explained and more Exercises Computer science in PDF only on Docsity!

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 Ava Olivia Sophie^ Poppy Isla Lily Amelia  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. Ava Jessica^ Olivia^ Sophie  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  Finally merge the two remaining lists in the same way.  Fill in the boxes below

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 len (lefthalf ) len (righthalf) i j k lefthalf[i ] righthalf[j] alist[k] 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) i = 0 i = 0 j = 0 k = 0

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

P L R

P L R

Unit 12 Algorithms