Data Structure - Chap 1
Data Structure - Chap 1
Mr Zahi Al Chami
Chap 1: Algorithms, complexity and recursion
Overview
The learning outcomes are:
Algorithms, complexity and recursion
Sorting algorithms
Pointers and structures
Singly Linked List
Doubly Linked List
Stack
Queue
Binary Tree
Project ( 30% )
int DateOfBirth=2020-age;
return DateOfBirth;
}
Introduction
To write a program, we process as follows:
Obtain a description of the problem
Analyze the problem
Develop a pseudo-code
Write the pseudo-code in a programming language.
For example:
f(n)= n2 and g(n)= 3n2 + 5n + 4 -> these two functions have the
same order
Time Complexity
Let us consider an algorithm ( f(n) ) which reads an array and
test each cell:
if the array has 100 cells, the algorithm will perform 100 tests
if the array has 5000 cells, the algorithm will perform 5000 tests.
If the array has n cells, the algorithm will perform n tests-> We say that
its complexity is in O(n)
Write the Fibonacci series using the iterative function. What is its
complexity?
Recursion with arrays
Recursion: Dichotomic search
A search algorithm that operates by selecting between two
distinct alternatives (dichotomies) at each step.
If the value of the search key is less than the item in the
middle of the interval, narrow the interval to the lower
half. Otherwise, narrow it to the upper half.
MaxLeft=7 MaxRight=8
O(1): Constant
O(logn): Logarithmic
O(n): Linear
O(n*logn): Linearithmic
O(n2): Quadratic
O(n3): Cubic
O(2n): Exponential
O(n!): Factorial
Time Complexity: O(1)
Time complexity of a function is considered as O(1)
if it doesn’t contain loop, recursion and call to any
other non-constant time function
For instance, let’s say that we want to look for a person in an old
phone book. It has every name sorted alphabetically. There are at
least two ways to do it:
Algorithm A
Start at the beginning of the book and go in order until you find the contact you
are looking for. Run-time O(n)
Algorithm B
Open the book in the middle and check the first name on it.
If the name that you are looking for is alphabetically bigger, then look to the right.
Otherwise, look in the left half
Time Complexity: Example O(Logn)
The algorithm Binary Search has a complexity of Logn.
For example:
Get the max/min value in an array
Find a given element in a collection
Time Complexity: Example O(n)
So, this gets us 3(n) + 2 -> By leaving the most significant term, we
get n. And finally using the big O notation we get O(n)
Time Complexity: O(n*logn)
Linearithmic time complexity is slightly slower than a
linear algorithm but still much better than a quadratic
algorithm.
You can select no topping (you are on a diet ;), you can
choose one topping or a combination of two or a
combination of three or all of them.
Time Complexity: Example O(2n)
return a+b;
}
In this particular method, three variables are used and allocated in memory:
The first int argument, a
The second int argument, b
The returned sum result which is also an int
In C++, a single integer variable occupies four bytes of memory. In this example, we
have three integer variables.
We can clearly see that the space complexity is constant. So, it can be expressed in big-O
notation as O(1)
Space Complexity: Example
int sumArray (int array[ ], int size){
Algorithm 2:
int sum=0;
return sum;
}
Let’s list all variables present in the above code:
array – the space taken by the array is equal to 4*n bytes where n is the length of the array
size – a 4 byte integer
sum – a 4 byte integer
i – a 4 byte integer
The total space needed is: 4*n+4+4+4. The highest order is n. Thus the
space complexity is O(n).