SlideShare a Scribd company logo
CS261
DATA STRUCTURES & ALGORITHMS
(WEEK-6)
LECTURE-11 & 12
INTRODUCTION TO DATA STRUCTURES &
ALGORITHMS
Lecturer
Azka Aziz
Azka.a@scocs.edu.pk
Data structures & Algorithms
Lecture#11 Link List & Array uses
Course contents
Link List
Uses
Advantages
Disadvantages
Applications
Linked Lists Advantages
 Dynamic data structure
We only need to create a linked list instance and later memory is
allocated dynamically for its nodes
Grow & shrink at run time
When a new node is created, memory is allocated for it
increasing overall size of linked list
When an existing node is deleted, memory is de-allocated
decreasing overall size of linked list
Theoretically, memory allocation has no limitations. It can grow
as much as total memory available in the system
Memory usage is efficient
Memory is allocated only when an node is created (i.e. no pre-
allocation)
Memory is only allocated that is really required by application
Linked Lists Advantages
Stack / Queue implementation
Linked lists can be used to implement data structures like Stack and Queue
Insertion operation is very efficient
Insertion at the start of list can be done in constant time no matter what the length
of list is
Insertion after some node (identified at runtime) can be done in linear time
No shifting of elements is required like arrays while performing insertion operation
over lists
Linked Lists Advantages
Linear Time Search Operation
Search operation can be performed in linear time
Every search operation requires the inspection of N/2 nodes (as an average)
Linked List Memory is not Contigous
List elements do not have to sit side by side in memory like arrays making better use
of memory
Heterogineous Nodes
All list nodes do not need to be of same type
Linked Lists disAdvantages
Extra Memory for Pointers
Every node of simple linked list requires extra memory to store pointer for next
node
In case of double linked list every node needs two pointers (one for next node,
second for previous node) requiring even more memory
No Random Access to List Nodes
Linked list elements can be accesed in linear fashion only
Random access (in arrays) can be done in constant time but in case of linked lists it
can be done in linear time making it a slow process
Linked Lists disAdvantages
Heap Size Limitation
Dynamic memory for linked list nodes is allocated from heap. Hence heap size
practically dictates the size of memory allocated for linked list
If heap does not have more memory left, no memory will be allocated for linked list
nodes
Reverse Traversing is Difficult
Simple linked list elements are connected in one way only making traversal in
backward direction very difficult
In case of double linked list backward traversal is easier but it involves one extra
pointer per node requiring more effort to maintain and more memory for storage
Linked Lists disAdvantages
Courting Linked List Elements
Simple implementation of linked lists require a linear time function to calculate the
number of elements in the list
Linked Lists Applications
Implement other Data Structures
Linked lists can be used to implement other data structures like queue, stack and
graph etc
Using list to implement said data structures is quite straight forward
Implement any List involving Application
Linked lists can be used to implement any list involving application e.g. list of
patients in a hospital (randomly being added to a list), list of planes willing to land
on an airport, list of applicants applying for some resource etc.
Linked Lists Applications
Round Robin Scheduling Algorithm
Any application requiring to accessing certain ordered resources in circular manner
can use circular linked list for its purpose
Implement Multiplayer Games
Any multiplayer board game that requires its players to make moves in some order
again and again (until the game or a round of game is over) can make use of circular
linked list
Order Processing
An order can contain order items presented as a linked list
Linked Lists Applications
Implement Todo List
One can implement a todo list of items as a linked list
Items can be traversed again and again in an order to keep track of which items have
been done and which ones are not
Response to Database Like Queries
Queries to database or database like repositories can be managed (implemented)
using linked list
Neighboring Nodes for Graph
Neighboring list for any graph node can be implemented using linked list
Data structures & Algorithms
Lecture#12 Searching
Lecture Contents
Sequential Search
Binary Search
Iterative
Recursive
Searching
It is often required to look for an element in some collection (typically an array, a
list, a BST etc.)
If the collection has no order associated with it, we have to search for required
element sequentially
Collection elements are matched with required key value one by one
Search exhausts if either required elements is found inside the collection or all
elements have been searched and none of them matched with required key
Sequential Search
proc sequentialSearch(A, key)
n  |A|
for j1 to n do
if A[j] = key then
return j
end if
next
return -1
end proc
Sequential Search … Demonstration (Best Case)
The Algorithm
Line
#
A n j A[j] Key
{12, 53, 18, 33, 48, 97, 6, 10} 12
Note: For sequential search best case is the
one when required key value is found at first
index of the array
Sequential Search … Demonstration (Best Case)
The Algorithm
Line
#
A n j A[j] Key
1 {12, 53, 18, 33, 48, 97, 6, 10} 8 12
1. A = {12, 53, 18, 33, 48, 97, 6, 10}
2. key = 12
3. n is initialized to 8 (length of A)
Sequential Search … Demonstration (Best Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97, 6, 10} 8 1 12
1. A = {12, 53, 18, 33, 48, 97, 6, 10}
2. key = 12
3. n is initialized to 8 (length of A)
4. J is initialized to 1 (loop condition is TRUE)
Sequential Search … Demonstration (Best Case)
The Algorithm
Line
#
A n j A[j] Key
3 {12, 53, 18, 33, 48, 97, 6, 10} 8 1 12 12
1. A = {12, 53, 18, 33, 48, 97, 6, 10}
2. key = 12
3. n is initialized to 8 (length of A)
4. J is initialized to 1 (loop condition is TRUE)
5. key = 12  A[j] is equal to key  if
condition is TRUE
Sequential Search … Demonstration (Best Case)
The Algorithm
Line
#
A n j A[j] Key
4 {12, 53, 18, 33, 48, 97, 6, 10} 8 1 12 12
1. A = {12, 53, 18, 33, 48, 97, 6, 10}
2. key = 12
3. n is initialized to 8 (length of A)
4. J is initialized to 1 (loop condition is TRUE)
5. key = 12
6. 1 is returned as required index value
Sequential Search … Calculating Cost (Best Case)
The Algorithm
A = {12, 53, 18, 33, 48, 97, 6, 10}
Key = 12
T(n) = 1+1+1+1 = 4 = C(constant)
= Ω(C)
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
{12, 53, 18, 33, 48, 97} 25
Note: For sequential search worst case is the
one when required key value is not found in
the array i.e. all values are compared with key
one by one but every comparison returns
FALSE
At the end of execution, -1is returned
indicating the required key value is not
present in array
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
1 {12, 53, 18, 33, 48, 97} 6 25
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6 (length of A)
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97} 6 1 12
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6 (length of A)
4. J = 1
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
3 {12, 53, 18, 33, 48, 97} 6 1 12 25
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6 (length of A)
4. J = 1
5. A[J] = 12
6. A[j] is compared with Key and if condition
returns FALSE as both values are not same i.e.
control transfers to for-loop header again
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97} 6 2 12
1. A = {12, 53, 18, 33, 48, 97, 6, 10}
2. key = 25
3. n = 6
4. J = 2
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
3 {12, 53, 18, 33, 48, 97} 6 2 12 25
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6 (length of A)
4. J = 2
5. A[J] = 53
6. A[j] is compared with Key and if condition
returns FALSE as both values are not same i.e.
control transfers to for-loop header again
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97} 6 3 12
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6
4. J = 3
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
3 {12, 53, 18, 33, 48, 97} 6 3 12 25
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6 (length of A)
4. J = 3
5. A[J] = 18
6. A[j] is compared with Key and returns
FALSE as both values are not same i.e.
control transfers to for-loop header again
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97} 6 4 12
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6
4. J = 4
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
3 {12, 53, 18, 33, 48, 97} 6 4 12 25
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6 (length of A)
4. J = 4
5. A[J] = 33
6. A[j] is compared with Key and returns
FALSE as both values are not same i.e.
control transfers to for-loop header again
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97} 6 5 12
1. A = {12, 53, 18, 33, 48, 97}
2. key = 12
3. n = 6
4. J = 5
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
3 {12, 53, 18, 33, 48, 97} 6 5 12 25
1. A = {12, 53, 18, 33, 48, 97}
2. key = 25
3. n = 6 (length of A)
4. J = 5
5. A[J] = 48
6. A[j] is compared with Key and returns
FALSE as both values are not same i.e.
control transfers to for-loop header again
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97} 6 6 12
1. A = {12, 53, 18, 33, 48, 97}
2. key = 12
3. n = 6
4. J = 6
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
3 {12, 53, 18, 33, 48, 97} 6 6 12 25
1. A = {12, 53, 18, 33, 48, 97}
2. key = 97
3. n = 6 (length of A)
4. J = 6
5. A[J] = 97
6. A[j] is compared with Key and returns
FALSE as both values are not same i.e.
control transfers to for-loop header again
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
2 {12, 53, 18, 33, 48, 97} 6 7 12
1. A = {12, 53, 18, 33, 48, 97}
2. key = 12
3. n = 6
4. J = 7
5. At this point for-loop exhausts shifting
control to line 5
Sequential Search … Demonstration (Worst Case)
The Algorithm
Line
#
A n j A[j] Key
5 {12, 53, 18, 33, 48, 97} 6 7 12
1. A = {12, 53, 18, 33, 48, 97}
2. key = 12
3. n = 6
4. J = 7
5. -1 is returned as response to this procedure
Sequential Search … Demonstration (Worst Case)
T(n) = 1 + n+1 + n + 1 = 2n+3 = O(n)
Sequential Search … Demonstration (AVeRAGE
Case)
Binary Search
If collection (array) is sorted, binary search algorithm can be applied
During every iteration of binary search:
Algorithm keeps track of lower bound and upper bound of elements to be searched
Algorithm compares the middle element (mid point of lower bound and upper
bound) with key value
If key value is equal to middle element’s value, its index is returned as result
If middle element is smaller than key value, binary search algorithm iterates again
for half of the elements (beyond middle element)
Otherwise, binary search algorithm iterates again for half of the elements (before
middle element)
Every iteration of binary search algorithm halves the size of array to be searched
Binary search algorithm is a very strong algorithm offering the worst case
complexity of O(log n); n being the size of array
Binary Search … Iterative Algorithm
proc binarySearch(A, key, low, upper)
while low ≤ upper do
mid  (low+upper)/2
if A[mid] = key then
return mid
else if A[mid] < key then
low mid +1
else
upper  mid -1
end if
next
return -1
end proc
Binary Search … Demo (Best Case)
Iterative Binary Search
Best case is the one in which key value
is found on middle of the array (to be
searched)
Suppose A ={17, 23, 28, 46, 54, 72, 95}
Binary Search … Demo (Best Case)
Iterative Binary Search
Best case is the one in which key value
is found on middle of the array (to be
searched)
A ={17, 23, 28, 46, 54, 72, 95}
low =1
upper =7
key =46
Binary Search … Demo(Best Case)
Iterative Binary Search
Best case is the one in which key value
is found on middle of the array (to be
searched)
A ={17, 23, 28, 46, 54, 72, 95}
low =1
upper =7
key =46
Binary Search … Demo (Best Case)
Iterative Binary Search
Best case is the one in which key value
is found on middle of the array (to be
searched)
A ={17, 23, 28, 46, 54, 72, 95}
low =1
upper =7
key =46
While low ≤ upper is TRUE i.e. control
shifts inside the loop body
Binary Search … Demo(Best Case)
Iterative Binary Search
Best case is the one in which key value
is found on middle of the array (to be
searched)
A ={17, 23, 28, 46, 54, 72, 95}
low =1
upper =7
key =46
mid =(1+7)/2 = 4
If A[4] = key is TRUE i.e. line 4
executes and returns 4 as required
index
Binary Search … Complexity (Best Case)
Iterative Binary Search
T(n) = 1+1+1+1 = 4
= C(constant)
= Ω(C)
Binary Search … Demo (Worst Case)
Iterative Binary Search
Worst case is the one in which key value is
not present in the array (to be searched)
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =1
upper =9
key =44
While Low ≤ upper is TRUE i.e. control shifts
to line 2
Binary Search … Demo (Worst Case)
Iterative Binary Search
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =1
upper =9
key =44
mid = (1+9)/2 = 5
A[5] = 54
Binary Search … Demo (Worst Case)
Iterative Binary Search
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =1
upper =9
key =44
mid = (1+9)/2 = 5
A[5] = 54
If condition at line 3 is FALSE i.e. control
shifts to line 5
Binary Search … Demo (Worst Case)
Iterative Binary Search
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =1
key =44
mid = (1+9)/2 = 5
A[5] = 54
If condition at line 5 is FALSE i.e. control
shifts to line 7 where upper is updated
upper = 4 and we move to next iteration of
while loop
Binary Search … Demo (Worst Case)
Iterative Binary Search
Iteration 2
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =1
upper =4
key =44
While Low ≤ upper is TRUE i.e. control shifts
to line 2
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 2
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =1
upper =4
key =44
mid =(1+4)/2 = 2
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 2
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =1
upper =4
key =44
mid = 2
A[mid] = 23
If condition at line 3 is FALSE i.e. control
shifts to line 5
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
upper =4
key =44
mid = 2
A[mid] = 23
If condition at line 5 is TRUE i.e. control
shifts to line 6 where low is updated i.e.
low = 3 and we move to next iteration of
while loop
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo (Worst Case)
Iterative Binary Search
Iteration 3
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =3
upper =4
key =44
While Low ≤ upper is TRUE i.e. control shifts
to line 2
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 3
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =3
upper =4
key =44
mid =(3+4)/2 = 3
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 3
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =3
upper =4
key =44
mid = 3
A[mid] = 28
If condition at line 3 is FALSE i.e. control
shifts to line 5
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 3
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
upper =4
key =44
mid = 3
A[mid] = 28
If condition at line 5 is TRUE i.e. control
shifts to line 6 where low is updated i.e.
low = 4
we move to next iteration of while loop
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo (Worst Case)
Iterative Binary Search
Iteration 4
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =4
upper =4
key =44
While Low ≤ upper is TRUE i.e. control shifts
to line 2
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 4
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =4
upper =4
key =44
mid =(4+4)/2 = 4
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 4
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =4
upper =4
key =44
mid = 4
A[mid] = 46
If condition at line 3 is FALSE i.e. control
shifts to line 5
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo(Worst Case)
Iterative Binary Search
Iteration 4
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
upper =4
key =44
mid = 4
A[mid] = 46
If condition at line 5 is FALSE i.e. control
shifts to line 7 where upper is updated i.e.
upper = 3
we move to next iteration of while loop
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Demo (Worst Case)
Iterative Binary Search
Iteration 5
A ={17, 23, 28, 46, 54, 72, 95, 112, 125}
low =4
upper =3
key =44
While Low ≤ upper is FALSE i.e. control shifts
to line 8
On line 8, -1 is returned as response of
procedure indicating the required value is
not present in the array
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Complexity(Worst Case)
Iterative Binary Search
1. Array (unsearched array) size is
halved during every failed iteration of
while loop i.e. a maximum of log N
iterations
2. During every iteration when key value
is not found two if-conditions are
FALSE and either lower or upper is
modified. Moreover, mid is also
updated on line 2
3. At end -1 is return on line 8
T(n) = log N (4) + 1 = O(log N)
March 31, 2020 (12:30 - 14:00) University of Lahore
Binary Search … Recursive Algorithm
proc binarySearch(A, key, low, high)
if low > high then
return -1
else
mid  (low + high)/2
if A[mid] = key then
return mid
else if A[mid] < key then
return binarySearch(A, key, mid+1, high)
else
return binarySearch(A, key, low, high-1)
end if
end if
end proc
Binary Search … Recursive Algorithm
proc binarySearch(A, key, low, high)
if low > high then
return -1
else
mid  (low + high)/2
if A[mid] = key then
return mid
else if A[mid] < key then
return binarySearch(A, key, mid+1, high)
else
return binarySearch(A, key, low, high-1)
end if
end if
end proc
1. Demonstrate it yourself
2. Also calculate its complexity

