Presentation on theme: "Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code."— Presentation transcript:
Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code –Data structures course Implement the solution using a programming language and concrete data structures –Introduction to computer science
Pseudo Code Pseudo code is a meta language, used to describe an algorithm for a computer program. We use a notation similar to C or Pascal programming language. Unlike real code pseudo code uses a free syntax to describe a given problem
Pseudo Code Pseudo code does not deal with problems regarding a specific programming language, as data abstraction and error checking Use indentation to distinguish between blocks of code (loop and conditional statements) Accessing values of an array is expressed with brackets
Pseudo code Accessing an attribute of an object is expressed by the attribute name followed by the object in brackets (length[a]) More on pseudo code style in the text book
What is the running time of these methods ? Proc2(A[1..n]) i 1; j 1; s 0 repeat if A[i] < A[j] s s + 1 if j=n j=i + 1; i i+1 else j j+1 until i+j > 2n Proc1(A[1..n]) s 0 for i = 1.. n do for j = (i +1).. n do if A[i] < A[j] s s + 1
Analysis of Bubble sort Bubble sort (A) –1. n length[A] –2. for j n-1 to 1 –3. for i 0 to j – 1 –4.if A[i] > A[i + 1] –5. Swap (A[i], A[i +1])
Time Analysis Best case – the array is already sorted and therefore no swap operations are required
Time Analysis Worst case – the array is sorted in descending order and therefore all swap operations will be executed For both inputs the solution requires time
Asymptotic Notation Considering two algorithms, A and B, and the running time for each algorithm for a problem of size n is T A (n) and T B (n) respectively It should be a fairly simple matter to compare the two functions T A (n) and T B (n) and determine which algorithm is the best!
Asymptotic Notation Suppose the problem size is n 0 and that T A (n 0 ) < T B (n 0 ) Then clearly algorithm A is better than algorithm B for problem size n 0 What happens if we do not know the problem size in advance ? If we can show that T A (n) < T B (n) regardless of n then algorithm A is better then algorithm B regardless of the problem size Since we don’t know size in advance we tend to compare the asymptotic behavior of the two.
Asymptotic Upper Bound - O O(g(n)) is the group of all functions f(n) which are non-negative for all integers, if there exists an integer n 0 and a constant c>0 such that for all integers
Asymptotic Notation When we use the term f = O(n) we mean that the function f O(n) When we write we mean that the aside from the function the sum includes an additional function from O(n) which we have no interest of stating explicitly
Example Show that the function f(n)=8n+128 =O(n 2 ) –lets choose c = 1, then
Conventions for using Asymptotic notation drop all but the most significant terms –Instead of we write drop constant coefficients –Instead of we use
Back to improved bubble sort We improve the sorting algorithm so that if in a complete iteration over the array no swap operations were performed, the execution stops Best case – Worst case – Average case –