You are on page 1of 31

DATA STRUCTURES WITH PYTHON

WEEK – 01

1.1 Design a Data structure for handling contact information records- designing
a solution, implementation (using basic DS).

1.1.1 Introduction to Data structures.

In computer science, a data structure is a way of organizing and storing data in a computer program
so that it can be accessed and used efficiently. Data Structure is a way to store and organize data
so that it can be used efficiently. Data structures provide a means of managing large amounts of
data, enabling efficient searching, sorting, insertion, and deletion of data.

The choice of data structure for a particular task depends on the type and amount of data to be
processed, the operations that need to be performed on the data, and the efficiency requirements
of the program.

1.1.2 Classification of data structure:

Data structures can be classified into two main types that are primitive data structures and non-
primitive data structures.

1.1.2.1 Primitive data structures:

Primitive data structure is the data structure that allows you to store only single data type values.
These are the most basic data structures and are usually built into programming languages.
Examples include:

 Integer: You can use an integer represent numeric data, and more specifically, whole
numbers from negative infinity to infinity, like 4, 5, or -1.
 Float: "Float" stands for 'floating point number'. You can use it for rational numbers,
usually ending with a decimal figure, such as 1.11 or 3.14.
 String: In Python, a string is a collection of alphabets, characters, or words. While it’s one
of the most primitive of data structures, it’s also vital to data manipulation. Python has a
built-in string class known as str, which is immutable.
 Boolean: This is one of Python’s built-in data types, and it represents the truth value of an
expression. The Python boolean type has only two possible values: true or false. In other
words, you can evaluate any expression and get either true or false in response.

1.1.2.2 Non-primitive data structures:

Non-Primitive data structure is a data structure that allows you to store multiple data type values.
These are complex data structures that are built using primitive data types. The choice of data
structure depends on the problem to be solved and the operations to be performed on the data.
Different data structures have different strengths and weaknesses and are suitable for different
scenarios. Understanding the different types of data structures and their characteristics is important
for efficient algorithm design and implementation.

 Linear Data Structure


 Non-Linear Data Structure.

1.1.2.2.1 Linear Data Structure:

A linear data structure is a type of data structure in which data elements are arranged in a sequential
order, and each element has a unique predecessor and successor, except for the first and last
elements. Linear data structures are one-dimensional and can be traversed sequentially from the
first to the last element.

Elements are arranged in one dimension, also known as linear dimension.

Linear data structure is further divided into two types, they are:

 Static data structure: It is a type of data structure where the size is allocated at the compile
time. Therefore, the maximum size is fixed.
Example: Array.
Arrays: A collection of elements of the same data type, stored in contiguous memory
locations.
 Dynamic data structure: It is a type of data structure where the size is allocated at the run
time. Therefore, the maximum size is flexible.
Example: lists, stack, queue, etc.
•Linked lists: A collection of elements that are connected by links or pointers.
•Stacks: A collection of elements that follow the Last-In-First-Out (LIFO) principle.
•Queues: A collection of elements that follow the First-In-First-Out (FIFO) principle.

1.1.2.2.2 Non-Linear Data Structure

A Non-linear data structure is a type of data structure in which data elements are not arranged in a
sequential order, and each element may have one or more predecessors and successors. Non-linear
data structures can represent complex relationships between data elements, such as hierarchies,
networks, and graphs.

Elements are arranged in one-many, many-one and many-many dimensions.

Example: tree, graph, table, etc.

• Trees: A hierarchical data structure consisting of nodes connected by edges.

• Graphs: A non-linear data structure consisting of nodes and edges.

• Hash tables: A data structure that stores data in an associative manner using a hash
function.

1.1.3 Characteristics of data structures.

There are three main characteristics:

 Correctness
 Time complexity
 Space complexity

Correctness

This property is related to the algorithm of data structures. It's important that the algorithm is
correct. Correctness here means that the algorithm always produces the expected output or follows
the ground truth for the range of valid inputs, and eventually, it terminates.

Time complexity

The running time or execution time of a data structure as a function of the length of the input is
known as time complexity.
It's a type of computation complexity that describe the time your algorithm needs to be executed.

Space complexity

When an algorithm or data structure runs on your computer, it needs some sort of space/memory.
The total memory space your data structure needs for its execution or to work properly is known
as space complexity.

1.1.4 Operations of a data structures.

The common operations that can be performed on the data structures are as follows:

 Traversal: Traversing a data structure means accessing each data element exactly once so
it can be administered.
 Search: Search is another data structure operation which means to find the location of one
