• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of O Notation

 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm trying to figure out the answer to these Order questions. I tried to answer a few, can you tell me if they are wrong/right and if wrong what the right answer would be? Thanks.

Here are the questions

What is the order of each of the following tasks in the worst case?

1. Computing the sum of the first n even integers by using a for loop?

2. Displaying all n integers in an array

3. Displaying n integers in a sorted linked list

4. Displaying all n names in a circular linked list

5. Displaying one array element

6. Displaying the last integer in a linked list

7. Searching an array of n integers for a particular value by using a binary search

8. Sorting an array of of n integers into descending order by using a mergesort

9. Adding an item to a stack on n items

10. Adding an item to a queue of n items


Ok here are my guesses.

1. You would begin with O(n/2) but since 1/2 is a constant its just O(n)

2. O(n) ???

3. O(n) ???

4. O(n) ???

5. Constant O(1)

6. Constant O(1)

7. You would begin with O(n/2) but since 1/2 is a constant its just O(n)

8. You would begin with O(n/2) but since 1/2 is a constant its just O(n)

9. Constant O(1)

10. Constant O(1)

How did I do? Can you help me with the ones I got wrong? Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

6. Displaying the last integer in a linked list

6. Constant O(1)


Depends on the linked list implementation. If the linked list has a tail pointer, then it is O(1). Otherwise, it is O(n).
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

7. Searching an array of n integers for a particular value by using a binary search

7. You would begin with O(n/2) but since 1/2 is a constant its just O(n)


Only in the worst case. In the average case, it is O(log n).
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
8. Sorting an array of of n integers into descending order by using a mergesort

8. You would begin with O(n/2) but since 1/2 is a constant its just O(n)


If my memory serves, this one should be O(n log n).
 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2,3,4 correct? thanks.
 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, all the other ones are right (IMO).
 
Alberto Caraz
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure about the wording of 7. By saying you're using binary search, I'm assuming that the array is already sorted. If that's the case, the absolute worst case is O(ln n). Otherwise, it will take O(n ln n) peprocessing time to sort them.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alberto Caraz:
I'm not sure about the wording of 7. By saying you're using binary search, I'm assuming that the array is already sorted. If that's the case, the absolute worst case is O(ln n). Otherwise, it will take O(n ln n) peprocessing time to sort them.


Yep, this is true.
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. I totally missed "array" in the description -- and just assumed some sort of unbalanced binary tree.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic