Data Structures & Algorithms: Week1
Data Structures & Algorithms: Week1
Data Structures & Algorithms: Week1
Week1
CHAPTER 1: INTRODUTION
(1) the logical arrangement of data elements, combined with (2) the set of operations we need to access the elements.
Atomic Variables
Example:library
is composed of elements (books) Accessing a particular book requires knowledge of the arrangement of the books Users access books only through the librarian
the logical arrangement of data elements, combined with the set of operations we need to access the elements.
Structures include
What is Algorithm?
Algorithm:
Sumary
1. FUNCTION 2. THE CONCEPT OF STACK 3. THE SEQUENCE OF EXECUTION DURING A FUNCTION CALL 4. PARAMETER PASSING 5. CALL BY REFERENCE 6. RESOLVING VARIABLE REFERENCES 7. RECURSION 8. STACK OVERHEADS IN RECURSION 9. WRITING A RECURSIVE FUNCTION 10. TYPES OF RECURSION
1. FUNCTION
provide modularity to the software divide complex tasks into small manageable tasks avoid duplication of work
A stack is memory in which values are stored and retrieved in "last in first out" manner by using operations called push and pop.
When the function is called, the current execution is temporarily stopped and the control goes to the called function. After the call, the execution resumes from the point at which the execution is stopped. To get the exact point at which execution is resumed, the address of the next instruction is stored in the stack. When the function call completes, the address at the top of the stack is taken.
Functions or sub-programs are implemented using a stack. When a function is called, the address of the next instruction is pushed into the stack. When the function is finished, the address for execution is taken by using the pop operation.
passing by value
the value before and after the call remains the same changed value after the function completes
passing by reference
7. RECURSION
A method of programming whereby a function directly or indirectly calls itself Problems: stop recursion?
7. RECURSION
7. RECURSION
two important results: the depth of recursion and the stack overheads in recursion
Recursion enables us to write a program in a natural way. The speed of a recursive program is slower because of stack overheads. In a recursive program you have to specify recursive conditions, terminating conditions, and recursive expressions.
LINEAR RECURSION TAIL RECURSION BINARY RECURSION EXPONENTIAL RECURSION NESTED RECURSION MUTUAL RECURSION
LINEAR RECURSION
only makes a single call to itself each time the function runs
TAIL RECURSION
Tail recursion is a form of linear recursion. In tail recursion, the recursive call is the last thing the function does. Often, the value of the recursive call is returned.
BINARY RECURSION
Some recursive functions don't just have one call to themself, they have two (or more).
EXPONENTIAL RECURSION
An exponential recursive function is one that, if you were to draw out a representation of all the function calls, would have an exponential number of calls in relation to the size of the data set (exponential meaning if there were n elements, there would be O(an) function calls where a is a positive number)
EXPONENTIAL RECURSION
NESTED RECURSION
In nested recursion, one of the arguments to the recursive function is the recursive function itself These functions tend to grow extremely fast.
MUTUAL RECURSION
A recursive function doesn't necessarily need to call itself. Some recursive functions work in pairs or even larger groups. For example, function A calls function B which calls function C which in turn calls function A.
MUTUAL RECURSION
Exercises : Recursion
Write a program to print a revert number Example: input n=12345. Print out: 54321.
Write a recursion function to find the sum of every number in a int number. Example: n=1980 => Sum=1+9+8+0=18.
S=a[0]+a[1]+a[n-1]
Print triangle
c d