or more data elements that meet certain constraints. Such a data element may or may not
be present in the given set of data elements.
 Insertion: Insertion means inserting or adding new data elements to the collection. For
example, we can use the insertion operation to add the details of a new employee the
company has recently hired.
 Deletion: Deletion means to remove or delete a specific data element from the given list
of data elements.
 Sorting: Sorting means to arrange the data elements in either Ascending or Descending
order depending on the type of application.
 Merge: Merge means to combine data elements of two sorted lists in order to form a single
list of sorted data elements.
 Create: Create is an operation used to reserve memory for the data elements of the
program. We can perform this operation using a declaration statement. The creation of data
structure can take place either during the following:
o Compile-time
o Run-time
 Selection: Selection means selecting a particular data from the available data. We can
select any particular data by specifying conditions inside the loop.
 Update: The Update operation allows us to update or modify the data in the data structure.
We can also update any particular data by specifying some conditions inside the loop, like
the Selection operation.
 Splitting: The Splitting operation allows us to divide data into various subparts decreasing
the overall process completion time.

1.1.5 Some Applications of Data Structures

The following are some applications of Data Structures:

 Data Structures help in the organization of data in a computer's memory.


 Data Structures also help in representing the information in databases.
 Data Structures allows the implementation of algorithms to search through data (For
example, search engine).
 We can use the Data Structures to implement the algorithms to manipulate data (For
example, word processors).
 We can also implement the algorithms to analyse data using Data Structures (For example,
data miners).
 Data Structures support algorithms to generate the data (For example, a random number
generator).
 Data Structures also support algorithms to compress and decompress the data (For
example, a zip utility).
 We can also use Data Structures to implement algorithms to encrypt and decrypt the data
(For example, a security system).
 With the help of Data Structures, we can build software that can manage files and
directories (For example, a file manager).
 We can also develop software that can render graphics using Data Structures. (For example,
a web browser or 3D rendering software).

1.1.6 Advantages of Data structures

The following are the advantages of a data structure:


Efficiency: If the choice of a data structure for implementing a particular ADT is proper, it makes
the program very efficient in terms of time and space.

Reusability: The data structure provides reusability means that multiple client programs can use
the data structure.

Abstraction: The data structure specified by an ADT also provides the level of abstraction. The
client cannot see the internal working of the data structure, so it does not have to worry about the
implementation part. The client can only see the interface.

1.1.7 Disadvantages of data structure

 Some of the data structures are complex to handle for a new programmer.
 Some of the data structures provide slower access to data due to the complex structure.
 In case of complex problems or issues, we may require experts to handle the situation.
 The designing of data structures from scratch is quite complex and may require complex
algorithms and a lot of time along with tests (quality testing).
 Larger applications require the data structures to be used within one another like the node
of a graph is composed of an array or list. So, in such scenarios, the maintenance is quite
complex and costlier.

1.1.8 Data structure for handling contact information

Algorithm:

Step 1: start.

Step 2: initialize a letter n to read how many records to be stored initially.

Step 3: initialize a variable named l1 to store an empty list.

Step 4: using for loop iterate over the number enter in n and read phone number , name, company,
email and group name from the user.

Step 5: create a tuple named t1 and store phone number, name, company, email and group name
in it.

Step 6: append the tuple t1 into the list l1.


Step 7: print l1 to display the list.

Step 8: create a function named insert and define a variable named n1 to read the number of records
the user want to insert inside the function.

Step 9: repeat step 4 inside the insert function

Step 10: create another tuple inside the function named t2 and store the above read variables in it

Step 11: append the append t2 to the list l1

Step 12: define another function named delete and create a variable named n2 and read the name
of the record that needs to be deleted in it.

Step 13: using if statement check whether the entered name exits in the list and delete the record
using remove function.

Step 14: define another function named search and read the name of the record that needs to be
searched and store it in a variable named n3 inside the function.

Step 15: using if statement check whether the searched record exists or not. If existing print the
record else print contact not found.

Step 16: now to call the function, create a variable named numb. Read a number from 1 to 3 and
store it in numb. 1 for insertion, 2 for deletion and 3 for searching.

Step 17: stop

In the following program, we use the following data types:

 Integers
 String
 List
 Tuple

We also use the following operations:

 Insertion
 Deletion
 Search

Program:

 In the first part of the program we are reading the contact info details from the
user/programmer to perform different operations on it.

Here we have variable named ‘n’ to store the number of records that the user wants to store initially,
so while using for loop it can iterate ‘n’ times. ’l1’ is an empty lists that is used to store the records.
‘t1’ is a tuple which consists the variables containing contact information which is appended to
the ‘l1’ list.

 In the second part of the program we are going to perform insertion operation.
