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

Algorithms_Unit_Test

Its an Agorithms test

Uploaded by

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

Algorithms_Unit_Test

Its an Agorithms test

Uploaded by

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

Unit test on algorithm efficiency

Instructions
1. You may use a scientific calculator and scratch paper.

2. You may not use any apps or applications (e.g. Processing, Desmos, Google, etc.) or open any
notes or web pages except for this document and our Google Classroom.

3. You may not use a phone.

4. You may not give help to others, or ask for help from others, or accept help from others while
writing this test. Doing so will be considered cheating and will result in a discussion with your
VP and parents or guardians, and loss of academic awards at commencement. You will also
have to write a harder version of this test.

In your own words, promise me that you will abide by instruction #4 above.

I promise that for the duration of this test, I will abide by


instruction number four.

60 marks total

I P Y S B O

IPYSBO
(P)
IPYSBO
(Y)
I P S Y B O (S)
BIPSYO
(B)
BIOPSY
(O)

( / 6 marks )

T(n) = (2n-1)(3n+1)(4n+5) + 6n2 3


𝑂(𝑛 )

T(n) = 36n + 984 𝑂(𝑛)

Bubble sort (BS) 2


𝑂(𝑛 )

Insertion sort (IS) 2


𝑂(𝑛 )

Merge sort (MS) 𝑂(𝑛 𝑙𝑜𝑔 𝑛)


Searching for a bridge in the dark by first walking 1 2
𝑂(𝑛 )
meter right, then 2 meters left, then 3 meters right,
then 4 meters left, and so on until you find it.
Assume the bridge is n meters away at the start.

Doing the same as above, except you walk 1 meter 𝑂(𝑛)


right, then 2 meters left, then 4 meters right, then 8
meters left, then 16 right, and so on.

Marking a stack of n quizzes, where each quiz takes O(n)


10 minutes

Drawing this spider using 13,470 polygons 𝑂(1)


( / 9 marks )

The above graph shows the number of comparisons made by insertion sort (IS), bubble sort (BS) and
merge sort (MS) on randomly generated arrays of various sizes.

State which curve is which. ( / 3 marks )

Which algorithm

Red curve This curve is bubble sort.


(top)

Blue curve This curve is insertion


(middle) sort.

Green curve This curve is merge sort.


(bottom)

In 2-3 sentences, explain your answers above. ( / 3 marks )

Bubble sort is the red curve as it trends upwards at a steady rate, resembling the parabola given
by bubble sort’s T-notation. Insertion sort is the middle curve its shape closely resembles the
red curve’s, although it is less steep and more erratic as it overall performs better than bubble
sort, but only in specific cases. Merge sort is the green curve as it is the least steep of these three
curves, reflecting merge sort’s 𝑂(𝑛 𝑙𝑜𝑔 𝑛) time complexity.

Explain why the middle blue curve is not perfectly smooth. ( / 2 marks )
The middle blue curve is not perfectly smooth because insertion sort varies in it’s ability to sort
efficiently, depending on the array. Somewhat sorted arrays are sorted best by insertion sort,
but backwards sorted arrays are sorted as badly as bubble sort would.

Use formulas to show why the point (25, 300) is on the top red curve (shown as a black dot) and why
the point (64, 384) is on the bottom green curve (shown as a red dot). ( / 4 marks )

(25, 300) on the top curve (64, 384) on the bottom curve

𝑛(𝑛−1) Merge sort makes 𝑛 𝑙𝑜𝑔 𝑛 comparisons. For


Bubble sort makes 2
comparisons. For
the sake of this question, I will state it as a
the sake of this question, I will state it as a
function of x.
function of x.

𝑥(𝑥−1) 𝑓(𝑥) = 𝑥𝑙𝑜𝑔2𝑥


𝑓(𝑥) = 2 Substituting x = 64,
Substituting 𝑥 = 25, 𝑓(64) = 64( 𝑙𝑜𝑔264)
25(25−1)
𝑓(25) = 2 𝑓(64) = 64(6)
25(24)
𝑓(25) = 2
𝑓(64) = 384
600
𝑓(25) = 2
𝑓(25) = 300

How would bozo sort rank among the three algorithms shown above? Explain why. ( / 2 marks )

Bozo sort would rank the worst among these three algorithms. It would take no actual
procedure to sort the array, instead just shuffling it randomly and performing a check to see if it
is sorted, and then continuing to shuffle it randomly if it was not sorted.

This algorithm makes, for an array of size 𝑛, 𝑛! possible permutations (array possibilities), and
on each shuffle, performs n checks to see if the array is correctly sorted. This gives bozo sort a
time complexity of 𝑂(𝑛(𝑛!))

Explain to your 10-year-old brother how to sort a deck of 32 cards using merge sort. If you don't have a
10-year-old brother, invent a name for him. ( / 4 marks )

