Professional Documents
Culture Documents
WEEK – 01
1.1 Design a Data structure for handling contact information records- designing
a solution, implementation (using basic DS).
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.
Data structures can be classified into two main types that are primitive data structures and non-
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.
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.
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.
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.
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.
• Hash tables: A data structure that stores data in an associative manner using a hash
function.
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.
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.
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.
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.
Algorithm:
Step 1: start.
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 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 10: create another tuple inside the function named t2 and store the above read variables in it
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.
Integers
String
List
Tuple
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’.
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).
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.
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.
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.
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.
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.
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.
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.
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 8: stop
Program:
Integer
String
List
Boolean
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.
Quick sort.
Bubble Sort.
Merge sort.
Insertion Sort.
Selection Sort.
Heap Sort.
Radix Sort.
Bucket 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:
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:
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
Step 2:
Step 4:
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
Fourth Iteration
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.
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.
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.
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 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
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 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:
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.
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.
Algorithm:
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:
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.
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 4 behind 1
Place 3 behind 1 and the array is sorted
Program:
Output:
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.