Here we have defined a named ‘insert()’ which is used to insert records to the existing list of
records. ‘n1’ is a variable used to read how many records needs to be inserted. Then the contact
info collected gets stored in a tuple named ‘t2’, which then gets appended to the list ‘l1’.

 In this part of the program we are going to perform deletion operation.

Here we have defined a function named ‘delete()’ which is used to delete records from the list of
records. ‘n2’ is a variable which is used to read the name of the record that needs to be deleted. If
the name in the variable ‘n2’ is same as the record in the list, then the record gets removed by
using the remove function.

 In the next part of the program, we are going to perform search operation on the records.

Here we have defined a function named ‘search()’ which is used to search for the specific record
that will be read from the user using the variable ‘n3’. Using for loop we give it a condition that if
the name read from the user is equal to the record in the list, then the record should be displayed.

 In the last part of the program, we are going see a menu driven program which will be used
to ask the whether the user wants to insert, delete or search for a record.

Output:
Week – 02

2.1 Design a data structure for handling contact information records- designing
a solution, implementation (using ADT).

2.1.1 Introduction to abstract data type (ADT).

In computer science, an abstract data type (ADT) is a mathematical model for data types. An
abstract data type is defined by its behavior (semantics) from the point of view of a user, of the
data, specifically in terms of possible values, possible operations on data of this type, and the
behavior of these operations.

Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
values and a set of operations. The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented. It does not specify how data will be
organized in memory and what algorithms will be used for implementing the operations. It is called
“abstract” because it gives an implementation-independent view. The process of providing only
the essentials and hiding the details is known as abstraction.

2.1.2 Features of ADT:

Abstract data types (ADTs) are a way of encapsulating data and operations on that data into a
single unit. Some of the key features of ADTs include:

 Abstraction: The user does not need to know the implementation of the data structure only
essentials are provided.
 Better Conceptualization: ADT gives us a better conceptualization of the real world.
 Robust: The program is robust and has the ability to catch errors.
 Encapsulation: ADTs hide the internal details of the data and provide a public interface for
users to interact with the data. This allows for easier maintenance and modification of the
data structure.
 Data Abstraction: ADTs provide a level of abstraction from the implementation details of
the data. Users only need to know the operations that can be performed on the data, not
how those operations are implemented.
 Data Structure Independence: ADTs can be implemented using different data structures,
such as arrays or linked lists, without affecting the functionality of the ADT.
 Information Hiding: ADTs can protect the integrity of the data by allowing access only to
authorized users and operations. This helps prevent errors and misuse of the data.
 Modularity: ADTs can be combined with other ADTs to form larger, more complex data
structures. This allows for greater flexibility and modularity in programming.

2.1.3 Advantages of ADT:

 Encapsulation: ADTs provide a way to encapsulate data and operations into a single unit,
making it easier to manage and modify the data structure.
 Abstraction: ADTs allow users to work with data structures without having to know the
implementation details, which can simplify programming and reduce errors.
 Data Structure Independence: ADTs can be implemented using different data structures,
which can make it easier to adapt to changing needs and requirements.
 Information Hiding: ADTs can protect the integrity of data by controlling access and
preventing unauthorized modifications.
 Modularity: ADTs can be combined with other ADTs to form more complex data
structures, which can increase flexibility and modularity in programming.

2.1.4 Disadvantages of ADT:

 Overhead: Implementing ADTs can add overhead in terms of memory and processing,
which can affect performance.
 Complexity: ADTs can be complex to implement, especially for large and complex data
structures.
 Learning Curve: Using ADTs requires knowledge of their implementation and usage,
which can take time and effort to learn.
 Limited Flexibility: Some ADTs may be limited in their functionality or may not be
suitable for all types of data structures.
 Cost: Implementing ADTs may require additional resources and investment, which can
increase the cost of development.
2.1.5 Types of ADT:
There are 3 ADT’s namely List ADT, Stack ADT, Queue ADT.

2.1.5.1 List ADT:

Vies of list

The data is generally stored in key sequence in a list which has a head structure consisting of count,
pointers and address of compare function needed to compare the data in the list.

The data node contains the pointer to a data structure and a self-referential pointer which points to
the next node in the list.

The List ADT Functions is given below:

 get() – Return an element from the list at any given position.


 insert() – Insert an element at any position of the list.
 remove() – Remove the first occurrence of any element from a non-empty list.
 removeAt() – Remove the element at a specified location from a non-empty list.
 replace() – Replace an element at any position by another element.
 size() – Return the number of elements in the list.
 isEmpty() – Return true if the list is empty, otherwise return false.
 isFull() – Return true if the list is full, otherwise return false.

