Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views11 pages

Practical 1 - Data Structure - 090044

The document provides an overview of data structures and algorithms, emphasizing their importance in organizing and efficiently managing data. It discusses various types of data structures, including linear and non-linear structures, as well as specific data structures in Python, such as lists, tuples, and dictionaries. Additionally, it covers basic operations on arrays and two-dimensional arrays, along with their implementation in Python.

Uploaded by

Figopec Edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Practical 1 - Data Structure - 090044

The document provides an overview of data structures and algorithms, emphasizing their importance in organizing and efficiently managing data. It discusses various types of data structures, including linear and non-linear structures, as well as specific data structures in Python, such as lists, tuples, and dictionaries. Additionally, it covers basic operations on arrays and two-dimensional arrays, along with their implementation in Python.

Uploaded by

Figopec Edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical 1

Data Structures & Algorithm


What are Data Structures?
Data Structures are used to organize data and store data to use it in an effective way when performing
data operations.

Data structures are fundamental concepts of computer science, which helps in writing efficient programs
in any language.

Characteristics of data structures

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.

Why do we need data structures?

As applications are getting complex and data rich, there are three common problems that applications
face now-a-days. 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(106 ) items every time slowing down
the search. As data grows, search will become slower. Processor Speed − Processor speed although
being very high, falls limited if the data grows to billion records. Multiple Requests − As thousands of
users can search data simultaneously on a web server, even the fast server fails while searching the data.
To solve the above-mentioned problems, data structures come to rescue. Data can be organized in a data
structure in such a way that all items may not be required to be searched, and the required data can be
searched almost instantly.

Execution Time Cases

There are three cases which are usually used to compare various data structure's execution time in a
relative manner. Worst Case − This is the scenario where a particular data structure operation takes
maximum time it can take. If an operation's worst case time is ƒ(n) then this operation will not take more
than ƒ(n) time, where ƒ(n) represents function of n.

Average Case − This is the scenario depicting the average execution time of an operation of a data
structure. If an operation takes ƒ(n) time in execution, then m operations will take mƒ(n) time.

Best Case − This is the scenario depicting the least possible execution time of an operation of a data
structure. If an operation takes ƒ(n) time in execution, then the actual operation may take time as the
random number which would be maximum as ƒ(n).

Basics of 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. 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 Algorithms

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.

Example: Write an algorithm to add two numbers and display 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 step

7 – STOP

Algorithm’s 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.

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.

General Data Structures


The various data structures in computer science are divided broadly into two categories.

Linear Data Structures These are the data structures which store the data elements in a sequential
manner.

 Array: It is a sequential arrangement of data elements paired with the index of the data element.
 Linked List: Each data element contains a link to another element, along with the data present in
it. Stack: It is a data structure, which follows only to specific order of operation. LIFO (last in First
Out) or FILO(First in Last Out).
 Queue: It is similar to Stack, but the order of operation is only FIFO (First In First Out).
 Matrix: It is two dimensional data structure in which, the data element is referred by a pair of
indices.

Non-Linear Data Structures These are the data structures in which, there is no sequential linking of data
elements. Any pair or group of data elements can be linked to each other and can be accessed without a
strict sequence.

 Binary Tree: It is a data structure, where each data element can be connected to maximum two
other data elements and it starts with a root node.
 Heap: It is a special case of Tree data structure, where the data in the parent node is either
strictly greater than/ equal to the child nodes or strictly less than its child nodes.
 Hash Table: It is a data structure, which is made of arrays associated with each other using a
hash function. It retrieves values using keys rather than, index from a data element.
 Graph: It is an arrangement of vertices and nodes, where some of the nodes are connected to
each other through links.

Python Specific Data Structures

Python is a high-level, interpreted, interactive and object-oriented scripting language. Hence, by using
python language, we can study the fundamentals of data structure in a simpler way as compared to
other programming languages.

There are some data strictures specific to python language and they give greater flexibility in storing
different types of data and faster processing in python environment.
List: It is similar to array with the exception, that the data elements can be of different data types. You
can have both numeric and string data in a python list.

Tuple: Tuples are similar to lists, but they are immutable which means, the values in a tuple cannot be
modified they can only be read.

Dictionary: The dictionary contains Key-value pairs as its data elements.

Data Structure - 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. The important terms to
understand the concept of Array are as follows:

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.

The basic operations supported by an array are as stated below:

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.

How to create array in Python

Array is created in Python by importing array module to the python program. Then, the array is declared
as shown below:

from array import *

arrayName = array (typecode, [initializers])

Typecode are the codes that are used to define the type of value the array will hold. Some common
typecodes used are as follows:

b Represents signed integer of size 1 byte/td>

B Represents unsigned integer of size 1 byte

c - Represents character of size 1 byte

i - Represents signed integer of size 2 bytes

I - Represents unsigned integer of size 2 bytes

f - Represents floating point of size 4 byte


d - Represents floating point of size 8 bytes

Example: An array contains the ages of some students in ND I computer science, Write python codes to
implement basic operations of array on the ages

1) Traversing an array

OUTPUT

2) Accessing Array Elements


We can access the elements of an array using its index. The code below shows how elements of
an array can be accessed

OUTPUT

The first print statement outputs the element of the array with index 0 and the second print
statement outputs the element at index 3.

3) 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. In this example, we add a data
element of the ages array created above at the middle of the array, using the python in-built insert()
method.

OUTPUT

4) Deletion Operation

Deletion refers to removing an existing element from the array and re-organising all elements of an array.
Here, we remove a data element at the middle of the array, using the python in-built remove() method.

OUTPUT

5) Update Operation

Update operation refers to updating an existing element from the array at a given index. Here, we simply
reassign a new value to the desired index we want to update. Using the ages array example, let try
updating the value of the lement at index 3.
OUTPUT

24 has changed to 50.

Practical 2

2 – Dimensional Arrays

Two dimensional array is an array within an array. It is an array of arrays. In this type of array, the
position of a data element is referred by two indices instead of one. So, it represents a table with rows
and columns of data.

In the below example of a two dimensional array, observe that each array element itself is also an array.
Consider an example of recording temperatures four times a day, every day. Sometimes, the recording
instrument may be faulty and we fail to record data. Such data for four days can be presented as a two
dimensional array as below.

Day 1 - 11 12 5 2

Day 2 - 15 6 10

Day 3 - 10 8 12 5

Day 4 - 12 15 8 6

To represent this data using python array,

T = [[11, 12, 5, 2], [15, 6, 10], [10, 8, 12, 5], [12, 15, 8, 6]]

Accessing values in a 2 – Dimensional Array

The data elements in two dimensional arrays can be accessed using two indices. One index referring to
the main or parent array and another index referring to the position of the data element in the inner
array. If we mention only one index, then the entire inner array is printed for that index position. The
example below illustrates how it work

OUTPUT

To print out the entire two dimensional array, we can use python for loop as shown below. We use end
of line to print out the values in different rows.

OUTPUT
Inserting Values in a 2 – Dimensional Array

We can insert new data elements at specific position, by using the insert() method and specifying the
index. In an example mentioned below, a new data element is inserted at index position 2.

OUTPUT

Updating Values in a 2 – Dimensional Array

We can update the entire inner array or some specific data elements of the inner array, by reassigning
the values using the array index.

In the example below, we change the content of temp[2] and that of temp[3]
OUTPUT

Deleting Values form a 2 – Dimensional Array

We can delete an entire inner array or some specific data elements of the inner array, by reassigning the
values using the del() method with index. But, in case you need to remove specific data elements in one
of the inner arrays, then use the update process described above.
OUTPUT

You might also like