Professional Documents
Culture Documents
Assignment 1 Analyses: System - Out.Printf ("%D/N", Malecount - Findlowestvaluebinsearch (Femalebunnies (I), Malebunnies) )
Assignment 1 Analyses: System - Out.Printf ("%D/N", Malecount - Findlowestvaluebinsearch (Femalebunnies (I), Malebunnies) )
Christopher Perkins
9/7/16
Pairings
Approach
My approach to pairings involved sorting an array of the male bunnies heights and then
binary searching through the array of male bunnies. I used home-grown merge sort code in
order to sort the bunnies, and for each female bunny I subtracted the first index of height
greater than the females height from the total number of males. The result will always be the
number of males that are greater than female bunnies since the heights for males are sorted.
Run-time Analysis
Because the program uses merge sort and a binary search within a for loop, both of
which are O(n log n), we receive our runtime. Our Big-O runtime should be O(n log n).
_____________________________________________________________________________________
Inversions
Approach
For inversions, I found (before you said it in class, I felt so clever) that you could use
merge sort in order to count how many inversions there were. An alternative method to this
could also be bubble sort, but this would provide a worse Big-O result. Both methods work since
each swap looks at each individual number, and therefore you can simply count how many
switches are down in order to determine how many inversions there are.
Runtime Analysis
Just like the last problem, we use merge sort. We also use a for loop to scan each
individual item in, but this is negligible under Big-O notation. Therefore, our Big-O runtime will
be O(n log n).
_____________________________________________________________________________________
Escape
Approach
This one was annoying, to be upfront. I had it running on O(n log n) and O(n2), but the
pure fact that it could be ran in O(n) made me search for the perfect (in a Big-O sense) solution. I
hold two cumulative arrays of all positions that bunnies could be at in an x coordinate fashion.
The first array, cumulativeNumBunnies, holds the number of bunnies that either
precede or are at a point. For instance, if there was a single bunny at or before position 3, then
cumulativeNumBunnies would be equal to 1.
The second array, cumulativeBunnyPositions, holds the total position distance of all
bunnies that either precede or are at a point. For instance, if there was two bunnies, one at
position 1 and one at position 3, cumulativeBunnyPositions would look as follows: {0, 1, 1, 4}.
Of course, we have two arrays like this for a reason. As our scenario coordinates come
in, we can quickly find the total distance the bunnies would travel to get to point b using the
formula below.
numBunnies refers to how many bunnies are in the storm, endCoordinate refers to
the endpoint of the storm, and relevantAggregateBunnyDistance refers to the total position
value of all bunnies that are in the storm. This works by getting the number of storms for each
bunny, so each bunny has to travel to its own endpoint. We subtract the aggregate distance in
order to achieve the total distance that bunnies are at. A more detailed breakdown of the
formula itself (and how it was derived) is in the code itself if my explanation here was unclear.
2) You may notice I used a variable usingAsMax rather than maxCoordinate. This is
because if a bunnys last position lies before the end of a store, our computer would crash since
the array I create is only the size of the last coordinate position of a bunny plus one. Therefore,
to prevent from going out of bounds, we use the following if statement:
int usingAsMax = maxCoordinate;
if(usingAsMax - 1 >= cumulativeNumBunnies.length)
{
//We don't worry about going out of bounds thanks to "- 1".
usingAsMax = cumulativeNumBunnies.length;
}
3) Finally, our distance is stored in an array. I wasnt sure how you wanted the answers
output. If the answers could be outputted as soon as we got one pair of coordinates as opposed
to waiting until we receive all input, the distance found below could be printed immediately
instead of waiting for all input.
distances[i] = FindBunnyDistance(cumulativeNumBunnies,
cumulativeBunnyPositions, minCoordinate, maxCoordinate);
}//Closes for loop for getting user inputted coordinates.
PrintArray(distances);
Runtime Analysis
As stated above, this code was redone several times to finally reach O(n). This is
ensured because we do not have any nested for loops. While there are certainly numerous for
loops ran during the process, none of them are within another. Therefore, our process scales on
a level equal to the input we receive.
_____________________________________________________________________________________
Peaks
Approach
Peaks was rather straightforward to me. The result can be found by simply finding the
amount of peak supporters (that is, a value lower than the peaks value) to the left and right,
then multiplying the two numbers together to find the number of pairs that could create a peak.
To find this, we go through each individual peak candidate, and then we go through each peak
supporter position.
2) This is more important for Big-O than anything, but I thought I should note it anyway.
Nothing special, just loop through each peak position then each position in the user-inputted
values array
for(int i = 1 ; i < arrayLength - 1 ; i++)
{
for(int j = 0; j < arrayLength; j++)
Runtime Analysis
Because the heaviest piece of work this code does is a nested for loop, we receive our
runtime to be equivalent to O(n2). All other code within the for loops can be considered O(1),
and are therefore negligible.