More Related Content

Similar to Data structure 6.pptx

Ch 1 intriductions
Ch 1 intriductionsCh 1 intriductions
Ch 1 intriductions
irshad17
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
mbadhi barnabas
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
Rai University
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
PRABHAHARAN429
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
Rai University
 
Selection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time BoundsSelection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time Bounds
theijes
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining Sort
IRJET Journal
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
Rai University
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
Akash Gaur
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
Ashim Sikder
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 

Similar to Data structure 6.pptx (20)

Ch 1 intriductions
Ch 1 intriductionsCh 1 intriductions
Ch 1 intriductions
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Sorting
SortingSorting
Sorting
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Selection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time BoundsSelection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time Bounds
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining Sort
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 

More from SajalFayyaz

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
SajalFayyaz
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptx
SajalFayyaz
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
SajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
SajalFayyaz
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 

More from SajalFayyaz (9)

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptx
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 

Recently uploaded

DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
Lars Albertsson
 
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
Vietnam Cotton & Spinning Association
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
ihavuls
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Kaxil Naik
 
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理 原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
tzu5xla
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
hyfjgavov
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
xclpvhuk
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
hqfek
 
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
lzdvtmy8
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
VyNguyen709676
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
aguty
 
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
eoxhsaa
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
Alireza Kamrani
 
