0

Hi there I have been researching and trying to learn hw to check for the time complexity of certain algorithms. I've seen this video which was very helpful.

That being said I wondered off and started trying to work out the Worsts Case and average case of certain algorithms.

Program #1: This is a calculator which calculates in RPN format.

function evaluate() {
        var input = prompt("Please enter your input string\n\nExamples of input strings:\n\n\t1. 10 4 5 + *\n\t2. 10 4 5 + * 2 +\n\t3. 10 8 *");
        var values = input.split(" ");
        var array = new Array();
        for (i in values) {
            if (values[i] != "+" && values[i] != "*" && values[i] != "-" && values[i] != "/") {
                array.push(parseInt(values[i]));
            } else {
                var operator = values[i];
                var val2 = array.pop();
                var val1 = array.pop();
                switch (operator) {
                    case "+":
                        array.push(eval("val1 + val2"));
                        break;
                    case "*":
                        array.push(eval("val1 * val2"));
                        break;
                    case "-":
                        array.push(eval("val1 - val2"));
                        break;
                    case "/":
                        array.push(eval("val1 / val2"));
                        break;
                }
            }
        }
        if (input == "" || input == null) {
            document.writeln("Oops, based on your input we have nothing to calculate for you!");
        } else {
            document.writeln("Your RPN calculation is: ");
            document.writeln(array + " with a starting input string of: " + input);
        }
    }

From what I've understood in this case the the for loop in this case is running for a constant number of times and that is the length of the array then performs a O(1) instruction hence the function reduce has a Time Complexity of O(1) no matter the size of the array passed.

Program #2: Check whether the number is Prime or Not

function isPrime(num) {
    if(num <= 1) return false;
    const limit = Math.floor(Math.sqrt(num));
    for(let i = 2; i <= limit; i++) {
        if(num % i === 0) return false;
    }
    return true;
}

Since there is one for loop and the program needs to at least look at every number from 2 to the inputted number hence I think it is O(n).

For now these are the one's I've done with lots of thinking and am hoping they are correct. My question is there an easy way on how to check the time complexity of functions I've never seen before.

P.S I THINK they are correct. Please if they aren't specify which and why.

Thanks for your help :)

Update

Apparently I was wrong and lexicore corrected me the answers are:

#1 No, it is not O(1). Assuming one step takes O(1) and you have n steps where n is the length of the array, time complexity is O(n).

#2 No, it is not O(n). You cycle makes at most sqrt(n) steps, each step O(1) so time complexity is O(sqrt(n)) here.

That being said I tried further and stumbled upon this in my textbook. It basically calculates an approximate square root.

#3

function sqrt(num) {
guess = num / 3;

  do {
    lastGuess = guess; 


    guess = (num / guess + guess) / 2;

   while(Math.abs(lastGuess - guess));

  return guess;  /

Am I correct to say this is O(n) my reasoning is since we loop until an approx is found.

#4 from the user thanksd in the link he calculates the largest number in an array. Is it safe to say that the time complexity in this is O(n) since the largest number may be at the very last, and hence why O(n).

Thanks again

  • 1) But is the length of the array a *constant*? (Possible confusion with terms here, and you also contradicted yourself with the last sentence) 2) There is no array here - look at the loop limits. – meowgoesthedog May 08 '18 at 11:16
  • If your code works but you're looking for comments or improvements, please take it to https://codereview.stackexchange.com/ – Máté Safranka May 08 '18 at 11:19
  • 1
    @matesafranka OP is not looking for help with debugging but for complexity analysis; anyhow this is still more suitable on the CS SE – meowgoesthedog May 08 '18 at 11:34
  • Yes precisely complexity analysis. This code doesn't belong to me but just trying to learn how to analyze it. Regarding #2 yes you are right Sorry it's a misunderstanding what I meant is the input of the user Ill edit that. Regarding #1 what do you mean a constant? –  May 08 '18 at 11:44
  • More importantly, what do *you* mean by a constant? Quoting your reasoning: `... the for loop in this case is running for a constant number of times and that is the length of the array ...` - is it though? – meowgoesthedog May 08 '18 at 13:26
  • @meowgoesthedog Oh sorry my bad. What I was referring to is equal to the number of times it has to loop, which is constant since its always 4 cases (+,-,*,/). –  May 08 '18 at 13:55

1 Answers1

0
  1. No, it is not O(1). Assuming one step takes O(1) and you have n steps where n is the length of the array, time complexity is O(n).

  2. No, it is not O(n). You cycle makes at most sqrt(n) steps, each step O(1) so time complexity is O(sqrt(n)) here.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks @lexicore! It seems I've learnt nothing :/ If I may ask since I'm working some others will of the following function be O(n) my reasoning is since we loop until an approx is found. function sqrt(num) { guess = num / 3; do { lastGuess = guess; guess = (num / guess + guess) / 2; while(Math.abs(lastGuess - guess)); return guess; Thanks –  May 08 '18 at 15:48
  • @user9138698 Please don't ask further questions in comments, post it as a separate question. – lexicore May 09 '18 at 06:35