281704lecture Notes 2-Data Structures Vs Algorithms-1718434382117
281704lecture Notes 2-Data Structures Vs Algorithms-1718434382117
LECTURE 2 NOTES
Data structures and algorithms are essential components of computer science. At some point,
all enthusiasts learn about these two important topics. They are distinct but closely related
topics. This interdependence raises an important question: “Which should I learn first, data
structures or algorithms?”
Algorithms are well-defined sets of instructions used to solve problems or complete tasks.
An algorithm is a set of instructions or actions that must be completed in a specific amount of
time to provide a solution to a problem. It is a systematic and logical approach to dealing with
or overcoming a problem through a step-by-step process of starting from scratch and
developing a solution.
It is critical to have a time and space-advanced algorithm, and the best way to illuminate
various types of issues is to use various types of algorithmic techniques. For example,
suppose you decide to try a new recipe, read the instructions, and then follow each step
exactly as written. You will be able to cook your food after following the steps.
In programming, algorithms are used to achieve the desired result. These algorithms are
language-independent, meaning they can be invoked in any language. The output will be the
same as expected. To put it simply, it is a series of operations carried out in a sequential order
to complete a task.
We can see that in order to perform the multiplication task, we must follow the operation step
by step. Any step taken in a different way, or any different step, will not produce the desired
result.
Data structures serve as the foundation for more complex problems. In general, data
structures are physical representations of abstract data types. They are crucial in the design
of efficient software and the implementation of algorithms. In addition to using data
structures, it is critical to select the appropriate data structure for each task. Inappropriate
data structure selection may result in slow runtimes or inefficient code.
The following are some examples of how data structures are used:
■ Storing data: Data structures are used in database management systems to store
records by specifying a collection of attributes and their structures.
■ Managing resources and services: Data structures such as linked lists for memory
allocation, file directory management, and file structure trees, as well as process
scheduling queues, are used to enable core operating system (OS) resources and
services.
■ Data exchange: Data structures, such as TCP/IP packets, organize information that is
shared among applications.
■ Ordering and sorting: Data structures, such as binary search trees, also known as
ordered or sorted binary trees, provide efficient methods of sorting objects. Priority
queues are data structures that organize items based on their priority, for example.
■ Searching: Indexes built with binary search trees, B-trees, or hash tables make it
easier to find a specific item.
■ Scalability: To ensure scalability and performance, big data applications use data
structures to allocate and manage data storage across distributed storage locations.
Note: Data structures and algorithms help in a deeper understanding of the nature of the
problem and, as a result, a better understanding of the world.
Data structures and algorithms are very much interlinked. Now, let us have a look at it.
■ A data structure allows you to store elements in memory and provides functions for
manipulating the stored elements.
■ Some data structures are better suited to solving specific problems than others.
■ We use data structures to implement an algorithm on our computer, which allows you
to store the data that you will use to solve the problem.
For example, if we need to sort a list of numbers, we can use an array data structure to store
the numbers and sort the array using sorting algorithms such as quicksort, insertion sort, and
so on.
If we want to store only unique elements and find any element within it, we can use a set data
structure that stores only unique elements and a binary search algorithm to find an element
within it.
1. We will discover how data structures enable the efficient storage of data in a
storage device.
2. We will also learn how data structures facilitate data retrieval from storage
devices and enable effective and efficient processing of small and large
amounts of data.
3. We will understand how selecting the right data structure can lower the cost of
operations like retrieving or processing data. This saves both programmers' and
users' time and money.
4. We will notice that large amounts of data can be easily modified by selecting
the best-suited data structure.
1. Going deep into data structures alone, without knowledge of algorithms, can
make the concepts difficult to grasp.
3. Learning only about data structures will be ineffective because the best data
structure is not always the most efficient for a given scenario.
1. We will learn how to divide the problem into smaller subproblems that can be
easily converted into a program.
5. Any programmer can easily debug the code and identify the logical errors in
the program.
2. Without a data structure, learning and applying the algorithm will be extremely
difficult.
3. In every scenario, the concept of the most efficient algorithm for a problem will
not be met.
● Which Should You Learn First: Data Structures or Algorithms? The Conclusion
Data structures and algorithms are mutually dependent. We apply algorithms to a well-suited
data structure, and we apply algorithms to the data structure. Furthermore, it is clear from
the definition that data structures organize unstructured data, whereas algorithms are the set
of instructions that a computer follows to complete a specific task.
Algorithms are the platforms on which data structures are applied and tested, and data
structures are the building blocks of algorithms.
With all of the cases presented, and after discussing the pros and cons of each scenario, it is
crucial that you begin learning data structures first, but do not dive deep into it without first
learning algorithms. Data structures and algorithms are both required for the best results, so
they should be studied concurrently.
Integer, float, double, long, and other data types are considered built-in data types, and we
can perform basic operations on them such as addition, subtraction, division, and
multiplication. Now, there may be a time when we need to define operations for our
user-defined data type. These operations can only be defined as and when they are required.
So, to simplify the problem-solving process, we can create data structures and their
operations, and such data structures that are not built-in are referred to as Abstract Data
Types (ADT).
An ADT is a type (or class) of an object whose behavior is defined by a set of values and a set
of operations. The definition of ADT only mentions what operations must be performed, not
how these operations will be carried out. It does not specify how data will be stored in
memory or which algorithms will be used to carry out the operations. It is called "abstract"
because it provides a view that is independent of implementation.
Abstraction is the process of providing only the essentials while hiding the details.
The user of a data type does not need to understand how that data type is implemented; for
example, we have been using primitive values such as int, float, and char data types only with
the knowledge that these data types can operate and be performed on without
understanding how they are implemented.
As a result, a user only needs to understand what a data type can do, not how it will be
implemented. Consider ADT to be a black box that conceals the inner structure and design of
the data type.
1. List ADT
2. Stack ADT
3. Queue ADT
1. List ADT
● The data is typically stored in a key sequence in a list with a head structure that
includes count, pointers, and the address of the compare function required to compare
the data in the list.
● The data node includes a pointer to a data structure and a self-referential pointer to
the next node in the list.
● The following are the List ADT functions:
■ remove() ‒ Remove the first occurrence of any element from a non-empty list.
2. Stack ADT
● Instead of storing data in each node, the pointer to data is stored in the Stack ADT
implementation.
● The program allocates memory for the data and passes the address to the ADT stack.
● The ADT contains the head node and the data nodes. The calling function can only see
the stack pointer.
● The stack head structure also includes a top pointer and a count of the number of
entries currently in the stack.
■ pop() ‒ If the stack is not empty, remove and return the element at the top.
■ peek() ‒ If the stack is not empty, return the element at the top of the stack
without removing it.
3. Queue ADT
● The queue abstract data type (ADT) is designed similarly to the stack abstract data
type.
● Each node has a void pointer to the data and a link pointer to the next queue element.
It is the program's responsibility to allocate memory for data storage.
■ dequeue() ‒ If the queue is not empty, remove and return the first element.
■ peek() ‒ If the queue is not empty, return the element without removing it.
■ Data Abstraction: The user does not need to know how the data structure is
implemented.
We can see from these definitions that they do not specify how these ADTs will be
represented or how the operations will be carried out. An ADT can be implemented in a variety
of ways. For example, the List ADT can be implemented using arrays, a singly linked list, or a
doubly linked list. Similarly, arrays or linked lists can be used to implement stack ADT and
queue ADT.