Skip to main content

Posts

Showing posts from September, 2019

Big O calculation rules

There are rules which helps us to identify Big O notations. Rule 1 :- Worst case Rule 2 :- Remove Constants Rule 3 :- Different terms for input Rule 4 :- Drop Non dominants Rule 1: The very first role when it comes to big-O that is worst case when calculating big. if you look at above function we're looping through the entire array to find "rakesh". 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com . algo . practise ; import java.util.concurrent.TimeUnit ; public class Performance { public static void main ( String [] args ) { String ar [] = { "rks" , "rk" , "rakesh" , "singhania" , "kumar" }; long timeMilli1 = System . nanoTime (); for ( int i = 0 ; i < ar . length ; i ++) { System . out . println ( "Loop" ); if ( ar [ i ]. equals ( "rakesh" )) { System . out . p...

Big O notation

Big-O Analysis of Algorithms The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. Don't get too hung up on this.  We want to use this as an example to measure how long it takes for this function to run.  We can do this in Java by saying let's say Time0 is going to  start this timer before the loop happens. And then when the loop ends I'm going to have another timer called T-1   package com. algo . practise ; import java.util.Arrays ; import java.util.concurrent.TimeUnit ; public class Performance { public static void main (String[] args) { String ar[] = { "rks" , "rk" , "rakesh" , "singhania" , "kumar" }; // Returns current time in millis long timeMilli1 = System. nanoTime (); for ( int i = 0 ; i < ar. length ; i++) { if (ar[i]. equals ( "rakesh" )) { System. out . println ( "found" ); } } long timeMil...