File Organization and Processing
Lecture 2
Running Time in Big O Notation
by: Aisha Alasmari
Introduction
• When solving a computer science problem there will usually be more than just one
solution.
• These solutions will often be in the form of different algorithms, and you will generally want to compare the algorithms to see which one is more efficient.
Introduction
• This is where Big O analysis helps – it gives us some basis for measuring the efficiency of an algorithm.
• Big O measures the efficiency of an algorithm based on the time it takes for the algorithm to run as a function of the input size.
History of Big-O-Notation
• Big O notation (with a capital letter O, not a zero), also called Landau's symbol.
• Big O notation used in complexity theory, computer science, and mathematics to
describe the basic behavior of functions.
• Basically, it tells you how fast a function grows or declines.
History of Big-O-Notation
• Landau's symbol comes from the name of the German mathematician “Edmund Landau”
who invented the notation.
• The letter O is used because the rate of
growth of a function is also called its order.
What is Big-O-Notation?
• It describes :
– Efficiency of an Algorithm.
– Time Factor of an Algorithm.
– Space Complexity of an Algorithm.
• It is represented by O(n) where n is the number of operations.
Big-O-Notation
• Example:
• Array[1000] , We want do Searching in array.
• Several algorithms can be used to solve. But what is the best algorithm?
• The best in terms of Efficiency, Performance and Space.
Big-O-Notation
• Example:
• Array[1000] , n = 1000
– Algorithm1 >> O(n) >> O(1000) = 1000 second
– Algorithm2 >> O(n2) >> O(10002) = 1000000 second – Algorithm3 >> O(Log2 n) >> O(Log21000) = 10 second
• Which algorithm is best in search?
– Algorithm3 >> Algorithm1 >> Algorithm2
Big-O-Notation
• Example:
• What is the Sum of the array[10]?
• Use Loop in programming , go to each element and collect it.
• What is Big-O-Notation for this Algorithm? O(n) n size of array.
10 40
5 19
8 3
21 4
11 30
Big-O-Notation
• Example:
• What is the Maximum of the array[10]?
• Take the first element and called Max 30 and compare all other elements, check if found grater than 30
replace it Max 40 , if not found still 30 maximum.
• What is Big-O-Notation for this Algorithm? O(n) , n size of array.
10 40
5 19
8 3
21 4
11 30
Big-O-Notation
• Best Case:
– This algorithm is efficient and effective as its O is small as compare to the 2nd algorithm And hence it gives the best result in less time.
Big-O-Notation
• Example:
• What is the Maximum of the array[10]?
• Use another algorithm , take the first element and compare all other elements, and check is it maximum or not?. And take the second element and so on…etc
• What is Big-O-Notation for this Algorithm?
n+n+n+n+…..+n = O(n2) , n is operations on array.
“Worst Case”
10 40
5 19
8 3
21 4
11 30
Big-O-Notation
• Worst Case:
– This algorithm is less effective than the other
Because this algorithm has a high Big O and hence It is more time taking and less effective.
Big-O-Notation
Big-O-Notation
Algorithm Running time- Big O
Comment
Linear search O(N) Fair
Binary search O(log N) Good
Insertion in unordered array O(1) Best Insertion in an ordered array O(N)
Deletion in an unordered array O(N) Deletion in an ordered array O(N)
Sorting O(N2) Very bad