0% found this document useful (0 votes)
7 views

Lecture # 1 Data Structures and Algorithm

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture # 1 Data Structures and Algorithm

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Data Structures and Algorithm

Introduction to Data
Structure

A data structure is a particular way of


storing and organizing data in a
computer so that it can be used
efficiently
Need for Data Structures
 Data structures organize data  more efficient programs.
 More powerful computers  more complex applications.
 More complex applications demand more calculations.
Organizing Data
 Any organization for a collection of records that can be searched,
processed in any order, or modified.
 The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days.
What is Data Structure? 5

 Data structure is a representation


of data and the operations
allowed on that data.
 A data structure is a way to store and organize
data in order to facilitate the access and
modifications.
 Data Structure is the method of representing of
logical relationships between individual data
elements related to the solution of a given
problem.
Data Structures 6

 A data structure is a particular way of organizing data


in computer memory so that it can be used efficiently.

 Classification of Data Structure


 Data structure can be classified into two main
categories: Primitive & Non-Primitive
1. Primitive Data Structure
 Basic data types are known as primitive data structures.
Also called simple Data Structure e.g. integer, real,
Boolean, character etc.
Data Structures 7

2. Non-Primitive Data Structure


 The data types which are derived from primary data types are
known as non-primitive data structure. These are used to store
group of values e.g. array, structures, link list, stacks, queues
etc.
 Non-Primitive data structure are further divided into two
categories: Linear & Non-Linear
 Linear Data Structure
 A data structure is said to be linear if its elements form a
sequence i.e. elements are attached with one another e.g.
array, link list, stacks, queues etc.
 Non-Linear Data Structure
 In non-linear data structure elements are not organized in a
sequential fashion. A data item can be attached to several
other data elements e.g. tree, graph etc.
array

Linked list

queue
tree stack
Linear Data Structures 1
0

 A data structure is said to be linear if its


elements form a sequence or a linear list.
 Examples:
 Arrays
 Linked Lists
 Stacks
 Queues
Non-Linear Data 1
1
Structures
 A data structure is said to be non-linear if its
elements does not form a sequence or a linear
list.
 Examples:
 Trees
 Graphs
 Hash Tables
 Each element may be connected with two or
more other nodes or items in a non-linear
arrangement.
Terms in Data Structures1
2
 Interface
 Each data structure has an interface.

thanks to www.tutorialspoint.com
Interface represents the set of operations
that a data structure supports.

 Implementation
 Implementation provides the internal
representation of a data structure. It also
provides the definition of algorithms used
in the operation of data structure.
Data Structure 1
3
Operations
 Traversing
accessing and processing each record
exactly once.
 Searching
finding the location of the record with key
value.
 Inserting
Adding new record to the structure.
 Deleting
Removing a record from the structure.
Characteristics of Data Structure1
4
 Correctness
 Data structure implementation should implement its

thanks to www.tutorialspoint.com
interface correctly.
 Time Complexity
 Running time or the execution time of operations of data
structure must be as small as possible.
 Space Complexity
 Memory usage of a data structure operation should be
as little as possible.
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must meet.
2. Determine the basic operations that must be
supported. Quantify the resource
constraints for each operation.
3. Select the data structure that best meets
these requirements.
1
6

Introduction to
Algorithms
A PRECISE RULE (OR SET OF RULES)
SPECIFYING HOW TO SOLVE SOME PROBLEM.
Algorithms 1
7
 An algorithm is a clearly defined set of instructions to
be followed to solve a problem. Algorithms are

thanks to www.tutorialspoint.com
generally created independent of underlying
programming languages.
 OR
 An algorithms is a well defined list of finite steps for
solving a particular problem.
 Major purpose is to develop efficient algorithm for the
processing of data.
 The time and space are two major measures to check
the efficiency of an algorithm.
1
8
 From the data structure point of view, following
are some important categories of algorithms
 Search − Algorithm to search an item in a data
structure
 Sort − Algorithm to sort items in a certain order
 Insert − Algorithm to insert item in a data
structure
 Update − Algorithm to update an existing item in
a data structure
 Delete − Algorithm to delete an existing item
from a data structure
Characteristics of an Algorithm
1
9
 Not all procedures can be called an algorithm. An algorithm
should have the following characteristics:

thanks to www.tutorialspoint.com
 Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should
be clear and must lead to only one meaning.
 Input − An algorithm should have 0 or more well-defined
inputs.
 Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
 Finiteness − Algorithms must terminate after a finite number
of steps.
 Feasibility − Should be feasible with the available resources.
 Independent − An algorithm should have step-by-step