2.1.5.2 Stack ADT

View of stack

In Stack ADT Implementation instead of data being stored in each node, the pointer to data is
stored.

The program allocates memory for the data and address is passed to the stack ADT.

The head node and the data nodes are encapsulated in the ADT. The calling function can only see
the pointer to the stack.

The stack head structure also contains a pointer to top and count of number of entries currently in
stack.

 push() – Insert an element at one end of the stack called top.


 pop() – Remove and return the element at the top of the stack, if it is not empty.
 peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
 size() – Return the number of elements in the stack.
 isEmpty() – Return true if the stack is empty, otherwise return false.
 isFull() – Return true if the stack is full, otherwise return false.

2.1.5.3 Queue ADT

View of Queue

The queue abstract data type (ADT) follows the basic design of the stack abstract data type.

Each node contains a void pointer to the data and the link pointer to the next element in the queue.
The program’s responsibility is to allocate memory for storing the data.

 enqueue() – Insert an element at the end of the queue.


 dequeue() – Remove and return the first element of the queue, if the queue is not empty.
 peek() – Return the element of the queue without removing it, if the queue is not empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise return false.
 isFull() – Return true if the queue is full, otherwise return false.

2.1.6 Designing a data structure for handling contact information records.

Algorithm:

Step 1: Start

Step 2: create a list named ‘l1’ and store some records in it to perform different operations on it

Step 3: define 3 functions named insert(), delete() and search() which helps to perform insertion
operation, deletion operation and search operation.

Step 4: save this file as mod1.py

Step5: select a new file and import mod1.py module to it


Step 6: create another variable named numb to read which operations should be performed from
the user.

Step 7: save this file as mod2

Step 8: stop

Program:

Data types used in this program:

 Integer
 String
 List
 Boolean

Operations performed in this program:

 Insertion
 Deletion
 search

Module1:
Module2:

Output:
Week – 03

3.1 Optimize your solution (Bubble sort, selection sort, insertion sort).

3.1.1 Sorting:

Sorting refers to ordering data in an increasing or decreasing fashion according to some linear
relationship among the data items.

Sorting can be done on names, numbers and records. Sorting reduces the For example, it is
relatively easy to look up the phone number of a friend from a telephone dictionary because the
names in the phone book have been sorted into alphabetical order.

This example clearly illustrates one of the main reasons that sorting large quantities of information
is desirable. That is, sorting greatly improves the efficiency of searching. If we were to open a
phone book, and find that the names were not presented in any logical order, it would take an
incredibly long time to look up someone’s phone number.

3.1.2 Types of sorting:

 Quick sort.
 Bubble Sort.
 Merge sort.
 Insertion Sort.
 Selection Sort.
 Heap Sort.
 Radix Sort.
 Bucket Sort.

3.1.3 Bubble sort:

Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until they
are in the intended order. Just like the movement of air bubbles in the water that rise up to the
surface, each element of the array move to the end in each iteration. Therefore, it is called a bubble
sort.
3.1.3.1 Working of bubble sort:

Algorithm:

The bubble sort algorithm works as follows

Step 1: start.

Step 2: Get the total number of elements. Get the total number of items in the given list

Step 3: Determine the number of outer passes (n – 1) to be done. Its length is list minus one

Step 4: Perform inner passes (n – 1) times for outer pass 1. Get the first element value and compare
it with the second value. If the second value is less than the first value, then swap the positions

Step 5: Repeat step 3 passes until you reach the outer pass (n – 1). Get the next element in the list
then repeat the process that was performed in step 3 until all the values have been placed in their
correct ascending order.

Step 6: Return the result when all passes have been done. Return the results of the sorted list

Step 7: stop

Working:

The following image shows the unsorted list

First Iteration

Step 1:
The values 21 and 6 are compared to check which one is greater than the other.

21 is greater than 6, so 21 takes the position occupied by 6 while 6 takes the position that was
occupied by 21

Our modified list now looks like the one above.

Step 2:

The values 21 and 9 are compared.

21 is greater than 9, so we swap the positions of 21 and 9

The new list is now as above


Step 3:

The values 21 and 33 are compared to find the greater one.

The value 33 is greater than 21, so no swapping takes place.

Step 4:

The values 33 and 3 are compared to find the greater one.

The value 33 is greater than 3, so we swap their positions.

The sorted list at the end of the first iteration is like the one above

