Chapter-1 Introduction and Searching
Chapter-1 Introduction and Searching
Algorithms
Definition: Data Structures
• Data structure is representation of the logical
relationship existing between individual elements of
data.
• In other words, a data structure is a way of
organizing all data items that considers not only the
elements stored but also their relationship to each
other
Definition: Algorithms
• In mathematics and computer science, an algorithm
is a set of instructions, typically to solve a class of
problems or perform a computation.
Float
Double
Character
boolean
Non Primitive data types
These Datastructures do not allow any specific instructions to be
performed on the Data items directly.
The different non primitive data types are
Arrays
Structures
Unions
Class etc.
Data structure
When working with certain data structures you need to know how
to insert new data, search for a specified item, and deleting a
specific item.
Commonly used algorithms include are useful for:
Searching for a particular data item (or record).
Sorting the data. There are many ways to sort data. Simple sorting,
Advanced sorting
Iterating through all the items in a data structure. (Visiting each item in turn
so as to display it or perform some other action on these items)
Classification
Stacks
Queue
Lists
Non-Linear Data structures
This Data structures involve representingthe
elements in Hierarchical order.
Eg:
Trees
Graphs
Data structure operations
Searching
Inserting
Deleting
Sorting
operations
Traversing
Searching
To search for a particular value in the data structure for the given
key value.
Inserting
Sorting
Merging
}
Intuitively: Set of all functions that
have the same rate of growth as g(n).
g(n) is an asymptotically tight bound for f(n).
-notation
For function g(n), we define (g(n)), big-
Theta of n, as the set:
(g(n)) = {f(n) :
positive constants c1, c2, and n0, such
that n n ,
0
}
Technically, f(n) (g(n)).
Older usage, f(n) = (g(n)).
I’ll accept either…
O(Nlog N)
Number of Inputs
Linear Search
Linear Search: A Simple Search
• A search traverses the collection until
– The desired element is found
– Or the collection is exhausted
• As (loga (a) = 1)
Therefore,=> k = log2 (n)
• Hence the time complexzity of Binary Search is
• log2 (n)
Question - MCQ
• Given an input arr = {2,5,7,99,899}; key = 899; What
is the level of recursion?
a) 5
b) 2
c) 3
d) 4
Answer
• Answer: c
Explanation:
level 1: mid = 7
level 2: mid = 99
level 3: mid = 899(this is the key)
Question - MCQ
• Given an array arr = {45,77,89,90,94,99,100} and key
= 99; what are the mid values(corresponding array
elements) in the first and second levels of recursion?
a) 90 and 99
b) 90 and 94
c) 89 and 99
d) 89 and 94
Answer
• Answer: a
Explanation: Trace the input with the binary search
recursive code.
k You …
Th a n