directions, which should be independent of any programming
code.
Example 2
0
 Problem − Design an algorithm to add two numbers
and display the result.

thanks to www.tutorialspoint.com
 Step 1 – START
 Step 2 – declare three integers a, b & c
 Step 3 – define values of a & b
 Step 4 – add values of a & b
 Step 5 – store output of Step 4 to c
 Step 6 – print c
 Step 7 – STOP
Example 2
1

 Problem − Design an algorithm to add two numbers

thanks to www.tutorialspoint.com
and display the result.

 Step 1 – START ADD


 Step 2 – get values of a & b
 Step 3 – c  a + b
 Step 4 – display c
 Step 5 – STOP

 Second method is preferable in Data Structure and


Algorithms
Best, Worst and Average 2
2
Cases
 Not all inputs of a given size take the same
time.
 Each algorithm has three cases:
 Best case:
 Worst Case:
 Average Case:
Example: Best, Worst and Average Cases
2
3

 Sequential search for ‘k’ in an array of ‘n’


integers:
 Best case: ‘k’ is the first element of the array.
 Worst case: the search must visit every element
once. This happens when the value being
searched for is either the last element in the list,
or is not in the list
 Average case: on average, assuming the value
searched for is in the list and each list element is
equally likely to be the value searched for, the
search visits only n/2 elements.
Algorithm notation 2
4

 The algorithm notations are used to present


different steps in solving a problem
 An algorithm consists of several parts and
different types of statements
 Name of Algorithm:
 Every algorithm is given a name that represents the
purpose of the algorithm. E.g. Sum, Average, Max
 Introductory Comments:
 The algorithm name is followed by a brief
description of the tasks that the algorithm performs.
Algorithm notation (cont.)

 Steps:
 Algorithm consists of a sequence of numbered steps.
Each step describes one task to be performed.
 Comments:
 Used to explain the purpose of a step
 Variable names:
 Name of variable is chosen according the value it hold
 Operator:
 Arithmetic (+,-,*,/) Relational(<,<=,>,>= etc.) and
logical( AND, OR, NOT)
 Assignment statement:
 the statement used “=” assignment operator
Sub Algorithm

 A sub algorithm is a complete and


independently defined algorithmic module
which is used (invoked or called) by some main
algorithm or by some other subalgorithm. A
subalgorithms receives values, called
arguments, from an originating (calling)
algorithm, performs computations; and then
sends back the result to the calling algorithm.
Traversing

 (Traversing a Linear Array) Here LA is a linear


array with lower bound LB and upper bound UB.
This algorithm travers LA applying an operation
PROCESS to each element of LA.
 1- Repeat for k=LB to UB
 Apply PROCESS to LA[k]
 [End of Loop]

2- Exit
• (Traversing a Linear Array) Here LA is a linear Array
with lower bound LB and upper bound UB. This algo
travers LA applying a PROCESS to each element of
LA

• Step 1
[Initialize] Set k=LB
• Step 2
Repeat Step 3 and 4 while K<=UB
• Step 3
Applying PROCESS to LA[k]
• Step 4
[Increment Counter] Set K=K+1
[End of Step 2 Loop]
Step 5 Exit.
Example

 An array DATA of numeric values in memory. We


want to find location LOC and largest element
MAX of DATA. Given no information of DATA.

 A formal presentation of this algorithm is as


follow
• (Largest Element in Array) A non empty array
DATA with N numerical values is given. This
algorithm finds the location LOC and value MAX of
the largest element of DATA. The variable K is used
as a counter.

• Step 1 [Initialize] Set k=1, LOC =1, MAX = DATA[1]


• Step 2 [Increment Counter] Set K=K+1
• Step 3 [Test Counter] if K>N then
• write LOC , MAX and exit.
• Step 4 [Compare and Update] if MAX<DATA[k] then
• Set LOC=K and MAX= DATA[K]
• Step 5 [Repeat Loop] Go to Step 2.
• (Linear Search) A Linear array DATA with N
elements and a specific ITEM of information are
given. This algorithm finds the LOC of ITEM in the
array DATA or set LOC=-1

• Step 1 [Initialize] Set k=0, LOC =-1


• Step 2 Repeat Step 3 and 4 while LOC=-1 and K<N
• Step 3 if ITEM=DATA[K] then
• Set LOC=K
• Step 4 [Increment Counter] Set K=K+1
• [End of Step 2 Loop]
• Step 5 if LOC=-1 then write ITEM is not in array
DATA
• Else Write LOC is the location of ITEM
[End of IF Structure]
Step 6 Exit.
Insertion
 (Insertion in to Linear Array) INSERT(LA,N,K,ITEM) Here LA
is a linear Array with N elements and K is a positive integer
such that k<=N. This algorithm insert an element ITEM into
k th position in LA.
 Step 1 [Initialize counter] Set J=N
 Step 2 Repeat Step 3 and 4 while J>=k
 Step 3 [Move Jth element downword]
 Set LA[j+1]=LA[j]
 Step 4 [Decrease Counter] Set J=J-1
[End of Step 2 Loop]
Step 5 [Insert Element.] SetLA[k]= ITEM
Step 6 [Reset N] Set N=N+1
Step 7 Exit
Deletion

 (Deleting from a Linear


Array)DELETE(LA,N,K,ITEM)
 Here LA is a linear Array with N elements and K
is a positive integer such that K<=N. This
algorithm deletes the kth element from LA.
 1. Set ITEM=LA[K]
 2. Repeat for j=k to N-1
 [Move J+1st element upward]Set LA[j]=LA[j+1]
 [End of Loop]
3. [Reset N] Set N=N-1
4. Exit
Bubble Sort

Let A be a list of N numbers. Sorting A refers to the operations of


rearranging the elements of A so that they are in increasing order, i.e.,
so that

A[1]< A[2]< A[3]< . . . A[N]

For example A is originally in the list

8 , 4 , 19, 2 , 7 , 13 , 5 , 16

After Sorting A is the list

2 , 4 , 5 , 7 , 8 , 13 , 16 , 19
Bubble sort cont.
Suppose that the list of numbers A[1], A[2],…A[N] is in memory,
working of bubble sort is as follow
Step-1: Compare A[1] and A[2] and arrange them in desired
order, so that A[1] < A[2].
Then compare A[2] and A[3] and arrange them , so that A[2] < A[3].
Continue until we compare A[N-1] with A[N] and arrange them , so that
A[N-1] < A[N].

Observe that Step – 1 involves N-1 comparisons. During step -1 the


largest element is bubbled up the nth position in the list.
Step- 2: Repeat step1 with one less comparisons. Step 2 involves N-2
comparisons.
…………………………………………………………………………………..
…………………………………………………………………………………..
…………………………………………………………………………………...
Step- N-1: Compare A[1] and A[2] and arrange them so that
A[1] < A[2].
32,51,27,85,66,23,13,57
Bubble sort
(Bubble Sort) BUBBLE(DATA,N)
Here DATA is an Array with N elements. This algorithm sort the
elements in DATA.

1- Repeat step 2 and 3 for k=0 to N-1


2- Set PTR=0 [Initialize Pass pointer]
3- Repeat while PTR<= N-K [Execute Pass]
a) if( DATA[PTR] > DATA[PTR+1] ) then
Interchange DATA[PTR] and DATA[PTR+1]
[End of if]
b) set PTR=PTR+1
[end of inner loop]
[end of step – 1 outer loop]
4- Exit.
Complexity of bubble
sort
 Traditionally, Time of sorting algorithm is measured in term of
the number of comparisons.
 There are n-1 comparison in first pass which place the largest
element in the last position.
 There are n-2 comparison in second pass which place the
second largest element in the next to last position and so on.
Thus
 F(n)=(n-1)+(n-2)+…+2+1
 = n(n-1)/2
 =n2/2+O(n)
 =O(n2)

Time required to execute the bubble sort algorithm


is proportional to n2 . Where n is the number of
input items.
Binary search

 Suppose DATA is an array which sorted in


increasing numerical order. Then Binary search
is extremely efficient searching algorithm.

(Binary Search)BINARY(DATA,LB,UB,ITEM,LOC)
Here DATA is sorted array with lower bound LB
and upper bound UB and ITEM is a given type
of information. The variables BEG,END, and
MID denote respectively, the beginning, end
and middle location of a segment of elements
of DATA. This algorithm finds the location LOC
of ITEM in DATA.
1- [initialize variables] Set BEG=LB, END=UB LOC=-1
and MID=INT((BEG+END))/2
2- Repeat step 3 and 4 while (BEG < = END) and
DATA[MID] != ITAEM
3- If ITEM<DATA[MID] then
Set END=MID-1
else
Set BEG= MID+1
[End of IF]
4- Set MID=INT((BEG+END))/2
[End of Step 2 loop]
5- If DATA[MID]=ITEM
then
Set LOC= MID
[End of IF]
6- Exit

You might also like