Merge sort’s goal is to split a list of items into small, sorted lists with one item each.

For a deck of 32 cards, merge sort will first split those lists into two halves, each with 16 cards.
Then merge sort will split each of these decks into two more decks, each with 8 cards. Merge
sort will keep doing this until, eventually, it has 32 decks that each have one card.

Merge sort will then begin to combine these decks. It will combine two one-card decks into a
single deck of 2 cards, and sort it. Once this has been done for all the one-card decks, it will
combine the two-card decks into single, sorted decks of 4 cards, then combine those into sorted
decks of 8 cards, until, eventually, we have two sorted decks of 16 cards each.

Then merge sort will combine those halves into one big deck of 32 cards, and sort it.

Suppose a father at the beach has lost track of his 3-year-old daughter, who wandered along the beach in an
unknown direction while the father was distracted. He doesn't know in which direction she went, or how
far away she is. Describe an efficient way for the father to search along the beach for his daughter. Assume
the shoreline is straight and extends for several kilometers in both directions.
( / 3 marks )

An efficient way for the father to search along the beach for his daughter is to go in one
direction for 100 meters, then go in the other direction for 200 meters, and then go back to
search in the original direction for 400 meters, and then go back in the other direction for 800
meters, repeating this back and forth and doubling the distance search each time until he has
found his daughter.

Without actually opening Processing, write Processing code for bubble sort and insertion sort as best you
can. Use spaces for indentation. ( / 10 marks )

int[] bubblesort( int[] a ) { int[] insertionsort( int[] a ) {


int n = a.length; int n = a.length;
for (int p = 1; p <= n-1; p++){ int c;
for (int i = 0; i <= (n-p)-1; i++){ for (int h = 1; h <= n - 1; h++){
if (arr[i+1] < arr[i]){ c = h;
swap(a[i], a[i+1]); while (c > 0 && (a[c-1] > a[c]){
} swap(a[c], a[c-1];
} c--;
} }

return a; }
}

return a;
}

Selection sort (SS) sorts an array A as follows:


First, it finds the smallest item in the array, then swaps that item with A[0].
Next it finds the 2nd smallest item in the array and swaps that item with A[1].
Next it finds the 3rd smallest item in the array and swaps that item with A[2].
…And so on, until the array is sorted.

Without actually opening Processing, code SS in Processing as best you can. ( / 6 marks)

int[] selectionSort( int[] a ) {


int min;
int n = a.length;
for (int i = 0; i < n-1; i++){
maxIndex = i;
for (int j = i+1; j <= n-1; j++){
if (a[j] < a[min]){
min = j;
}
}
if (min != i){
swap(a[i], a[min]);
}

}
return a;
}

What is the Big-O runtime of SS? In 60-100 words, explain why. ( / 4 marks)

2
The big-o runtime of selection sort is 𝑂(𝑛 ). Selection sort utilizes two loop, one to set the initial
minimum element as the first element, and the second to check if the minimum element’s index
has changed to one in the still unsorted array. If it has changed, it swaps the items. If it hasn’t, the
minimum has been found, and 𝑖 is incremented, and the process repeats again with the second
element now as the initial minimum.

This algorithm utilizes nested for loops, each with a bound of n-1. For this implementation, n-1
items are set as the minimum, with n-i items being compared to in in order to sort them. As such,
2
the algorithm runs in quadratic time as 𝑂(𝑛 ).

Tell me two other things you learned in this unit or found interesting. 30-50 words each. ( / 4 marks)

One thing I learned in this unit was algorithm efficiency. The concept of algorithm efficiency has long been
one that I have tried to understand, but not been able to fully grasp. Through this unit, however, I fully
understand concepts such as T and big-O notation, factors affecting algorithm efficiency, and what makes an
algorithm efficient.

One thing I found interesting about this unit is that it is impossible to make an algorithm that runs in linear,
or 𝑂(𝑛) time. Even with our modern knowledge about algorithm efficiency and techniques to create an
efficient algorithm, we still cannot create a linearly scaling algorithm. What I have also found interesting
about this, however, is that we have created more efficient algorithms, that run in times even better than
linear, such as 𝑂(𝑙𝑜𝑔 𝑛) time.

Bonus question for up to +2 recovery marks

One night, I had the following nightmare. I mark 25 unit tests, then go home. When I come back to
school the next day, suddenly there are 50 more tests to mark. I mark those, then go home. When I
come back to school the next day, there are 100 more tests to mark. Then 200 the next day, then
400 the next, then 800 the next. This lasts for 20 days, at which point I quit teaching and take up
snow shoveling.

If this nightmare were true, compute how many unit tests I would mark before quitting. Show all
steps. ( / +2 marks)

You might also like