Algorithms_Unit_Test
Algorithms_Unit_Test
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.
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.
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 )
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.
Which algorithm
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
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 )
return a; }
}
return a;
}
Without actually opening Processing, code SS in Processing as best you can. ( / 6 marks)
}
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.
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)