3. COURSE INTRODUCTION
This course introduces students to learn about data elements which
provides an efficient way of storing and organizing data in the computer
so that it can be used efficiently.
Also about some of Data Structures such as arrays, Linked List, Stack,
Queue, etc.
Data Structures are widely used in almost every aspect of Computer
Science i.e. Operating System, Compiler Design, Artificial intelligence,
Graphics and many more.
4. Course Contents
Review of elementary programming concepts: Variables and Data
types, Conditional Statements, Looping Statements,Arrays.
Fundamental data structures: Stacks,Queues,Linked lists,Hash
tables,Trees,Graphs.
Fundamental computing algorithms :O(N log N) sorting algorithms
Binary search trees,Representations of graphs,Depth- and Breadth-first traversals.
Recursion :The concept of recursion,Recursive mathematical functions,Simple
recursive procedures,Divide-and-conquer strategies.
Algorithmic strategies,Brute-force algorithms,Greedy algorithms,Branch-and-
bound; Pattern matching and string/text algorithms; Numerical approximation algorithms
5. MODE OF ASSESSMENTS
CONTINUOUS ASSESSMENT Week Weight Remarks
ASSIGNMENTS
1-4 10% Subjective
CLASS TEST 6
10%
Subjective
QUIZ 3-8 10%
Subjective
FINAL EXAM 12 60%
Subjective
6. Data: Collection of raw facts.
Data structure is a specialized format for organizing and storing data in
memory that considers not only the elements stored but also their
to each other.
• Data structure Is an arrangement of data In computer's memory. It makes
data to be quickly available to the processor for required operations.
• It is a structure program used to store ordered data, so that various
operations can be performed on it easily.
It should be designed and implemented in such a way that it reduces the
complexity and increases the efficiency.
INTRODUCTION
7. INTRODUCTION
Data structure affects the design of both structural & functional aspects of
a program.
Program=algorithm + Data Structure
You know that a algorithm is a step by step procedure to solve a
particular function.
8. Classification of data structure
Data structure are normally divided into two broad categories:
Primitive Data Structure & NON-PRIMITIVE DATA
STRUCTURE
.
9. Primitive Data Structure
Data structures that are directly operated upon the machine-level
instructions are known as primitive or primary data structures.
Integer, Floating-point number, Character constants, string constants,
pointers etc, fall in this category as example of Simple data structure.
The most commonly used operation
on data structure are broadly
categorized into following types:
◦ Create
◦ Selection ◦ Updating
◦ Destroy or Delete
10. NON-PRIMITIVE DATA STRUCTURE
The Data structures that are derived from the primitive data structures are
called Non-primitive or secondary data structure.
The non-primitive data structures emphasize on structuring of a group of
homogeneous (same type) or heterogeneous ( different type) data items.
NON-PRIMITIVE DATA STRUCTURE IS DIVIDED INTO:
1. linear data structure
2. Non-linear data structure
NOTE There are more sophisticated data structures.
11. 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.
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.
◦ some commonly non-linear data structures are trees and graphs.
12. OPERATIONS APPLIED ON DATA STRUCTURES
The most commonly used operation on data structure are broadly categorized
into following types:
◦ Traversal
◦ Insertion
◦ Selection
◦ Searching
◦ Sorting
◦ Merging
◦ Destroy or Delete
13. Different between A primitive data structure
and non primitive data structure
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, AVL Tree, graph etc.
14. INTRODUCTION TO ALGORITHM
Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output.
Algorithms are generally created independent of underlying languages, i.e. an
algorithm can be implemented in more than one programming language
15. SOME IMPORTANT CATEGORIES OF ALGORITHMS
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.
16. CHARACTERISTICS OF AN ALGORITHM
Not all procedures can be called an algorithm. An algorithm should have the following
characteristics −
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.
17. HOW TO WRITE AN ALGORITHM
There are no well-defined standards for writing algorithms. Rather, it is problem and
resource dependent. Algorithms are never written to support a particular programming
code.
As we know that all programming languages share basic code constructs like loops (do,
for, while), flow-control (if-else), etc. These common constructs can be used to write an
algorithm.
We write algorithms in a step-by-step manner, but it is not always the case. Algorithm
writing is a process and is executed after the problem domain is well-defined. That is,
we should know the problem domain, for which we are designing a solution.
18. HOW TO WRITE AN ALGORITHM
Example
Let's try to learn algorithm-writing by using an example.
Problem − Design an algorithm to add two numbers and display the result.
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
In design and analysis of algorithms, usually the second method is used to describe an algorithm. It
makes it easy for the analyst to analyze the algorithm ignoring all unwanted definitions. He can
observe what operations are being used and how the process is flowing.
Writing step numbers, is optional.
We design an algorithm to get a solution of a given problem. A problem can be solved in more
than one ways.
19. ALGORITHM ANALYSIS
Efficiency of an algorithm can be analyzed at two different stages, before implementation and after
implementation. They are the following −
A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an algorithm is measured
by assuming that all other factors, for example, processor speed, are constant and have no effect on
the implementation.
A Posterior Analysis − This is an empirical analysis of an algorithm. The selected algorithm is
implemented using programming language. This is then executed on target computer machine. In
this analysis, actual statistics like running time and space required, are collected.
We shall learn about a priori algorithm analysis. Algorithm analysis deals with the execution or running
time of various operations involved. The running time of an operation can be defined as the number
of computer instructions executed per operation.
20. Algorithm Complexity
Suppose X is an algorithm and n is the size of input data, the time and space used
by the algorithm X are the two main factors, which decide the efficiency of X.
Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
Space Factor − Space is measured by counting the maximum memory space
required by the algorithm.
The complexity of an algorithm f(n) gives the running time and/or the storage space
required by the algorithm in terms of n as the size of input data.
21. SPACE COMPLEXITY
Space complexity of an algorithm represents the amount of memory space
required by the algorithm in its life cycle. The space required by an algorithm is
equal to the sum of the following two components
A fixed part that is a space required to store certain data and variables, that are
independent of the size of the problem. For example, simple variables and
constants used, program size, etc.
A variable part is a space required by variables, whose size depends on the size of
the problem. For example, dynamic memory allocation, recursion stack space, etc.
Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed
part and S(I) is the variable part of the algorithm, which depends on instance
characteristic I. Following is a simple example that tries to explain the concept
22. TIME COMPLEXITY
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop
Here we have three variables A, B, and C and one constant. Hence S(P) = 1 + 3.
Now, space depends on data types of given variables and constant types and it
will be multiplied accordingly.
23. TIME Complexity
Time complexity of an algorithm represents the amount of time required
by the algorithm to run to completion. Time requirements can be defined
as a numerical function T(n), where T(n) can be measured as the number
of steps, provided each step consumes constant time.
For example, addition of two n-bit integers takes n steps. Consequently,
the total computational time is T(n) = c ∗ n, where c is the time taken for
the addition of two bits. Here, we observe that T(n) grows linearly as the
input size increases.
24. Description of various data structure
ARRAY
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.
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:
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
25. ARRAY
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.
The elements of linear array are stored in consecutive
memory locations. It is shown below
26. ARRAY
◦ 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.
◦ Array can always be read or written through loop.
For(i=0;i<=9;i++)
{scanf(“%d”,&arr[i]); printf(“%d”,arr[i]);
27. TYPES OF ARRAY
Single Dimension Array: Array with one subscript
Two Dimension Array: Array with two subscripts (Rows and Column)
Multi Dimension Array: Array with Multiple subscripts
28. Basic operations of Arrays
Some common operation performed on array are:
◦ Traversing
◦ Searching
◦ Insertion
◦ Deletion
◦ Sorting
◦ Merging
29. TRAVERSING
Traversing:It is used to access each data item
exactly once so that it can be processed. E.g.
We have linear array A as below:
0 1 2 3 4
10 20 30 40 50
Here we will start from beginning and will go till
last element and during this process we will
access value of each element exactly once as
below:
A [1] = 10 A [2] = 20
A [3] = 30
A [4] = 40
A [5] = 50
30. INSERTION
Insertion: It is used to add a new data item in the given collection of data
items.
E.g. We have linear array A as below:
12 3 4 5
10 20 50 30 15
New element to be inserted is 100 and location for insertion is 3. So shift
the elements from 5th location to 3rd location downwards by 1 place.
And then insert 100 at 3rd location. It is shown below:
32. DELETION OF ARRAYS
Deletion of arrays: It is used to delete an existing data item from the
given collection of data items
33. SEARCHING ARRAY
Searching: It is used to find out the location of the data item if it exists in the
given collection of data items.
E.g. We have linear array A as below:
12 3 4 5
15 50 35 20 25
Suppose item to be searched is 20. We will start from beginning and will compare
20 with each element. This process will continue until element is found or array is
finished. Here:
1) Compare 20 with 15 20 # 15, go to next element. 2) Compare 20 with 50 20
# 50, go to next element.
Compare 20 with 35 20 #35, go to next element.
Compare 20 with 20
20 = 20, so 20 is found and its location is 4.
35. BINARY SEARCH
algorithm can be used with
only sorted list of
elements.
Binary Search first divides
a large array into two
smaller sub -arrays and
then recursively operate
the sub -arrays.
Binary Search basically
reduces the search space to
half at each step