Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Assignment 1 Analyses

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.

Important Piece(s) of Code:


1) As stated, the below code will find the total number of pairings a female has by
subtracting the first index of a height greater than the female (found using binary search) from
the total male count. This, of course, is done in a for loop using i as its index.
System.out.printf("%d\n",
maleCount - FindLowestValueBinSearch(femaleBunnies[i], maleBunnies));

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.

Important Piece(s) of Code:


1) The actual magic is done using the below line of code. It finds the total number of
inversions by simply adding one and also recognizing how many potential spots also have
inversions. The (middle newLow) part took awhile to figure out, since I simply thought
adding one would be sufficient. I should note count holds the number of inversions.
count += (middle - newLow);
count++;

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.

Important Piece(s) of Code:


1) The formula we talked about above, but in code form. Its a lot uglier when its not
nicely formatted. Note that - 1 is done on UsingAsMaxin order to ensure we dont try to log
positions of bunnies that are already outside the storm, and - 1 is also done on
minCoordinate to subtract all bunnies that are NOT in the storm. This prevents extraneous
bunny data from getting through.
return (cumulativeNumBunnies[usingAsMax - 1] cumulativeNumBunnies[minCoordinate - 1]) * maxCoordinate (cumulativeBunnyPositions[usingAsMax - 1] cumulativeBunnyPositions[minCoordinate - 1]);

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.

Important Piece(s) of Code:


1) This is basically what the entire program was built around. If we find a peak support
on the left, add one to our numLeft value. The same is done for the numRight value. After
looping, we then multiply the two together and add that value to our peak count. We repeat
this step for each loop through we do.
if (intArray[i] > intArray[j])
{
if (i > j)
{
numLeft++;
}
else if (i < j)
{
numRight++;
}
}
}
count += (numLeft * numRight);

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.

You might also like