UofT毕业证如何办理
UofT毕业证如何办理UofT毕业证如何办理
UofT毕业证如何办理
exukyp
 
Building a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdfBuilding a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdf
cjimenez2581
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
nuttdpt
 

Recently uploaded (20)

DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
 
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics March 2024
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
 
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理 原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
原版一比一爱尔兰都柏林大学毕业证(UCD毕业证书)如何办理
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
 
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
 
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
 
UofT毕业证如何办理
UofT毕业证如何办理UofT毕业证如何办理
UofT毕业证如何办理
 
Building a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdfBuilding a Quantum Computer Neutral Atom.pdf
Building a Quantum Computer Neutral Atom.pdf
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
 

Data structure 6.pptx

  • 1. CS261 DATA STRUCTURES & ALGORITHMS (WEEK-6) LECTURE-11 & 12 INTRODUCTION TO DATA STRUCTURES & ALGORITHMS Lecturer Azka Aziz Azka.a@scocs.edu.pk
  • 2. Data structures & Algorithms Lecture#11 Link List & Array uses
  • 4. Linked Lists Advantages  Dynamic data structure We only need to create a linked list instance and later memory is allocated dynamically for its nodes Grow & shrink at run time When a new node is created, memory is allocated for it increasing overall size of linked list When an existing node is deleted, memory is de-allocated decreasing overall size of linked list Theoretically, memory allocation has no limitations. It can grow as much as total memory available in the system Memory usage is efficient Memory is allocated only when an node is created (i.e. no pre- allocation) Memory is only allocated that is really required by application
  • 5. Linked Lists Advantages Stack / Queue implementation Linked lists can be used to implement data structures like Stack and Queue Insertion operation is very efficient Insertion at the start of list can be done in constant time no matter what the length of list is Insertion after some node (identified at runtime) can be done in linear time No shifting of elements is required like arrays while performing insertion operation over lists
  • 6. Linked Lists Advantages Linear Time Search Operation Search operation can be performed in linear time Every search operation requires the inspection of N/2 nodes (as an average) Linked List Memory is not Contigous List elements do not have to sit side by side in memory like arrays making better use of memory Heterogineous Nodes All list nodes do not need to be of same type
  • 7. Linked Lists disAdvantages Extra Memory for Pointers Every node of simple linked list requires extra memory to store pointer for next node In case of double linked list every node needs two pointers (one for next node, second for previous node) requiring even more memory No Random Access to List Nodes Linked list elements can be accesed in linear fashion only Random access (in arrays) can be done in constant time but in case of linked lists it can be done in linear time making it a slow process
  • 8. Linked Lists disAdvantages Heap Size Limitation Dynamic memory for linked list nodes is allocated from heap. Hence heap size practically dictates the size of memory allocated for linked list If heap does not have more memory left, no memory will be allocated for linked list nodes Reverse Traversing is Difficult Simple linked list elements are connected in one way only making traversal in backward direction very difficult In case of double linked list backward traversal is easier but it involves one extra pointer per node requiring more effort to maintain and more memory for storage
  • 9. Linked Lists disAdvantages Courting Linked List Elements Simple implementation of linked lists require a linear time function to calculate the number of elements in the list
  • 10. Linked Lists Applications Implement other Data Structures Linked lists can be used to implement other data structures like queue, stack and graph etc Using list to implement said data structures is quite straight forward Implement any List involving Application Linked lists can be used to implement any list involving application e.g. list of patients in a hospital (randomly being added to a list), list of planes willing to land on an airport, list of applicants applying for some resource etc.
  • 11. Linked Lists Applications Round Robin Scheduling Algorithm Any application requiring to accessing certain ordered resources in circular manner can use circular linked list for its purpose Implement Multiplayer Games Any multiplayer board game that requires its players to make moves in some order again and again (until the game or a round of game is over) can make use of circular linked list Order Processing An order can contain order items presented as a linked list
  • 12. Linked Lists Applications Implement Todo List One can implement a todo list of items as a linked list Items can be traversed again and again in an order to keep track of which items have been done and which ones are not Response to Database Like Queries Queries to database or database like repositories can be managed (implemented) using linked list Neighboring Nodes for Graph Neighboring list for any graph node can be implemented using linked list
  • 13. Data structures & Algorithms Lecture#12 Searching
  • 14. Lecture Contents Sequential Search Binary Search Iterative Recursive
  • 15. Searching It is often required to look for an element in some collection (typically an array, a list, a BST etc.) If the collection has no order associated with it, we have to search for required element sequentially Collection elements are matched with required key value one by one Search exhausts if either required elements is found inside the collection or all elements have been searched and none of them matched with required key
  • 16. Sequential Search proc sequentialSearch(A, key) n  |A| for j1 to n do if A[j] = key then return j end if next return -1 end proc
  • 17. Sequential Search … Demonstration (Best Case) The Algorithm Line # A n j A[j] Key {12, 53, 18, 33, 48, 97, 6, 10} 12 Note: For sequential search best case is the one when required key value is found at first index of the array
  • 18. Sequential Search … Demonstration (Best Case) The Algorithm Line # A n j A[j] Key 1 {12, 53, 18, 33, 48, 97, 6, 10} 8 12 1. A = {12, 53, 18, 33, 48, 97, 6, 10} 2. key = 12 3. n is initialized to 8 (length of A)
  • 19. Sequential Search … Demonstration (Best Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97, 6, 10} 8 1 12 1. A = {12, 53, 18, 33, 48, 97, 6, 10} 2. key = 12 3. n is initialized to 8 (length of A) 4. J is initialized to 1 (loop condition is TRUE)
  • 20. Sequential Search … Demonstration (Best Case) The Algorithm Line # A n j A[j] Key 3 {12, 53, 18, 33, 48, 97, 6, 10} 8 1 12 12 1. A = {12, 53, 18, 33, 48, 97, 6, 10} 2. key = 12 3. n is initialized to 8 (length of A) 4. J is initialized to 1 (loop condition is TRUE) 5. key = 12  A[j] is equal to key  if condition is TRUE
  • 21. Sequential Search … Demonstration (Best Case) The Algorithm Line # A n j A[j] Key 4 {12, 53, 18, 33, 48, 97, 6, 10} 8 1 12 12 1. A = {12, 53, 18, 33, 48, 97, 6, 10} 2. key = 12 3. n is initialized to 8 (length of A) 4. J is initialized to 1 (loop condition is TRUE) 5. key = 12 6. 1 is returned as required index value
  • 22. Sequential Search … Calculating Cost (Best Case) The Algorithm A = {12, 53, 18, 33, 48, 97, 6, 10} Key = 12 T(n) = 1+1+1+1 = 4 = C(constant) = Ω(C)
  • 23. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key {12, 53, 18, 33, 48, 97} 25 Note: For sequential search worst case is the one when required key value is not found in the array i.e. all values are compared with key one by one but every comparison returns FALSE At the end of execution, -1is returned indicating the required key value is not present in array
  • 24. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 1 {12, 53, 18, 33, 48, 97} 6 25 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 (length of A)
  • 25. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97} 6 1 12 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 (length of A) 4. J = 1
  • 26. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 3 {12, 53, 18, 33, 48, 97} 6 1 12 25 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 (length of A) 4. J = 1 5. A[J] = 12 6. A[j] is compared with Key and if condition returns FALSE as both values are not same i.e. control transfers to for-loop header again
  • 27. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97} 6 2 12 1. A = {12, 53, 18, 33, 48, 97, 6, 10} 2. key = 25 3. n = 6 4. J = 2
  • 28. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 3 {12, 53, 18, 33, 48, 97} 6 2 12 25 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 (length of A) 4. J = 2 5. A[J] = 53 6. A[j] is compared with Key and if condition returns FALSE as both values are not same i.e. control transfers to for-loop header again
  • 29. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97} 6 3 12 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 4. J = 3
  • 30. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 3 {12, 53, 18, 33, 48, 97} 6 3 12 25 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 (length of A) 4. J = 3 5. A[J] = 18 6. A[j] is compared with Key and returns FALSE as both values are not same i.e. control transfers to for-loop header again
  • 31. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97} 6 4 12 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 4. J = 4
  • 32. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 3 {12, 53, 18, 33, 48, 97} 6 4 12 25 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 (length of A) 4. J = 4 5. A[J] = 33 6. A[j] is compared with Key and returns FALSE as both values are not same i.e. control transfers to for-loop header again
  • 33. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97} 6 5 12 1. A = {12, 53, 18, 33, 48, 97} 2. key = 12 3. n = 6 4. J = 5
  • 34. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 3 {12, 53, 18, 33, 48, 97} 6 5 12 25 1. A = {12, 53, 18, 33, 48, 97} 2. key = 25 3. n = 6 (length of A) 4. J = 5 5. A[J] = 48 6. A[j] is compared with Key and returns FALSE as both values are not same i.e. control transfers to for-loop header again
  • 35. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97} 6 6 12 1. A = {12, 53, 18, 33, 48, 97} 2. key = 12 3. n = 6 4. J = 6
  • 36. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 3 {12, 53, 18, 33, 48, 97} 6 6 12 25 1. A = {12, 53, 18, 33, 48, 97} 2. key = 97 3. n = 6 (length of A) 4. J = 6 5. A[J] = 97 6. A[j] is compared with Key and returns FALSE as both values are not same i.e. control transfers to for-loop header again
  • 37. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 2 {12, 53, 18, 33, 48, 97} 6 7 12 1. A = {12, 53, 18, 33, 48, 97} 2. key = 12 3. n = 6 4. J = 7 5. At this point for-loop exhausts shifting control to line 5
  • 38. Sequential Search … Demonstration (Worst Case) The Algorithm Line # A n j A[j] Key 5 {12, 53, 18, 33, 48, 97} 6 7 12 1. A = {12, 53, 18, 33, 48, 97} 2. key = 12 3. n = 6 4. J = 7 5. -1 is returned as response to this procedure
  • 39. Sequential Search … Demonstration (Worst Case) T(n) = 1 + n+1 + n + 1 = 2n+3 = O(n)
  • 40. Sequential Search … Demonstration (AVeRAGE Case)
  • 41. Binary Search If collection (array) is sorted, binary search algorithm can be applied During every iteration of binary search: Algorithm keeps track of lower bound and upper bound of elements to be searched Algorithm compares the middle element (mid point of lower bound and upper bound) with key value If key value is equal to middle element’s value, its index is returned as result If middle element is smaller than key value, binary search algorithm iterates again for half of the elements (beyond middle element) Otherwise, binary search algorithm iterates again for half of the elements (before middle element) Every iteration of binary search algorithm halves the size of array to be searched Binary search algorithm is a very strong algorithm offering the worst case complexity of O(log n); n being the size of array
  • 42. Binary Search … Iterative Algorithm proc binarySearch(A, key, low, upper) while low ≤ upper do mid  (low+upper)/2 if A[mid] = key then return mid else if A[mid] < key then low mid +1 else upper  mid -1 end if next return -1 end proc
  • 43. Binary Search … Demo (Best Case) Iterative Binary Search Best case is the one in which key value is found on middle of the array (to be searched) Suppose A ={17, 23, 28, 46, 54, 72, 95}
  • 44. Binary Search … Demo (Best Case) Iterative Binary Search Best case is the one in which key value is found on middle of the array (to be searched) A ={17, 23, 28, 46, 54, 72, 95} low =1 upper =7 key =46
  • 45. Binary Search … Demo(Best Case) Iterative Binary Search Best case is the one in which key value is found on middle of the array (to be searched) A ={17, 23, 28, 46, 54, 72, 95} low =1 upper =7 key =46
  • 46. Binary Search … Demo (Best Case) Iterative Binary Search Best case is the one in which key value is found on middle of the array (to be searched) A ={17, 23, 28, 46, 54, 72, 95} low =1 upper =7 key =46 While low ≤ upper is TRUE i.e. control shifts inside the loop body
  • 47. Binary Search … Demo(Best Case) Iterative Binary Search Best case is the one in which key value is found on middle of the array (to be searched) A ={17, 23, 28, 46, 54, 72, 95} low =1 upper =7 key =46 mid =(1+7)/2 = 4 If A[4] = key is TRUE i.e. line 4 executes and returns 4 as required index
  • 48. Binary Search … Complexity (Best Case) Iterative Binary Search T(n) = 1+1+1+1 = 4 = C(constant) = Ω(C)
  • 49. Binary Search … Demo (Worst Case) Iterative Binary Search Worst case is the one in which key value is not present in the array (to be searched) A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =1 upper =9 key =44 While Low ≤ upper is TRUE i.e. control shifts to line 2
  • 50. Binary Search … Demo (Worst Case) Iterative Binary Search A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =1 upper =9 key =44 mid = (1+9)/2 = 5 A[5] = 54
  • 51. Binary Search … Demo (Worst Case) Iterative Binary Search A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =1 upper =9 key =44 mid = (1+9)/2 = 5 A[5] = 54 If condition at line 3 is FALSE i.e. control shifts to line 5
  • 52. Binary Search … Demo (Worst Case) Iterative Binary Search A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =1 key =44 mid = (1+9)/2 = 5 A[5] = 54 If condition at line 5 is FALSE i.e. control shifts to line 7 where upper is updated upper = 4 and we move to next iteration of while loop
  • 53. Binary Search … Demo (Worst Case) Iterative Binary Search Iteration 2 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =1 upper =4 key =44 While Low ≤ upper is TRUE i.e. control shifts to line 2
  • 54. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 2 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =1 upper =4 key =44 mid =(1+4)/2 = 2 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 55. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 2 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =1 upper =4 key =44 mid = 2 A[mid] = 23 If condition at line 3 is FALSE i.e. control shifts to line 5 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 56. Binary Search … Demo(Worst Case) Iterative Binary Search A ={17, 23, 28, 46, 54, 72, 95, 112, 125} upper =4 key =44 mid = 2 A[mid] = 23 If condition at line 5 is TRUE i.e. control shifts to line 6 where low is updated i.e. low = 3 and we move to next iteration of while loop March 31, 2020 (12:30 - 14:00) University of Lahore
  • 57. Binary Search … Demo (Worst Case) Iterative Binary Search Iteration 3 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =3 upper =4 key =44 While Low ≤ upper is TRUE i.e. control shifts to line 2 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 58. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 3 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =3 upper =4 key =44 mid =(3+4)/2 = 3 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 59. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 3 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =3 upper =4 key =44 mid = 3 A[mid] = 28 If condition at line 3 is FALSE i.e. control shifts to line 5 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 60. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 3 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} upper =4 key =44 mid = 3 A[mid] = 28 If condition at line 5 is TRUE i.e. control shifts to line 6 where low is updated i.e. low = 4 we move to next iteration of while loop March 31, 2020 (12:30 - 14:00) University of Lahore
  • 61. Binary Search … Demo (Worst Case) Iterative Binary Search Iteration 4 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =4 upper =4 key =44 While Low ≤ upper is TRUE i.e. control shifts to line 2 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 62. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 4 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =4 upper =4 key =44 mid =(4+4)/2 = 4 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 63. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 4 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =4 upper =4 key =44 mid = 4 A[mid] = 46 If condition at line 3 is FALSE i.e. control shifts to line 5 March 31, 2020 (12:30 - 14:00) University of Lahore
  • 64. Binary Search … Demo(Worst Case) Iterative Binary Search Iteration 4 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} upper =4 key =44 mid = 4 A[mid] = 46 If condition at line 5 is FALSE i.e. control shifts to line 7 where upper is updated i.e. upper = 3 we move to next iteration of while loop March 31, 2020 (12:30 - 14:00) University of Lahore
  • 65. Binary Search … Demo (Worst Case) Iterative Binary Search Iteration 5 A ={17, 23, 28, 46, 54, 72, 95, 112, 125} low =4 upper =3 key =44 While Low ≤ upper is FALSE i.e. control shifts to line 8 On line 8, -1 is returned as response of procedure indicating the required value is not present in the array March 31, 2020 (12:30 - 14:00) University of Lahore
  • 66. Binary Search … Complexity(Worst Case) Iterative Binary Search 1. Array (unsearched array) size is halved during every failed iteration of while loop i.e. a maximum of log N iterations 2. During every iteration when key value is not found two if-conditions are FALSE and either lower or upper is modified. Moreover, mid is also updated on line 2 3. At end -1 is return on line 8 T(n) = log N (4) + 1 = O(log N) March 31, 2020 (12:30 - 14:00) University of Lahore
  • 67. Binary Search … Recursive Algorithm proc binarySearch(A, key, low, high) if low > high then return -1 else mid  (low + high)/2 if A[mid] = key then return mid else if A[mid] < key then return binarySearch(A, key, mid+1, high) else return binarySearch(A, key, low, high-1) end if end if end proc
  • 68. Binary Search … Recursive Algorithm proc binarySearch(A, key, low, high) if low > high then return -1 else mid  (low + high)/2 if A[mid] = key then return mid else if A[mid] < key then return binarySearch(A, key, mid+1, high) else return binarySearch(A, key, low, high-1) end if end if end proc 1. Demonstrate it yourself 2. Also calculate its complexity