Lecture 1 - Introduction+Array

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

DATA

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.

1. Data Search − Consider an inventory of 1 million(106) items of a store. If the


application is to search an item, it has to search an item in 1 million(10 6) items every
time slowing down the search. As data grows, search will become slower.

2. Processor speed − Processor speed although being very high, falls limited if the data
grows to billion records.

3. Multiple requests − As thousands of users can search data simultaneously on a web


server, even the fast server fails while searching the data.
APPLICATIONS OF DATA STRUCTURE AND ALGORITHMS

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.
APPLICATIONS OF DATA STRUCTURE AND
ALGORITHMS

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

 Fibonacci number series

 Tower of Hanoi

 All pair shortest path by Floyd-Warshall

 Shortest path by Dijkstra

 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:

Figure 1: Logical representation of an array of integer


elements.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Initialization Arrays
When an array is declared in a local scope, if not specified otherwise, its elements are
not initialized by some value by default. Elements of global and static arrays are
automatically initialized by their default values (zeros for numeric data types).
If we want to initialize any global or local array by assigning some values to its
element we can do so. Following example represents the initialization of an array of 5
integer elements.
Example:
int Numbers [5] = { 16, 2, 77, 40, 12071 };
OR Figure 2: An array initialized by values.
int Numbers [] = { 16, 2, 77, 40, 12071 };
Figure 2 represents the above initialized array with values
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Accessing values from an array
We can access value of an element of array in our program as it was a normal variable.
We are allowed to read and modify the values of these elements. Following the previous
examples in which Numbers array has 5 elements and each of those elements was of
type int, the name which we can use to refer to each element is the following:
For example, to store the value 25 in the third element of Numbers, we could write the
following statement:
Numbers [2] = 75;
To pass the value of the third element of Numbers to a variable called a, we could write:
a = Numbers[2];
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Static Arrays
Static arrays are allocated memory at compile time and their size is fixed, it means it
cannot be changed latter during program execution. For example two int arrays are
declared, one initialized and one without initialization.
int a[10];
int b[5] = {8, 20, 25, 9, 14};

Dynamic Memory Allocation in C++


Dynamic memory allocation in C++ is performed using new and delete operators. These
operators can be used with primitive data types (int, float, char etc) as well as with user
defined data types (UDT), such as classes. Following examples illustrate usage of new
and delete operators for primitive as well as user defined data types.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
For int data type, new operator can be used as:
int *pointer = new int;
*pointer = 5;
delete pointer;
Above code will create a pointer variable named “pointer” which is pointing to a
dynamic variable of int type created by new operator.
After the creation of variable a constant value 5 is assigned to this variable using
the pointer variable, because a dynamic variable does not have a name to be
referenced.
When dynamic variable is used and is no more required, we can de allocate its
memory by using delete operator. With delete operator we use name of pointer
variable which is pointing to that dynamic variable required to be de allocated.
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
We may allocate and de allocates int size;
memory space dynamically to an cout<< "Enter size of array: ";
array of integers. Following example cin>> size;
illustrates this concept. int* array = new int[size];
for(int i = 0; i < size; i++)
{
array[i] = i+1;
cout<< array[i] << " ";
}
delete [] array;
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Multi Dimensional Arrays
An array of arrays is called a multi dimensional array. Number of dimensions in a multi
dimensional array is represented by number of subscripts in definition of that array.
A multi dimensional array can represent data from different dimensions point of view.
For example product sales information of departmental store from product, location/city
and year of sale dimensions point of view is an example of multi dimensional array. It can
be logical represented by following figure
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Two Dimensional Arrays in C++
A two dimensional array is a specific case of multi dimensional array in
which two subscripts are involved in definition of array. For example
“Numbers” represents an array of 3 per 5 elements. The way to declare this
array in C++ should be:
int numbers [3][5];
Following figure shows a logical representation of a two dimensional array
“numbers”
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
Indices of first subscripts are logical represented as rows and indices of second subscripts
are logically represented as columns. Now for example the way to reference the second
element vertically and fourth horizontally in an expression would be:
numbers [1][3];
Following figure shows the element in the two dimensional array which is referred
above.

To assign some value to elements of a


two-dimensional array, we simply use
two subscripts:
numbers[1][3] = 7;
DATA STRUCTURES AND
ALGORITHMS - ARRAYS
To initialize a two-dimensional array, it is easiest way to use nested braces, with each set of
numbers representing a row: 0 1 2 3 5
int numbers[3][5] = {{ 1, 2, 3, 4, 5, }, // row 0
0 1 2 3 4 5
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2 1 6 7 8 9 10
}; 2 11 12 13 14 15

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy