DS Week 3 Lecture Structure Pointer DMA Recursion ToH by DR Gaurav
DS Week 3 Lecture Structure Pointer DMA Recursion ToH by DR Gaurav
Week 3 Lecture
by
Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Last Week’s Learnings
• Asymptotic Notations
1. What is an algorithm?
c) A programming language
algorithm?
a) Finite steps
b) Infinite loops
c) Well-defined inputs
int a = 5;
a) 5 6
b) 5 7
c) 6 7
d) 6 6
Answer: b) 5 7
Quick Recap of Visualization of Searching Algorithms
01 Linear Search
02 Binary Search
Visualization of Linear Search
8 3 1 2 4 5 6 7
Algorithm of linear search
Key Element = 4 Step 1- Start from the leftmost element of arr[] and one
Key Element = 7
Step 3- If x doesn’t match with any of the elements,
Total Number of Comparison = 8 return -1.
Visualization of Linear Search
8 3 1 2 4 5 6 7
Key Element = 8
Total Number of Comparison = 1
f(n) = 1 = Constant = O(1)
8 3 1 2 4 5 6 7
Key Element = 7
Total Number of Comparison = 7
8 3 1 2 4 5 6 7
Key Element = 8 or 3 or 1 or 2 or 4 or 5…
Total Avg Time = All possible case time divided by number of cases
Average Case Time for n elements Average Time= (1+2+3+...n)/n = n(n+1)/2n = n+1/2
Complexity Avg Case Time Complexity
f(n) = O(n)
Quick Recap of Visualization of Searching Algorithms
2. Binary Search
• Binary search is a fast way to find an item in a sorted list.
• It follows the divide and conquer approach in which the list is divided into
two halves, and the item is compared with the middle element of the list.
• If the match is found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending upon the result
produced through the match.
Visualization of Searching Algorithms
2. Binary Search
Visualization of Binary Searching Algorithms
1. Divide the search space into two halves by finding the middle index
“mid”.
2. Compare the middle element of the search space with the key.
4. If the key is not found at middle element, choose which half will be used as the next search space.
1. If the key is smaller than the middle element, then the left side is used for next search.
2. If the key is larger than the middle element, then the right side is used for next search.
5. This process is continued until the key is found or the total search space is exhausted.
Time Complexity of Binary Searching Algorithms
2. Compare the middle element of the search space with the key. {
5. This process is continued until the key is found or the total search space is low = mid + 1;
}
exhausted. }
return -1;
}
Visualization of Time Complexity of Binary Searching Algorithm
2 1 ---------→ 21
4 2 1 ---------→ 22
8 4 2 1 ---------→ 23
16 8 4 2 1 ---------→ 24
32 16 8 4 2 1 ---------→ 25
64 32 16 8 4 2 1 ---------→ 26
128 64 32 16 8 4 2 1 ---------→ 27
. .
.
. .
.
.
.
.
.
N …………………. ---------→ 2k
char title[50];
book1
char author[50]; char title[50]; | (50 bytes)
Book1.book_id = 6495407;
int *ip;
double *dp;
float *fp;
char *ch ;
Pointer
A pointer stores the direct address of the variable, or it points to the memory
location of variable.
cout<<arr;
ptr
(arr indicated the base address of array)
Pointer Arithmetic
ptr
b = temp; cout << "x = " << x << ", y = " << y;
} b } x y
a
Output? ? ?
10 5
Applications of Pointer
❖ Pointers allow functions to modify variables outside their scope.
b = temp; cout << "x = " << x << ", y = " << y;
} b } x y
a
Output: 5 10
10 5
Note: When Swap function is called, it creates a local copy of variables, and scope of the variable is till function alive.
the actual value of variables in the main function will remain unchanged.
Applications of Pointer
❖ Pointers allow functions to modify variables outside their scope.
*b = temp; 6000 cout << "x = " << x << ", y = " << y;
} b } x y
a
Output: 10 5
2000 4000
8000 12000
Pointers to Structures
struct Books { struct Books *struct_pointer;
char title[50];
char author[50]; struct_pointer = &book1;
char subject[100];
int book_id;
}book1, book2;
To access the members of a structure using a pointer to that structure, you must use
the → operator as follows
struct_pointer->title;
In Normal Structure
cout<< "Book title :“ << struct_pointer->title; book.title
Extended Pointer (Pointer to Pointer or Double Pointers)
int a, *b, **c; a
a=10;
10 b
b=&a;
c=&b; 2000 2000
c
cout<<"The value of a is : “<< a; 4000
4000
cout<<"The value of b is : “<< *b ;
7000
cout<<"The value of c is : “<< **c;
Correct Answer is B
Assessment Time
int a, *b, **c; a
a=10;
*b **c 30
10
b=&a; b
c=&b; 2000
**c=a + 2*(*b); 2000
cout<<"The value of a is : “<< a;
c
4000
Output: 4000
(A) 10
(B) 20 7000
(C) 30
(D) Error
Correct Answer is C
Returning a Pointer (Assessment Time)
Types
• Allows the creation of data structures like linked lists, trees, etc., which
• new Operator: Allocates memory on the heap and returns a pointer to it.
Self Study
Recursion
T (n) = T(n-1) + n
Unstructured
Recursion
fact(int n)
{
if (n < = 1) // base case or stopping condition
return 1;
else
return n*fact(n-1);
}
Unstructured
Visualization of Recursion
Example - Sum of first n natural numbers
A recurrence relation is an equation or inequality that describes a function in terms of its values on
smaller inputs.
Used to reduce complicated problems to an iterative process based on simpler versions of the
problem
Unstructured
T(n) term is used to define the time complexity in recurrence relation analysis.
Writing Recurrence Relations
Example- Analyze the time complexity of this algorithm
T(n)= T(n-1) + 1
Writing Recurrence Relations
Example - Sum of first n natural numbers
else
return n + sum(n-1); n + T(n-1)
}
Unstructured
Solving Recurrence Relations
Master Method
Substitution Method
Note:
• Please self read these topics.
• Detail of these topic will be covered in the course Design and analysis of Algorithms (Next Semester)
Self Practice Problem using Recursion
• Bubble
• Selection
• Insertion Sort
• Merge Sort
Any Queries?
Office MCub311
Discussion Time: 3-5 PM
Mob: +91-8586968801