CMPS 261 Test 1

Ace your homework & exams now with Quizwiz!

analysis of merge sort complexity and explain

2^log2(n) = N, so that is where the T(1) comes from because it n/2^log(n) = n/n = 1

explain how to utilize an abstract class

Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class. by saying extends, the program which is extended gives all its methods to the subclass

complexity of merge sort and disadvantage

Needs additional memory to store temporary arrays notice how since it is no longer slow i and fast j(tortise and hair) but they can move in paraellel it is much faster

how does frequency count anaylsis work? how does it work for an array? how does it work for multiple variable defined in a signle line?

We keep track of frequency of statements in the program • Each statement in a program adds the value of 1 to the frequency count each time it is executed if redefining already defined variable, 0 gets added tot the space count array takes up m space, where m is its size any initilzation of variables, setting variables equal to a value adds to the space count loops add to the time count, and it is n+1 since it has to check the last time before exiting the loop all other plain statements just add 1 to time also, 1 line is only 1 time even if there are two variables being defined in one line

how should compare to methods work?

a.compareTo(b) returns 1 if a>b 0 if a=b and -1 if a<b

Explain a recursive function for the fibonacci series?

basically breaking each one down into a treee until you get into the leaves

worse case running time of quicksort vs best case REVIEW

best case = O(nlogn)

example on how intialize a new object and utilize its methods

classname variablename = new classname (parameters);

binary search method

compare to function 0 if equal positive is first string is greater than second negative if first string is less than second

how to create a regular class

define class name define variables set variables in cosntructor write methods

frequency count analysis vs big o analysis

frequency count - includes constant and lower level complexity terms where big O removes both

binsort code and complexity?

n = # of array items/ k = number of different possible elements j is defined outside, x ++ O(n+k) this code takes into account if 0 is a possibility

what is the best way to traverse through an array?

since indexs start at 0

remainder division in java

take the remainder whole number!! %

what does it mean if a class implements the comparable function?

you can utilize the compare to function

abstract method syntax

Abstract method has no body • Always end the declaration with a semicolon(;) • It must be overridden. ▫ An abstract class must be extended ▫ in a same way abstract method must be overridden • Abstract method must be in a abstract class

What does a linear search mean?

T(n)=O(n)

describe the divide and conquer algorithims

recursively breaks down a problem in to multiple sub problems ▫ until the resulting problems are simple enough to solve immediately ▫ The solution for the main problem is then produced by combining the solved sub problems as the algorithm falls out of recursion.

how do strings work in java in terms of indexing, and how do you access a certain character?

same as arrays first letter index = 0, last letter index = length-1

syntax to create an arraylist, and why are they useful? how to add items to them?

useful when dealing with unknown sized arrays which are needed

what is important about helper functions? REVIEW

usually make them private, same r4eturn type for both

insertion and deletion of priority que

insert : O(n) Delete : O(1)

complexity of merge sort and quicksort and limitations of each

nlogn memory for merge sort partition for quick sort(try to be the middle value)

how do you pass an arraylist to a function? how do you return one in a nonvoid function?

nonvoid function = Arraylist<type>

how does an allocation of a new variable work? how does a nested for loop work for frequency count analyisis?

not defining a value for a variable, then it adds 0 for time the header of a loop is m+1 the body is m times

what is the merge sort code?

notice it will only enter if it is greater than one, otherwise, you find each component, and then you will sort after creating a bunch of different arrays if the i or k finishes up its entire portion of the array, then since it is already sorted, you can just slide them over

explain inheritance

public class dog extends animal gains all methods of animal, but can be overriden for certain other aspects if a class 1 extends class 2, class 1 is a class 2, but a class2 is not a class 1

merge process visual

will compare the two in parallel, working all the way down, and once the i or j are all the way down, we know since they are already sorted you can just start placing them on the array 1 at a time i is for first half j is for second half k is for the array you are editing

is this code tail recursive? private static boolean isPalindrome(String s, int low, int high) { if (high <= low) // Base case return true; else if (s.charAt(low) != s.charAt(high)) // Base case return false; else return isPalindrome(s, low + 1, high - 1); }

yes, only returns the defintion

what do you need to remember to do about any abstract methods inherited from interfaces or abstract classes?

you must always define them in classes which ARE not abstract if an abstract class implements an interface, you do not need to define them since an abstract class has abstract methods and an interface only contains abstract methods however, once a normal class extends the abstract class or interface, all abstract methods must be defined in concrete code with code body

what is binsort/bucketsort?

- good for a limited range of values such as age - two different arrays (the inputted list and another array which is the size of the amount of bins you need to categorize) - each time a number appears for that bin, you increase the count for that bin - list can be infinitley large but values must fall within a certain range - complexting = O(n+k) where n is the length of the list given

what is the use of an abstract class? what are some things to remember?

-An abstract class has no use until unless it is extended by some other class. • If you declare an abstract method in a class, then you must declare the class abstract as well. ▫ you can't have abstract method in a non-abstract class. • Abstract class can have non-abstract method (concrete) as well.

Explain the process of a recursive function and things recursive functions need?

-basically, you start at the the largest set of possible data, and work your way down layer by layer, where natural recursion USUALLY ends at 0 or 1 -each layer will have an unknown to where the function is called again, and when the function works down to the anatomical units which we know to be true, it plugs in the previous and builds it back up -*recursive functions will call for something smaller (size, length, general number)- * -must have a terminating line checking for the basecase to be met which will ALWAYS APPEAR AT THE TOP OF THE RECURSIVE FUNCTION - you can have more than 1 base case

Abstract Class vs Interface

1. If the object has a few abstract methods and a few concrete methods: declare it as an abstract class. - extend an abstract class 2. If the object has only abstract methods: declare it as an interface - implement an interface they both can have variables and methods

what is the basis of quicksort?

3 arrays one for less than pivot, one for eaul to pivot and one for greater than the pivot value

what is the divide and conquer method?

An algorithm design paradigm where a problem is repeatedly subdivided into smaller problems until the solution to sub problem is trivial. • The solved sub problem is often then combined with other solved sub problems to assemble the solution to the full problem. • Often implemented using recursion.

explain the relationships between multiple interface and multiple classes

An interface cannot implement another interface ▫ Only a class can • Interface can extend other interface • A class can implement multiple interfaces ▫ Interfaces with methods of same name Possible if same return data type Error: if different data type ▫ Variable conflicts could be resolved by using interface name

big O notation and example

Syntax: O(largest-magnitude-term) Example: O(n2 ) make sure to right assumptions so we can get into terms of 1 variable

easiest way to copy an array

System.arraycopy(list, 0, firsthalf, 0, list.length/2); the other array must be created first before copying

how do strings work in terms of comparison?

a comes before b, so b>a later in the alphabet means greater values( if compare To function returns >0 value, that means first is greater than second, <0 means second is less than the first, and =0 means they are the same

what is the total complexity for quick sort binary search?

add together(two separate entitites) nlogn+logn

what do you need to notice about returns of recrusive functions?

everything comes after the return

what is radix sort? what is its complexity?

examines each digit of every number separately, and then if it is 0, moves to the front, and starts fro m the least valued digit and then moves to the tens, hundreds, thousands, and when youre done it is in the correct order Total time is 𝑂(𝐷(𝑛 + 𝐵)) so do not use if you have a large number of digits D = digits, B = base

what do you need to remember about the complexity of any nested loop?

nested twice = n^2 thrice = n^3 etc

what is tail recursion and why is it used?

makes recursion behave more similar to a lop by putting it at top of the function and have no other dependent operations return MethodA(); - no dependent operations return MethodA() +MethodA(); not tail recrusive because it depends on other operations Fibbonaci and nfactorial functions are not tail recursive do to depenednet natures of the code When there are no pending operations to be performed on return from a recursive call.

how does quicksort and merge sort differ?

merge sort - always nlogn since repetivly dividing but uses a lot of extra memory quick sort - best case nlogn but can get up to n^2 but doesnt use extra memory

interface syntax and requiremens? variables?

milarity with class ▫ Can have variables ▫ Can have methods • Difference with class ▫ Methods must be abstract interfaces can have variables?

do you want compexity to be complex or not complex for a program? use an example to explain why Assume that, in the worst case, an algorithm takes 1 second to process 100 elements. How long will it take to process 2,000 elements if the growth rate of the algorithm is? (a) O(N) (b) O(N^2)

n is the number of times it takes to solve the program (k * number of elements N) = time

what is selection sort? complexity?when to use?

n^2 it is an inplace sort - no addiotnal memory small list

How does a helper function work for a recursive function? REVIEW!

same name, but it has different parameters of different types which is how the computer knows to use it- it seems you need to use it when pass parameters which do not include the full amount of variables needed so you create a new one which derives variables given off of the user inputted ones it has return statement on the call!!

time complexity of binary search (review?)basically what does it mean when repeatedly dividing by a number?

splitting in half is log2(N) splitting in thirds is log3(n) you split the area in half each time assuminh power of n, 2^n/n = 2^n-1 so 2^5/2= 2^4... basically repeatedly dividing by 2 is log2(n) etc - you take down the complexity to a logn complex

merge sort visual

you will basically keep dividing the array into halfs, once it is in anatomical units or single elements, compare the two that were preveiously in a pod and then order them, and then do that for the next two, and order that four to get back to the sorted sequence complete the left arrays first and then you move on to the right side each merge sort call will create two separate arrays until it gets down to two single array whree they compare the values and arrange using merge function,

what is insertion sort? complexity for insertion and total time?! when to use?

• Idea: like sorting a hand of playing cards • Start with an empty left hand and the cards facing down on the table. • Remove one card at a time from the table, and insert it into the correct position in the left hand ▫ compare it with each of the cards already in the hand, from right to left • The cards held in the left hand are sorted ▫ these cards were originally the top cards of the pile on the table n^2 at worse, n at best, for time in place sort as well - no additional memory required use for a small list or already sorted list with few positions out of place in place sort

what is the best way to pick a pivot value?

• Strategy 3: Median-of-three Partitioning ▫ Ideally, the pivot should be the median of input array A Median = element in the middle of the sorted sequence ▫ Would divide the input into two almost equal partitions ▫ Unfortunately, its hard to calculate median quickly, without sorting first! ▫ So find the approximate median Pivot = median of the left-most, right-most and center element of the array A Solves the problem of sorted input

what is a bubble sort? complexity? what can you do to improve bubble sort? when to use?

• Traversing from last index, each neighboring pair of elements is swapped if the neighbors are out of order • The first pass ends with the first element being in order. • Each successive pass traverses one less element in the list until there are no more elements that can be swapped n^2 - no additional memory used either If at any time a traversal ends with no swaps having taken place, the list is sorted. easy to implement and no additional memory

what are some requirements of interfaces?

• We can not instantiate an interface • Must be implemented by a class ▫ implements keyword is used by classes to implement an interface • Class must implement all abstract methods by Provides body to the abstract functions If not, declare the class as abstract • Interface methods ▫ Default: abstract and public • Interface variables: must be initialized ▫ Default: public, static and final ▫ final: can be assigned only once, cannot be changed

What is upper bound analysis? (big o Analysis) what is the process?

● In general, we care about the long-term behavior of functions and we just want to find nice polynomials that bound the growth of our actual functions. find the most exexuted instruction, remove all constant, and keep the term for the largest value of n keep only the most complex term! Review ascending order list on moodle///////// It is very difficult to compare algorithms by measuring their execution time. To overcome these problems, a theoretical approach was developed to analyze algorithms independent of computers and specific input. This approach approximates the effect of a change on the size of the input. In this way, you can see how fast an algorithm's execution time increases as the input size increases, so you can compare two algorithms by examining their growth rates.


Related study sets

Ch. 9 Antibacterial drugs that interefere with DNA/ RNS synthesis

View Set

Chapter 5: Settings for Psychiatric Care

View Set

Interpersonal Communication Midterm

View Set