1

First, here is my code for the O(n):

import java.util.*;

public class BigO{

  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a number: ");
    String userInput = in.nextLine();
    int mNum = Integer.parseInt(userInput);

    int y = new BigO().linear(mNum);
    System.out.println(y);
  }

 //O(n) - Linear
 public int linear(int n) {
  int sum = 0;
  for (int j = 0; j < n; j++) {
   sum++;
   System.out.print(sum + " ");
  }
  return sum;
 }

I apologize if this is a dumb question because I haven't done big-O notation for a long time and I want to make sure, but for whatever that I have above, is it a Bottom-Up or Top-Down calculation? If it's neither, how can I approach for either one of them (or both)? Please let me know. Thanks.

UPDATE: Alright, nevermind, I've asked some of my friends who are in my class as well as the professor and he written down our problem incorrectly. He corrected it and meant to say we were suppose to use this type of O(n) time algorithm for the recursive fibonacci. Sorry about that lol.

user207421
  • 305,947
  • 44
  • 307
  • 483
ZetaX9
  • 37
  • 1
  • 6
  • 8
    I swear I don't understand one single bit of your question ! – Razvan Aug 24 '12 at 20:30
  • Which part don't you understand? – ZetaX9 Aug 24 '12 at 20:32
  • There is no *mechanical procedure* to determine `Big-O`. [This answer](http://stackoverflow.com/a/4852666/716076) explains `Big-O` quite well. – Chris Dargis Aug 24 '12 at 20:35
  • @Razvan looks like some people can't explain that OP is mixing potatoes and gears. What have these in common? Nothing, that's what must be explained. ZetaX9, yes, this is a code with Big O notation = O(n), if that's what you were asking. – Luiggi Mendoza Aug 24 '12 at 20:36
  • Well that's not what I meant and I know that's a O(n). I was asking if that was either top-down or bottom-up approach, but I guess I need to ask my professor about this again. Thanks for the info though. :) – ZetaX9 Aug 24 '12 at 20:36
  • 1
    Your linear algorithm is so simple, I don't see how either top-down or bottom-up can be used to describe it. – hatchet - done with SOverflow Aug 24 '12 at 20:40
  • Alright, nevermind, I've asked some of my friends who are in my class as well as the professor and he written down our problem incorrectly. He corrected it and meant to say we were suppose to use this type of O(n) time algorithm for the recursive fibonacci. Sorry about that lol. – ZetaX9 Aug 24 '12 at 20:45

1 Answers1

5

The Big O has nothing to do with top-down/bottom-up.

  • The first one refers to a way of representing an algorithms complexity and running time, depending on the size of its input.
  • The second refers to the approach taken when breaking a problem up in subproblems in order to reach a solution.

All these are very accessible via a Google search :) .

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69