0% found this document useful (0 votes)
11 views117 pages

Unit 1 Updated

This document provides an overview of the course on data structures and algorithms. The course aims to teach fundamental concepts like data organization, data structures and their operations, algorithms, complexity analysis and searching techniques. It covers both linear data structures like stacks, queues and linked lists as well as non-linear structures like trees. The course is intended to help students choose appropriate data structures for problems and develop efficient algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views117 pages

Unit 1 Updated

This document provides an overview of the course on data structures and algorithms. The course aims to teach fundamental concepts like data organization, data structures and their operations, algorithms, complexity analysis and searching techniques. It covers both linear data structures like stacks, queues and linked lists as well as non-linear structures like trees. The course is intended to help students choose appropriate data structures for problems and develop efficient algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 117

Data structures and Algorithms UNIT 1

© Kalasalingam academy of research and education


Course Outline
CO 1 Understand the role of algorithms and
programming constructs as a systematic and
efficient way of solving problems & searching
techniques. This course provides a fairly good concept of the
fundamentals of data structures and algorithms to
the students. The main objective of this course is
CO 2 Create Abstract Data Types for linear data
structures and implement the same. to teach the basic skills and knowledge to develop
efficient algorithms to solve computational
problems and to make informed choices between
CO 3. Design and implement non-linear data different solutions for the same problem. After
structures such as tree.
completion of this course, students will be able to
choose an appropriate data structure for a
CO 4. Interpret and analyze efficiency of various particular problem.
sorting techniques and hashing.

CO 5. Design data structures and develop code for


real life problems using graphs.

© Kalasalingam academy of research and education 2


Unit 1
Basic Terminologies: Elementary Data
Organizations, Data Structure and its Operations;
Algorithm, Asymptotic Notations, Time, Space
Complexity, Recursion. Searching: Linear Search
and Binary Search Techniques, and their complexity

© Kalasalingam academy of research and education 3


Unit 1 Outline

Lesson 1. Basic Terminologies: Elementary Data Organizations

Lesson 2. Data Structure and its Operations

Lesson 3. Algorithm, Asymptotic Notations, Time, Space Complexity

Lesson 4. Recursion

Lesson 5. Searching: Linear Search and Binary Search Techniques, and their complexity

© Kalasalingam academy of research and education 4


Lesson 1. Basic Terminologies: Elementary Data Organizations

© Kalasalingam academy of research and education 5


Data, Entity and Information
 Data represents a single value or a set of values assigned to
entities. Data item refers a single or group of values with in the
data
• An entity is a thing that has some properties which can take
values.
• Processed or meaning full data is called information. This is used
for taking some action.

© Kalasalingam academy of research and education 6


Lesson 2. Data Structure and its Operations

© Kalasalingam academy of research and education 7


Data Structure
• A data structure is a specialized format for organizing,
processing, retrieving and storing data.
• While there are several basic and advanced structure types,
any data structure is designed to arrange data to suit a specific
purpose so that it can be accessed and worked with in
appropriate ways.

© Kalasalingam academy of research and education 8


Data Structure
• In computer programming, a data structure may be
selected or designed to store data for the purpose of
working on it with various algorithms.
• Each data structure contains information about the
data values, relationships between the data and
functions that can be applied to the data.

© Kalasalingam academy of research and education 9


Data Structure
• The data structure is basically a technique of organizing
and storing of different types of data items in computer
memory.

• It is considered as not only the storing of data elements but


also the maintaining of the logical relationship existing
between individual data elements.

• The Data structure can also be defined as a mathematical or


logical model, which relates to a particular organization of
different data elements.
© Kalasalingam academy of research and education 10
Classification of Data Structure
• Data structure are normally divided into two broad
categories:

• Primitive Data Structure


• Non-Primitive Data Structure

© Kalasalingam academy of research and education 11


Classification of Data Structure

© Kalasalingam academy of research and education 12


Primitive Data Structure
• There are basic structures and directly operated upon by
the machine instructions.
• Data structures that are directly operated upon the
machine-level instructions are known as primitive data
structures.
• Integer, Floating-point number, Character constants, string
constants, pointers etc, fall in this category.

© Kalasalingam academy of research and education 13


Primitive Data Structure
• The most commonly used operation on data structure
are broadly categorized into following types:
◦ Create
◦ Selection
◦ Updating
◦ Destroy or Delete

© Kalasalingam academy of research and education 14


Non-Primitive Data Structure
• There are more sophisticated data structures.
• The Data structures that are derived from the primitive
data structures are called Non-primitive data structure.
• The non-primitive data structures emphasize on structuring
of a group of homogeneous (same type) or heterogeneous
(different type) data items.

© Kalasalingam academy of research and education 15


Non-Primitive Data Structure
Linear Data structures:
◦ Linear Data structures are kind of data structure that has
homogeneous elements.
◦ The data structure in which elements are in a sequence and
form a liner series.
◦ Linear data structures are very easy to implement, since the
memory of the computer is also organized in a linear fashion.
◦ Some commonly used linear data structures are Stack, Queue
and Linked Lists.

© Kalasalingam academy of research and education 16


Non-Primitive Data Structure
Non-Linear Data structures:
◦ A Non-Linear Data structures is a data structure in which
data item is connected to several other data items.
◦ Non-Linear data structure may exhibit either a hierarchical
relationship or parent child relationship.
◦ The data elements are not arranged in a sequential structure.
◦ The different non-linear data structures are trees and graphs.

© Kalasalingam academy of research and education 17


Non-Primitive Data Structure
⚫ The most commonly used operation on data structure are
broadly categorized into following types:
◦ Traversal
◦ Insertion
◦ Selection
◦ Searching
◦ Sorting
◦ Merging
◦ Destroy or Delete
© Kalasalingam academy of research and education 18
• A primitive data structure is generally a basic structure
that is usually built into the language, such as an integer, a
float.

• A non-primitive data structure is built out of primitive


data structures linked together in meaningful ways, such
as a or a linked-list, binary search tree, AVLTree, graph
etc.

© Kalasalingam academy of research and education 19


Arrays
⚫ An array is defined as a set of finite number
of homogeneous elements or same data
items.
⚫ It means an array can contain one type of data
only, either all integer, all float-point number or
all character.

© Kalasalingam academy of research and education 20


One dimensional array:
⚫ An array with only one row or column is called one-dimensional
array.
⚫ It is finite collection of n number of elements of same type such
that:
◦ can be referred by indexing.
◦ The syntax Elements are stored in continuous locations.
◦ Elements x to define one-dimensional array is:

© Kalasalingam academy of research and education


One dimensional array:
⚫ Syntax: Datatype Array_Name [Size];
⚫ Where,
Datatype : Type of value it can store (Example: int, char, float)
Array_Name: To identify the array.
⚫ Size : The maximum number of elements that the array can hold.

© Kalasalingam academy of research and education 22


Arrays
• Simply, declaration of array is as follows: int arr[10]
• Where int specifies the data type or type of elements
arrays stores.
• “arr” is the name of array & the number specified inside
the square brackets is the number of elements an array
can store, this is also called sized or length of array.

© Kalasalingam academy of research and education 23


Represent a Linear Array in memory
⚫ The elements of linear array are stored in consecutive memory
locations. It is shown below:

© Kalasalingam academy of research and education 24


Arrays
◦ The elements of array will always be stored in the consecutive
(continues) memory location.
◦ The number of elements that can be stored in an array, that is
the size of array or its length is given by the following equation:
(Upperbound-lowerbound)+1
◦ For the above array it would be (9-0)+1=10,where 0 is the lower
bound of array and 9 is the upper bound of array.

© Kalasalingam academy of research and education 25


Array
◦ Array can always be read or written through loop.
for(i=0;i<=9;i++)
{
scanf(“%d”,&arr[i]);
printf(“%d”,arr[i]);
}

© Kalasalingam academy of research and education 26


Arrays types
⚫ Single Dimension Array
◦ Array with one subscript
⚫ Two Dimension Array
◦ Array with two subscripts (Rows and
Column)
⚫ Multi Dimension Array
◦ Array with Multiple subscripts

© Kalasalingam academy of research and education 27


Two dimensional array
⚫A two dimensional array is a collection of elements and
each element is identified by a pair of subscripts. A[3] [3]
⚫ The elements are stored in continuous memory locations.
⚫ The elements of two-dimensional array as rows and
columns.

© Kalasalingam academy of research and education 28


Two dimensional array
⚫ The number of rows and columns in a matrix is called as
the order of the matrix and denoted as mxn.
⚫ The number of elements can be obtained by multiplying
number of rows and number of columns.
A[0] A[1] A[2]

A[0] 10 20 30

A[1] 40 50 60

A[2] 70 80 90

© Kalasalingam academy of research and education 29


Advantages of Array:
⚫ It is used to represent multiple data items of same
type by using single name.
⚫ It can be used to implement other data structures
like linked lists, stacks, queues, tree, graphs etc.
⚫ Two-dimensional arrays are used to represent
matrices.
⚫ Many databases include one-dimensional arrays
whose elements are records.

© Kalasalingam academy of research and education 30


Disadvantages of Array
⚫ We must know in advance the how many elements are to
be stored in array.
⚫ Array is static structure. It means that array is of fixed size. The
memory which is allocated to array cannot be increased or
decreased.
⚫ Array is fixed size; if we allocate more memory than
requirement then the memory space will be wasted.
⚫ The elements of array are stored in consecutive memory
locations. So insertion and deletion are very difficult and time
consuming.
© Kalasalingam academy of research and education 31
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.
 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)

© Kalasalingam academy of research and education 32


Data structure operations
• Operation means processing the data in the data structure. The
following are some important operations.
• Traversing
• Searching
• Inserting
• Deleting
• Sorting
• Merging
© Kalasalingam academy of research and education 33
Traversing

• It means to access each data item exactly once so that it can be


processed.
• Example-to print the names of all the students in a class

© Kalasalingam academy of research and education 34


Searching

• Finding the location of a particular data item with given value


or finding the location of all the records which satisfies one or
more conditions
• Example-to find the names of all the students who secured 100
marks in data structures

© Kalasalingam academy of research and education 35


Insertion

• It is used to add new data items to the given list of data


items.

• Example-To add the details of a new students,who has


recently joined

© Kalasalingam academy of research and education 36


Deletion

• It means to remove or delete a particular data from the given collection


of data items

• Example –to delete name of a student who has left the course

© Kalasalingam academy of research and education 37


Sorting

• Data items can be arranged in some order like ascending order


or descending order depending on type of application
• Arranging the names of students in a class in alphabetic order

© Kalasalingam academy of research and education 38


Merging

• Combining the list of two files of sorted data items into a single
sorted file

© Kalasalingam academy of research and education 39


Some Real Time Applications
• The organization must be convenient for users.
• Data structures are implemented in the real time in the following
situations:
– Car park
– File storage
– Machinery
– Shortest path
– Sorting
– Networking
– Evaluation of expressions
40

© Kalasalingam academy of research and education 40


Lesson 3. Algorithm, Asymptotic Notations, Time, Space
Complexity

© Kalasalingam academy of research and education 41


Algorithm
• What is Computer algorithm?
• a set of steps to accomplish or complete a task
that is described precisely enough that a
computer can run it.

42

© Kalasalingam academy of research and education 42


Algorithm
• For example,
• Task: to make a cup of tea.
• Algorithm:
o add water and milk to the kettle,
o Boil it,
o add tea leaves,
o Add sugar,
o and then serve it in cup

© Kalasalingam academy of research and education 43


Characteristics of an algorithm
• Must take an input.
• Must give some output(yes/no, value etc.)
• Definiteness – each instruction is clear and unambiguous.
• Finiteness – algorithm terminates after a finite number of
steps.
• Effectiveness – every instruction must be basic i.e. simple
instruction.

© Kalasalingam academy of research and education 44


Algorithm Analysis
• An Algorithm is a sequence of steps to solve a problem.
• The Analysis of Algorithm is very important for
designing algorithm to solve different types of problems
in the branch of computer science and information
technology.

© Kalasalingam academy of research and education 45


Algorithm Analysis
• In the analysis of algorithms, it is common to
estimate their complexity in the asymptotic
sense.
• to estimate the complexity function for arbitrarily
large input.

© Kalasalingam academy of research and education 46


Algorithm Analysis
• Expectation from an algorithm
– Correctness:-
• Correct: Algorithms must produce correct result.
• Produce an incorrect answer: Even if it fails to give correct
results all the time still there is a control on how often it gives
wrong result.
• Approximation algorithm: Exact solution is not found, but
near optimal solution can be found out.
–Less resource usage:
• Algorithms should use less resources (time and space).
© Kalasalingam academy of research and education 47
Algorithm Analysis
• Expectation from an algorithm
–Resource usage:
• The time is considered to be the primary efficiency.
• It is also concerned with how much the respective algorithm
involves the computer memory.
• The actual running time depends on the speed of the Computer, the
language in which the algorithm is implemented, the
compiler/interpreter, skill of the programmers etc.
• mainly the resource usage can be divided into:
1.Memory (space) 2.Time
© Kalasalingam academy of research and education 48
Algorithm Analysis
• The topic “Analysis of Algorithms” is
concerned primarily with determining the
memory (space) and time requirements
(complexity) of an algorithm.
• The time complexity (or simply, complexity)
of an algorithm is measured as a function of
the problem size.

© Kalasalingam academy of research and education 49


Algorithm Analysis
• Time taken by an algorithm?
– performance measurement or Apostoriori Analysis:
• Implementing the algorithm in a machine and then
calculating the time taken by the system to execute the
program successfully.
–Performance Evaluation or Apriori Analysis.
• Before implementing the algorithm in a system. This is done
as follows

50
© Kalasalingam academy of research and education 50
Algorithm Analysis
• Time taken by an algorithm?
– How long the algorithm takes :-
• will be represented as a function of the size of the input.
• f(n)→how long it takes if ‘n’ is the size of input.
– How fast the function that characterizes the
running time grows with the input size.
• “Rate of growth of running time”.
• The algorithm with less rate of growth of running time is
considered better.
© Kalasalingam academy of research and education 51
Asymptotic Notations
• Asymptotic Notations are languages that allow us to analyze
an algorithm’s running time by identifying its behavior as the
input size for the algorithm increases.
• This is also known as an algorithm’s growth rate.
• The word Asymptotic means approaching a value or curve
arbitrarily closely (i.e., as some sort of limit is taken).

© Kalasalingam academy of research and education 52


Asymptotic Notations
• Asymptotic Notations are the expressions that are used to
represent the complexity of an algorithm.
• When it comes to analysing the complexity of any algorithm in
terms of time and space, we can never provide an exact number
to define the time required and the space required by the
algorithm, instead we express it using some standard notations,
also known as Asymptotic Notations.

© Kalasalingam academy of research and education 53


Asymptotic Notations
• When we analyse any algorithm, we generally get a
formula to represent the amount of time required for
execution or the time required by the computer to run the
lines of code of the algorithm, number of memory accesses,
number of comparisons, temporary variables occupying
memory space etc.

© Kalasalingam academy of research and education 54


Asymptotic Notations
• There are three types of analysis that we perform on a particular
algorithm.
• Best Case: In which we analyse the performance of an algorithm
for the input, for which the algorithm takes less time or space.
• Worst Case: In which we analyse the performance of an algorithm
for the input, for which the algorithm takes long time or space.
• Average Case: In which we analyse the performance of an
algorithm for the input, for which the algorithm takes time or
space that lies between best and worst case.
© Kalasalingam academy of research and education 55
Types of Data Structure Asymptotic Notation
1. Big-O Notation(Ο) – Big O notationspecifically describes
worst case scenario.
2. Omega Notation (Ω) – Omega(Ω) notation
specifically describes best case scenario.
3. Theta Notation (θ) – This notation represents the
average complexity of an algorithm.

© Kalasalingam academy of research and education 56


Big-O Notation (Ο)
• Big O notation specifically describes worst case scenario.
• It represents the upper bound running time complexity of an
algorithm.
• The longest amount of time an algorithm can possibly take to
complete.
• It provides us with an asymptotic upper bound for the growth
rate of run-time of an algorithm.
• Lets take few examples to understand how we represent the
time and space complexity using Big O notation.
© Kalasalingam academy of research and education 57
Big-O Notation (Ο)
• O(1)
– Big O notation O(1) represents the complexity of an algorithm that always
execute in same time or space regardless of the input data.
– example
The following step will always execute in same time(or space) regardless of
the size of input data.
. array index(int num = arr[5])
• Accessing

This function runs in O(1) time (or "constant time") relative to its input. The input array could be 1
item or 1,000 items, but this function would still just require one step.
© Kalasalingam academy of research and education 58
Big-O Notation (Ο)
• O(n)
– Big O notation O(N) represents the complexity of an algorithm, whose
performance will grow linearly (in direct proportion) to the size of the
input data.
– O(n)example

– This function runs in O(n) time (or "linear time"), where n is the number of items in the
array. If the array has 10 items, we have to print 10 times. If it has 1000 items, we have to print 1000
times.
© Kalasalingam academy of research and education 59
Big-O Notation (Ο)
• O(n2)
– Big O notation O(n2) represents the complexity of an algorithm,
whose performance is directly proportional to the square of the
size of the input data.
– O(n2) example
• Traversing a 2D array

© Kalasalingam academy of research and education 60


Big-O Notation (Ο)
• O(n2)
– Here we're nesting two loops. If our array has n items, our outer
loop runs n times and our inner loop runs n times for each
iteration of the outer loop, giving us n2 total prints. Thus this
function runs in O(n2) time (or "quadratic time"). If the array has
10 items, we have to print 100 times. If it has 1000 items, we have
to print 1000000 times.

61

© Kalasalingam academy of research and education 61


Big-O Notation (Ο)
• It provides us with an asymptotic upper bound for the growth
rate of runtime of an algorithm.
• Say f(n) is your algorithm runtime, and g(n) is an arbitrary
time complexity you are trying to relate to your algorithm.
• A function f(n) can be represented is the order of g(n) that is
O(g(n)).
• f(n) is O(g(n)), if for some real constants c (c > 0) and n0, f(n)
<= c g(n) for every input size n (n > n0).

© Kalasalingam academy of research and education 62


Big-O Notation (Ο)
• It tells us that a certain function will never exceed a specified
time for any value of input n.
• Consider Linear Search algorithm, in which we traverse an
array elements, one by one to search a given number.
• starting from the front of the array, we find the element or
number we are searching for at the end, which will lead to a
time complexity of n, where n represents the number of total
elements.

© Kalasalingam academy of research and education 63


Big-O Notation (Ο)
• But it can happen, that the element that we are
searching for is the first element of the array, in
which case the time complexity will be 1.
• when we use the big-O notation, we mean to say
that the time complexity is O(n), which means
that the time complexity will never exceed n,
defining the upper bound, hence saying that it
can be less than or equal to n, which is the
correct representation.
© Kalasalingam academy of research and education 64
Omega Notation (Ω)
• Omega notation specifically describes best case scenario.
• It represents the lower bound running time complexity of
an algorithm.
• So if we represent a complexity of an algorithm in Omega
notation, it means that the algorithm cannot be completed
in less time than this.
• It provides us with an asymptotic lower bound for the
growth rate of runtime of an algorithm.

© Kalasalingam academy of research and education 65


Omega Notation (Ω)
• This always indicates the minimum time required for any
algorithm for all input values, therefore the best case of any
algorithm.
• In simple words, when we represent a time complexity for
any algorithm in the form of big-Ω, we mean that the
algorithm will take atleast this much time to complete it's
execution.

© Kalasalingam academy of research and education 66


Omega Notation (Ω)
• The actual time complexity of the function which is determined
by the time for an algorithm is increased
• Now you want to give a lower bound to that function i.e g(n) in
such a way that c*g(n) is less then f(n) after some value of n i.e
no.
Which means that f(n)>=c*g(n) After some value of n i.e n>=no
Where c is a constant if c>0 &no is an input i.e no>=1

© Kalasalingam academy of research and education 67


Theta Notation (θ)
• This notation describes both upper bound and lower bound of
an algorithm so we can say that it defines exact asymptotic
behaviour.
• In the real case scenario the algorithm not always run on best
and worst cases, the average running time lies between best
and worst and can be represented by the theta notation.

© Kalasalingam academy of research and education 68


Theta Notation (θ)
• Theta commonly written as Θ is an
Asymptotic Notation to denote
the asymptotically tight bound on the growth
rate of runtime of an algorithm.

© Kalasalingam academy of research and education 69


Theta Notation (θ)
• If we have a function f(n) then we should find the upper and
lower bound by a function just by the value of some constant.
• If f(n) is bounded by c1*g(n) and c2*g(n) then we can say that
f(n) is θ (g(n)).
• So the constants c1 &c2 could be different and moreover after a
value we could taken any value
• Which means that after the value of no both of them are c1*g(n)
less then f(n) and c2*g(n) greater then f(n)

© Kalasalingam academy of research and education 70


Performance analysis
• Performance analysis is a process of calculating space and time
required by that algorithm
• Performance analysis of algorithm is performed by considering
following measures.
1.Space required to complete the task of that algorithm (Space
Complexity). It includes program space and data space
2.Time required to complete the task of that algorithm (Time
Complexity)

© Kalasalingam academy of research and education 71


Space complexity
• Total amount of computer memory required by an algorithm to
complete its execution is called space complexity of that algorithm.
• When a program is under execution it uses computer memory
for three reasons
1. Instructional space: stores compiled version of instructions
2. Environmental stack: store information about function calls
3. Data space: stores variables, constants and structures etc…

To compute space complexity data space is to be considered.

© Kalasalingam academy of research and education 72


Example-1

int add(int a,
int b)
{
return a+b;
}

total space is 2 bytes for each variable a and b.


2 bytes for return value
Total space required is 6 bytes.
© Kalasalingam academy of research and education 73
Example -2
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}

total space is A[ ] n*2


n= 2 i=2 sum=2
return value=2 total space is 2n+8
© Kalasalingam academy of research and education 74
Time Complexity
• Time complexity of an algorithm is amount of time required to complete its
execution.
• The running time of an algorithm depends on following ..
1. Processor speed
2. Single or multiple processors
3. 32 bit or 64 bit of a machine
4. Depends on operations
5. Input size
to compute time complexity only input size is to be considered.
• It requires 1 unit of time for Arithmetic and Logical operations
• It requires 1 unit of time for Assignment and Return value
• It requires 1 unit of time for Read and Write operations
© Kalasalingam academy of research and education 75
Lesson 4 - Recursion

© Kalasalingam academy of research and education 76


Recursion - Concept
Recursion concept is to implement the algorithm family of Divide
and Conquer and Dynamic Programming

The problem to be solved is represented in the form of recursive sub


problem, while doing so the problem size is reduced.

While reducing the problem size, there should be a limit after which
the problem size reduction should not go on.

© Kalasalingam academy of research and education 77


Recursion – Sub Problem Identification
Sub problem identification is very crucial
By identifying sub problem, the problem size got reduced
Eg.
5! = 5 x 4 !
In the above example, problem to be solved is 5!, and it has been
represented in terms of sub problem 4!. Now the problem size is
reduced.

© Kalasalingam academy of research and education 78


Recursion – Stopping Condition
If the recursion logic is not stopped, the logic will go towards infinity
It is mandatory for a recursive logic to have stopping condition
Eg.
5! = 5 x 4 !
 4! = 4 x 3!
 3! = 3 x 2!
 2! = 2 x 1!
Here in the above example 1! Is the stopping point, after that, sub-problem logic
should be stopped and unwinding should happen
© Kalasalingam academy of research and education 79
Recursive Functions
•Recursion is a process in which a function calls itself as a
subroutine.
•This allows the function to be repeated several times, since it calls
itself during its execution.
•Functions that incorporate recursion are called recursive functions.

© Kalasalingam academy of research and education 80


Recursion – Examples
•GCD finding using euclid method
•Factorial finding
•Fibonacci series printing
•Binary search

© Kalasalingam academy of research and education 81


THINGS TO NOTE WHILE WRITING RECURSIVE
FUNCTION

•IDENTFIY RECURSIVE SUB PROBLEM AND CREATE


FUNCTION ACCORDINGLY

•STOPPING CONDITION

© Kalasalingam academy of research and education 82


Finding GCD using Recursion – Euclid method
#include<stdio.h>
int GCD(int large, int small)
{
int remainder;
remainder = large % small;
if (remainder == 0)
return small;
return GCD(small, remainder);
}
© Kalasalingam academy of research and education 83
Binary Search using Recursion

© Kalasalingam academy of research and education 84


Factorial using Recursion

© Kalasalingam academy of research and education 85


Fibonacci Series Printing using Recursion
(first 15 items)

© Kalasalingam academy of research and education 86


Lesson 4 – Linear Search, Binary Search

© Kalasalingam academy of research and education 87


Linear Search

• The linear search is a sequential search, which uses a loop to


step through an array, starting with the first element.
• It compares each element with the value being searched for,
and stops when either the value is found or the end of the
array is encountered.
• If the value being searched is not in the array, the algorithm
will unsuccessfully search to the end of the array.

© Kalasalingam academy of research and education 88


Linear Search

• Since the array elements are stored in linear order


searching the element in the linear order make it easy and
efficient.
• The search may be successful or unsuccessfully. That is,
if the required element is found them the search is
successful other wise it is unsuccessfully.

© Kalasalingam academy of research and education 89


Advantages

• The linear search is simple - It is very easy to understand


and implement
• It does not require the data in the array to be stored in
any particular order

© Kalasalingam academy of research and education 90


Disadvantages

• It has very poor efficiency because it takes lots of


comparisons to find a particular record in big files
• The performance of the algorithm scales linearly with
the size of the input
• Linear search is slower then other searching
algorithms

© Kalasalingam academy of research and education 91


Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


© Kalasalingam academy of research and education 92
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


© Kalasalingam academy of research and education 93
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


© Kalasalingam academy of research and education 94
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


© Kalasalingam academy of research and education 95
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


© Kalasalingam academy of research and education 96
Linear Search Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86: found!


97
© Kalasalingam academy of research and education 97
Analysis of Linear Search
• How long will our search take?

• In the best case, the target value is in the first element


of the array.
• So the search takes some tiny, and constant, amount of
time.
• In the worst case, the target value is in the last element
of the array.
• So the search takes an amount of time proportional to
the length of the array.

© Kalasalingam academy of research and education 98


Analysis of Linear Search

• In the average case, the target value is somewhere in the


array.
• In fact, since the target value can be anywhere in the array,
any element of the array is equally likely.
• So on average, the target value will be in the middle of the
array.
• So the search takes an amount of time proportional to half
the length of the array

© Kalasalingam academy of research and education 99


Binary Search
The general term for a smart search through sorted data is a binary search.
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. If you’ve found your target, stop.
4. If your target is less than the middle data value, the new search region is
the lower half of the data.
5. If your target is greater than the middle data value, the new
search region is the higher half of the data.
6. Continue from Step 2.

© Kalasalingam academy of research and education 100


Binary Search Example

-86 -37 -23 0 5 18 21 64 97

low middle high

Searching for 18.


© Kalasalingam academy of research and education 101
Binary Search Example

-86 -37 -23 0 5 18 21 64 97

low high
middle
Searching for 18.
© Kalasalingam academy of research and education 102
Binary Search Example

-86 -37 -23 0 5 18 21 64 97

low high
middle
Searching for 18: found!
17
© Kalasalingam academy of research and education 103
Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

© Kalasalingam academy of research and education 104


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

© Kalasalingam academy of research and education 105


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.

© Kalasalingam academy of research and education 106


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.

© Kalasalingam academy of research and education 107


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.

© Kalasalingam academy of research and education 108


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

© Kalasalingam academy of research and education 109


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.

© Kalasalingam academy of research and education 110


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.

© Kalasalingam academy of research and education 111


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.

© Kalasalingam academy of research and education 112


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.

© Kalasalingam academy of research and education 113


Binary Search – Example 2
Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.
© Kalasalingam academy of research and education 114
Time Complexity of Binary Search
How fast is binary search?
Think about how it operates: after you examine a value, you cut the
search region in half.
So, the first iteration of the loop, your search region is the whole array.
The second iteration, it’s half the array.
The third iteration, it’s a quarter of the array.
...
The kth iteration, it’s (1/2k-1) of the array.
© Kalasalingam academy of research and education 115
Problem with Binary Search
• Binary search only works if the array is already sorted.
• It turns out that sorting is a huge issue in computing.

© Kalasalingam academy of research and education 116


The End

© Kalasalingam academy of research and education 117

You might also like