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

Algorithms Homework 4 Merge Sort and Quicksort

The document discusses merge sort and quicksort algorithms. It provides an explanation and pseudocode of how merge sort works by splitting lists in half and merging them back together in sorted order. It also gives an incomplete quicksort pseudocode and asks to complete it by filling in missing lines and identifying the initial pivot value and base case.

Uploaded by

finlay
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)
110 views

Algorithms Homework 4 Merge Sort and Quicksort

The document discusses merge sort and quicksort algorithms. It provides an explanation and pseudocode of how merge sort works by splitting lists in half and merging them back together in sorted order. It also gives an incomplete quicksort pseudocode and asks to complete it by filling in missing lines and identifying the initial pivot value and base case.

Uploaded by

finlay
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/ 4

Homework 4 Merge sort and quick sort

Unit 12 Algorithms

Homework 4 Merge sort and quick sort


1. (a) The diagram below shows an algorithm in graphical form. What is the algorithm
depicted? [1]

Merge Sort
(b) Describe briefly how the algorithm works. [5]

-Split the list down in half repeatedly, until the lists are single pieces of data
-Reconstruct the lists by comparing each term in the list one by one to reconstruct in the correct
order
-Keep rejoining the lists in correct order until all of the lists are merged

(c) Which of the following is the order of time complexity of the algorithm?
O(n log n) [1]

2. (a) Explain the function and significance of the “split point” in the quicksort algorithm. [2]
This is the number which splits the lists, into upper and lower lists

(b) An incomplete algorithm for the quicksort is given on the next page.
(i) Complete the algorithm by inserting the missing lines at A, B, C, D and E. [9]
(ii) What is the initial value of the pivot in the pseudocode below? 9 [1]

1
Homework 4 Merge sort and quick sort
Unit 12 Algorithms
(iii) What is the base case that stops the recursion from continuing indefinitely? [1]
Total 20 marks If start is less than end, this means that if there is only one item in
the list then it will not run, or if the list is two items then and slrreayd sorted it will
not run.

2
Homework 4 Merge sort and quick sort
Unit 12 Algorithms

function partition(alist, start, end)


pivot = alist[start]
leftmark = start + 1
rightmark = end
done = False
while done = False
while leftmark <= rightmark and alist[leftmark] <= pivot

leftmark += 1
endwhile
while alist[rightmark] >= pivot and rightmark >= leftmark

rightmark -= 1
endwhile
if rightmark < leftmark
done = True
else
# swap the list items

Temp = alist[leftmark]
Alist[leftmark] = alist[rightmark]
Alist[rightmark] = temp

endif
endwhile
# swap the pivot with alist[rightmark]
Alist[start] = alist[rightmark]
Alist[rightmark] = pivot

return rightmark
endfunction partition

function quicksort(alist, start, end)


if start < end
# partition the list
split = partition(alist, start, end)
# sort both halves

Quicksort(alist, split – 1, end)


quicksort(alist, split+1, end)
endif
return alist

3
Homework 4 Merge sort and quick sort
Unit 12 Algorithms
endfunction quicksort

alist = [9, 5, 4, 15, 3, 8, 11]


sortedList = quicksort(alist,0,len(alist) - 1)
print(sortedList)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy