int n = 100 for(int i = 0; i < n; i++) //this loop is executed n times, so O(n) { for(int j = n; j > 0; j/=2) //this loop is executed O(log n) times { } }
Explanation: The outer for loop should be clear; it is executed n
times. Now to the inner loop. In the inner loop, you take n
and always divide it by 2
. So, you ask yourself: How many times can I divide n
by 2
?
It turns out that this is O (log n)
. In fact, the base of log
is 2
, but in Big-O notation, we remove the base since it only adds factors to our log
that we are not interested in.
So, you are executing a loop n
times, and within that loop, you are executing another loop log(n)
times. So, you have O(n) * O(log n) = O(n log n)
.
Related Posts:
- Example of O(n!)?
- A tool for calculating the big-O time complexity of Java code? [closed]
- Why does cache use Most Recently Used (MRU) algorithm as evict policy?
- Finding the max/min value in an array of primitives using Java
- Find how many connected groups of nodes in a given adjacency matrix
- Good Java graph algorithm library?
- Generating all permutations of a given string
- Efficient swapping of elements of an array in Java
- Java, Shifting Elements in an Array
- Creating a maze solving algorithm in Java
- What is the difference between an Abstract Data Type(ADT) and a Data Structure?
- How to implement a most-recently-used cache
- How Do I Implement an Insertion Sort Method for A Generic ArrayList?
- How to format strings in Java
- What exactly is Apache Camel?
- What is an instance variable in Java?
- How does the Java ‘for each’ loop work?
- Cannot make a static reference to the non-static method fxn(int) from the type Two
- How to implement a tree data-structure in Java?
- Difference between StringBuilder and StringBuffer
- How can I use pointers in Java?
- How do I generate random integers within a specific range in Java?
- A long bigger than Long.MAX_VALUE
- How to remove single character from a String
- Unsupported major.minor version 52.0 [duplicate]
- How do I limit the number of decimals printed for a double?
- Unfortunately MyApp has stopped. How can I solve this?
- exception in thread ‘main’ java.lang.NoClassDefFoundError:
- java.io.FileNotFoundException: the system cannot find the file specified
- How to parse this string in Java?
- How to match “any character” in regular expression?
- Raw use of parameterized class
- :: (double colon) operator in Java 8
- What does ‘0’ do in Java?
- Service will not start: error 1067: the process terminated unexpectedly
- Explain the use of a bit vector for determining if all characters are unique
- Big O, how do you calculate/approximate it?
- Convert java.util.Date to String
- How do I do a deep copy of a 2d array in Java?
- Returning an empty array
- Java Security: Illegal key size or default parameters?
- Could not reserve enough space for object heap
- How to change a package name in Eclipse?
- Are arrays passed by value or passed by reference in Java?
- How do I get “Press any key to continue” to work in my Java code?
- What does “?” mean in Java? [duplicate]
- What is the difference between a local variable, an instance field, an input parameter, and a class field?
- Error: Selection does not contain a main type
- Java, “Variable name” cannot be resolved to a variable
- JRE 1.7 – java version – returns: java/lang/NoClassDefFoundError: java/lang/Object
- Integrate Ant builder into Eclipse: Error “Variable references empty selection”
- paint() and repaint() in Java
- Good Hash Function for Strings
- Error – Illegal static declaration in inner class
- Bubble Sort Manually a Linked List in Java
- Checking Password Code
- java – how to create and manipulate a bit array with length of 10 million bits
- What is the difference between object-oriented languages and non object-oriented languages?
- Installing jdk8 on ubuntu- “unable to locate package” update doesn’t fix
- Java – Access is denied java.io.FileNotFoundException
- Static Block in Java
- What does Scanner input = new Scanner(System.in) actually mean?
- Class ‘Room’ is abstract; cannot be instantiated
- error: incompatible types: char cannot be converted to String – Java
- mongoDB, connection refused
- Displaying Image in Java
- Median of Medians in Java
- How to check the input is an integer or not in Java?
- How to read and write XML files?
- Why am i getting ” Duplicate modifier for the type Test” and how to fix it
- Picked up _JAVA_OPTIONS: -Xmx256M
- When to use static methods
- How to serialize an object into a string
- Getting the array length of a 2D array in Java
- Java String to SHA1
- Create a two dimensional string array anArray[2][2]
- Convert double to Int, rounded down
- A beginner’s error (args[0]) [duplicate]
- Converting from byte to int in Java
- When/why to call System.out.flush() in Java
- Throwing multiple exceptions in Java
- fixing errors on a program to call methods in java
- How to Set the Background Color of a JButton on the Mac OS
- How to easily convert a BufferedReader to a String?
- How can I calculate a time difference in Java?
- Detecting a long press with Android
- How to increase the size of an array in Java?
- Cannot invoke toString() on the primitive type int
- Difference between O(n) and O(log(n)) – which is better and what exactly is O(log(n))?
- Can you write virtual functions / methods in Java?
- Creating video player using Java
- Can I override and overload static methods in Java?
- How can i fix this equals on primitive type(int)
- Split string into array of character strings
- intellij idea – Error: java: invalid source release 1.9
- How fix Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
- Is it okay to throw NullPointerException programmatically?
- Android: how to create Switch case from this?
- Read next word in java
- Convert float to double without losing precision