Lecture 1 - Introduction+Array
Lecture 1 - Introduction+Array
Lecture 1 - Introduction+Array
STRUCTURES AND
ALGORITHMS
INTRODUCTION TO DATA
STRUCTURES
What is DATA?
In general, data is any set of characters that is gathered and translated for some purpose,
usually analysis.
In a computer's storage, data is a series of bits (binary digits) that have the value one or
zero. Data is processed by the CPU, which uses logical operations to produce new data
(output) from source data (input).
For Example,
Joe, Smith, 1234 Circle, 8404, 8015533144
INTRODUCTION TO DATA
STRUCTURES
A data structure is a particular way of organizing data in a computer so that it can be used
effectively.
A way of collecting and organizing data in such a way that we can perform operations on
these data in an effective way.
It is about rendering data elements in terms of some relationship, for better organization and
storage.
For example, we have some data which has, player's name “William" and age 26. Here
“William" is of String data type and 26 is of integer data type.
WHY DATA STRUCTURE AND
ALGORITHMS?
As applications are getting complex and data rich, there are three common problems that
applications face now-a-days.
2. Processor speed − Processor speed although being very high, falls limited if the data
grows to billion records.
From the data structure point of view, following are some important operations 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.
APPLICATIONS OF DATA STRUCTURE AND
ALGORITHMS
The following computer problems can be solved using Data Structures
Tower of Hanoi
Project scheduling
SO WHAT IS DATA STRUCTURE
ACTUALLY?
In simple language, Data Structures are structures programmed to store ordered data, so
that various operations can be performed on it easily. It represents the knowledge of data
to be organized in memory. It should be designed and implemented in such a way that it
reduces the complexity and increases the efficiency.
CHARACTERISTICS OF A DATA
STRUCTURE
Correctness − Data structure implementation should implement its 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.
BASIC TYPES OF DATA
STRUCTURES
Non-primitive
data
Primitive data structures are
structures are more
those which are complicated data
predefined way structures and are
of derived
storing data by from primitive
the system data structures.
CLASSIFICATION OF DATA
STRUCTURES
The data structures can also be classified on the basis of the following
characteristics:
Characterstic Description
Linear In Linear data structures, the data items are arranged in a linear sequence. Example: Array
Non-Linear In Non-Linear data structures,the data items are not in sequence. Example: Tree, Graph
Homogeneous In homogeneous data structures,all the elements are of same type. Example: Array
Non-Homogeneous In Non-Homogeneous data structure, the elements may or may not be of the same type.
Example: Structures
Static Static data structures are those whose sizes and structures associated memory locations are fixed, at
compile time. Example: Array
Dynamic Dynamic structures are those which expands or shrinks depending upon the program need and its
execution. Also, their associated memory locations changes. Example: Linked List created using
pointers
DATA TYPES
Data Type - Data type is a way to classify various types of data such as integer,
string, etc. which determines the values that can be used with the corresponding type
of data, the type of operations that can be performed on the corresponding type of
data. There are two data types :
1. Built-in Data Type - Those data types for which a language has built-in support are
known as Built-in Data types. For example, most of the languages provide the following
built-in data types.
Integers
Boolean (true, false)
Floating (Decimal numbers)
Character and Strings
DATA TYPES
2. Derived Data Type - Those data types which are implementation independent as they
can be implemented in one or the other way are known as derived data types. These data
types are normally built by the combination of primary or built-in data types and
associated operations on them. For example :
List
Array
Stack
Queue
BASIC OPERATIONS ON DATA
The data in the data structures are processed by certain operations. The particular
data structure chosen largely depends on the frequency of the operation that needs to
be performed on the data structure.
Traversing
Searching
Insertion
Deletion
Sorting
Merging
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Array is a container which can hold a fix number of items and these items should be
of the same type. Most of the data structures make use of arrays to implement their
algorithms. Following are the important terms to understand the concept of Array.
In C++ arrays are used to store such items whose data type is same.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index, which is
used to identify the element.
Array Representation
Arrays can be declared in various ways in
different languages. For illustration,
let's take C array declaration.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
As per the above illustration, following are the important points to be considered.
1. Index starts with 0.
2. Array length is 10 which means it can store 10 elements.
3. Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 9.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Basic Operations
Following are the basic operations supported by an array.
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
Update − Updates an element at the given index.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Static arrays are allocated memory at compile time and their size is fixed, it means it cannot be
changed latter during program execution.
If we want to change the size of array at program execution time, we will need to declare dynamic
arrays.
A dynamic array is allocated memory using new operator and is used in the same way as a static
array. Dynamic arrays are accessed using pointers.
Dynamic Memory Allocation
Assume you have a program which is used to store information about the employees of some
organization, when this program starts its execution we are not sure that how many number of
employee records will be stored in this program.
Therefore how much memory is required for this program is dependent on the fact that how many
records we will enter when program will be executing.
So memory required by program is to be estimated at program execution time, so we will need a
memory allocation mechanism which is called dynamic memory allocation for this program, which
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Multi Dimensional Arrays
Some time the data which we need to process in our program is more easily
interpretable if represented in the form of a table.
A table is a structure which contains some rows and some columns, on intersection of
each row and a column some data is stored. Examples of such data representations may
be a parcel weight-location chart used by some post office; a public transport rent chart
and representation of a digital image etc. So we may easily understand this data
representation if we use a two dimensional array to represent it (remember a two
dimensional array is a special case of a multi dimensional array).
Some time data to be represented is based on more than two dimensions, for example if
we need to represent the sales of some departmental stores by product, year of sale and
location wise, it can be easily represented by a three dimensional structure, which may
be logically represented as a cube. To represent such data we will use three dimensional
arrays.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
An array named “Numbers” containing five elements of type integer (int) can be defined
as:
int Numbers [5];
Figure 1 represents the logical representation of this array, which shows elements of the
array are located consecutively:
We may initialize a two dimensional array of type int by values 0 by using following statement.
Remember that we have to specify the size of array in terms of rows and columns for proper
initialization. 0 1 2 3 5
int numbers[3][5] = { 0 }; 0 0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Two dimensional arrays can be created and accessed using pointers to pointers.
Following code segment provides information that such array usage.
int **a = new int*[height];
for(int i=0; i < height; i++ )
a[i] = new int[width];
The above code segment declares an array of integer pointers to integer pointers. The
first thing we do is then make a point to an array of integer pointers. Then we have to
loop through all of those pointers and assign them to an array of ints of size width.
What this does is create an array in the size of Height x Width.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Three Dimensional Arrays in C++
Multi dimensional arrays may be larger than two dimensions, arrays which have 3 dimensions
are called multi dimensional arrays, they can be logical represented by a cube shape. An array
representing product sales for departmental stores at five cities/locations, 10 products and
containing sale information for 7 years can be declared as:
float productSales [5][10][7];
Where first subscript represents the location/city number, second subscript represents the
product number and third subscript represents the year number for which product sale
information will be stored in this array.
Three dimensional arrays cannot be easily initialized as compared to two dimensional arrays
initialization, so normally they are initialized by zero/blank value and then further initialization
is performed by using nested loops.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
TASK 01: TASK 02:
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Traverse Operation
This operation is to traverse through the elements of an array.
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of array.
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all
elements of an array.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Search Operation
You can perform a search for an array element based on its value or its index.
Update Operation
Update operation refers to updating an existing element from the array at a given index.