Second Iteration
The new list after the second iteration is as follows

Third Iteration

The new list after the third iteration is as follows

Fourth Iteration

The new list after the fourth iteration is as follows

3.1.3.2 Program:

Output:
3.1.3.3 Advantages of bubble sort:

 The primary advantage of the bubble sort is that it is popular and easy to implement.
 In the bubble sort, elements are swapped in place without using additional temporary
storage.
 The space requirement is at a minimum.

3.1.3.4 Disadvantages of bubble sort:

 The main disadvantage of the bubble sort is the fact that it does not deal well with a list
containing a huge number of items.
 The bubble sort requires n-squared processing steps for every n number of elements to be
sorted.
 The bubble sort is mostly suitable for academic teaching but not for real-life applications.

3.1.4 Selection sort:

Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each
iteration and places that element at the beginning of the unsorted list.

3.1.4.1 Working of selection sort:

Algorithm:

Step 1: Get the value of n which is the total size of the array

Step 2: Partition the list into sorted and unsorted sections. The sorted section is initially empty
while the unsorted section contains the entire list

Step 3: Pick the minimum value from the unpartitioned section and placed it into the sorted section.

Step 4: Repeat the process (n – 1) times until all of the elements in the list have been sorted.

Working:

The following image shows the unsorted list


Step 1:

The first value 21 is compared with the rest of the values to check if it is the minimum value.

3 is the minimum value, so the positions of 21 and 3 are swapped. The values with a green
background represent the sorted partition of the list.

Step 2:

The value 6 which is the first element in the unsorted partition is compared with the rest of the
values to find out if a lower value exists

The value 6 is the minimum value, so it maintains its position.

Step 3:
The first element of the unsorted list with the value of 9 is compared with the rest of the values to
check if it is the minimum value.

The value 9 is the minimum value, so it maintains its position in the sorted partition.

Step 4:

The value 33 is compared with the rest of the values.

The value 21 is lower than 33, so the positions are swapped to produce the above new list.

Step 5:

We only have one value left in the unpartitioned list. Therefore, it is already sorted.
The final list is like the one shown in the above image.

3.1.4.2 Program:

Output:

3.1.4.3 Advantages of selection sort:

 The main advantage of the selection sort is that it performs well on a small list.
 Because it is an in-place sorting algorithm, no additional temporary storage is required
beyond what is needed to hold the original list.
 Its performance is easily influenced by the initial ordering of the items before the sorting
process.

3.1.4.4 Disadvantages of selection sort:

 The primary disadvantage of the selection sort is its poor efficiency when dealing with a
huge list of items.
 The selection sort requires n-squared number of steps for sorting n elements.
 Quick Sort is much more efficient than selection sort
3.1.5 Insertion sort:

Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each
iteration.

Insertion sort works similarly as we sort cards in our hand in a card game.

We assume that the first card is already sorted then, we select an unsorted card. If the unsorted
card is greater than the card in hand, it is placed on the right otherwise, to the left. In the same way,
other unsorted cards are taken and put in their right place.

3.1.5.1 Working of insertion sort:

Algorithm:

To sort an array of size N in ascending order:

Step 1: Iterate from arr[1] to arr[N] over the array.

Step 2: Compare the current element (key) to its predecessor.

Step 3: If the key element is smaller than its predecessor, compare it to the elements before. Move
the greater elements one position up to make space for the swapped element.

Working:

Suppose we need to sort the following array.

The first element in the array is assumed to be sorted. Take the second element and store it
separately in key.

Compare key with the first element. If the first element is greater than key, then key is placed in
front of the first element.
If the first element is greater than key, then key is placed in front of the first element.

Now, the first two elements are sorted.

Take the third element and compare it with the elements on the left of it. Placed it just behind the
element smaller than it. If there is no element smaller than it, then place it at the beginning of the
array.

Place 1 at the beginning

Similarly, place every unsorted element at its correct position.

Place 4 behind 1
Place 3 behind 1 and the array is sorted

Program:

Output:

3.1.5.2 Advantages of insertion sort:

 The main advantage of the insertion sort is its simplicity.


 It also exhibits a good performance when dealing with a small list.
 The insertion sort is an in-place sorting algorithm so the space requirement is minimal.

3.1.5.3 Disadvantages of insertion sort:

 The disadvantage of the insertion sort is that it does not perform as well as other, better
sorting algorithms
 With n-squared steps required for every n element to be sorted, the insertion sort does not
deal well with a huge list.
 The insertion sort is particularly useful only when sorting a list of few items.

You might also like