CMSC 132A Lecture Notes - Lecture 38: Linear Search

108 views4 pages
CMSC132A Lecture 38: Algorithmic Complexity 2
Now we will talk about the algorithmic performance of different operations.
Constant time
This just means the algorithm requires a fixed amount of time to run that does not
depend on the input size.
And now, for an example.
String inputKey, correctKey;
Boolean isCorrectKey(String inputKey, String outputKey) {
this.inputKey=inputKey;
correctKey=outputKey;
if(!(inputKey.eqauls(outputKey)) {
Throw new Error(“That is incorrect!”);
}
Else {
Return true;
}
}
This block of code runs in constant time because the compiler executes line by line until
it reaches the if block. If the statement is true, the program goes into the block and
returns the error message. If the statement is false, the program skips past the if block
and just returns true. Either way, this is a constant time process. There are only two
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Now we will talk about the algorithmic performance of different operations. This just means the algorithm requires a fixed amount of time to run that does not depend on the input size. Boolean iscorrectkey(string inputkey, string outputkey) { this. inputkey=inputkey; correctkey=outputkey; if(! (inputkey. eqauls(outputkey)) { This block of code runs in constant time because the compiler executes line by line until it reaches the if block. If the statement is true, the program goes into the block and returns the error message. If the statement is false, the program skips past the if block and just returns true. Either way, this is a constant time process. There are only two variables at play and you are comparing them. It is not like you are entering variables to compute. Some operations that you need to know that are constant time: This means the algorithm runs with time proportional to the input time.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents