Data structures are methods for organizing and storing data to enable efficient operations, characterized by correctness, time complexity, and space complexity. Algorithms, defined as step-by-step procedures for problem-solving, are essential in programming and can be categorized into various types, including search, sort, insert, update, and delete. Data structures are divided into linear (e.g., arrays, stacks, queues, linked lists) and non-linear (e.g., graphs, trees) types, each with its own advantages and use cases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
21 views22 pages
Introduction To DSA
Data structures are methods for organizing and storing data to enable efficient operations, characterized by correctness, time complexity, and space complexity. Algorithms, defined as step-by-step procedures for problem-solving, are essential in programming and can be categorized into various types, including search, sort, insert, update, and delete. Data structures are divided into linear (e.g., arrays, stacks, queues, linked lists) and non-linear (e.g., graphs, trees) types, each with its own advantages and use cases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 22
Data Structures
Elem aed aieIntroduction
Data Structure
+ Data Structure is a way of collecting and organizing data in such a way that we can
perform operations on these data in an effective way. Data Structures is about rendering
data elements in terms of some relationship, for better organization and storage,
* 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.Need for Data Structure
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 | 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 instantlyWheat is an Algorithm?
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.
In computer programming terms, an algorithm is a set of well-defined instructions to solve a
particular problem, It takes a set of input(s) and produces the desired output.
For example:
An algorithm to add two numbers:
* Take two number inputs
+ Add numbers using the + operator
+ Display the resultLet us consider the problem of preparing an omelette. To prepare an omelette, we follow the steps
given below:
1) Get the frying pan.
2) Get the oil.
a, Do we have oil?
If yes, put it in the pan.
Ifno, do we want to buy oil?
* Ifyes, then go out and buy.
* Ifo, we can terminate.
3)Turn on the stove, ete...
What we are doing is, for a given problem (preparing an omelette), we are providing a step-by step
procedure for solving it. The formal definition of an algorithm can be stated as: An algorithm is the
step-by-step unambiguous instructions to solve a given problem,Qualities of a Good Algorithm
Input and output should be defined precisely.
Each step in the algorithm should be clear and unambiguous.
Algorithms should be most effective among many ways to solve a problem.
An algorithm shouldn't include computer code. Instead, the algorithm should be written in
such a way that it can be used in different programming languages,
ene.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 an Algorithm
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 | 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.Algorithm 1: Add two numbers entered by the user
Somes Tr)
Step 2: Declare variables num1, num2 and sum.
ASCH CHC R (ep DRT BNL
Step 4: Add num] and num2 and assign the result to sum.
ESTE LU etn
Senay O Sng
Step 6: StopAlgorithm 2: Find the largest number among three numbers
Step 1: Start
Seer
Step 3: Read variables a,b and c
Newt
eas
DRT a een
rad
Display cis the largest number
Else
Tees
Denese scs
Da accaA good algorithm maintains a level of correctness while being efficient.
Meaning, there is little error, and it doesn’t take much time to complete.
Another important component is comprehensibility. We wouldn’t be able
to use algorithms so frequently if they couldn’t be understood.
Algorithmic and computational thinking is so pervasive that it governs the
simplest things in our daily lives. Here are some examples of algorithms
you interact with everyday.Recipes
Just like sorting papers and even tying your shoes, following a recipe is a type of
algorithm. The goal of course being to create a duplicated outcome. In order to
complete a recipe, you must follow a given set of steps. Say you are making bread.
You need flour, yeast and water. After you have your ingredients, you need to combine
them in a certain way that will create a predictable outcome, in this case a loaf of
bread.Sorting Papers
A simple task and yet it uses algorithmic thinking. When you are sorting
office files or your personal documents you are implementing an
algorithm. In its most basic sense, you are following a set of tasks to
achieve an outcome. The reason why sorting papers is a great example, is
because it shows the variety of tasks and specifications algorithms can
use, For instance, you can sort your files alphabetically, by word count, by
date, and countless others. The goal is to simplify the organizational
process by using small tasks.Types of Data Structure
Basically, data structures are divided into two categories:
* Linear data structure
* Non-linear data structure
Linear data structures
* In linear data structures, the elements are arranged in sequence one after the other. Since
elements are arranged order, they are easy to implement.
* However, when the complexity of the program increases, the linear data structures might
not be the best choice because of operational complexitiesPopular linear data structures are:
1. Array Data Structure
In an array, elements in memory are arranged in continuous memory. All the elements of an array
are of the same type. And the type of elements that can be stored in the form of arrays is determined
by the programming language.
index
An array with each element represented by an index2. Stack Data Structure
In stack data structure, elements are stored in the LIFO principle. That is, the last element stored in
astack will be removed first.
It works just like a pile of plates where the last plate kept on the pie willbe removed firs
_ f
add remove
Ina stack, operations can be performed only from one end (top here).3. Queue Data Structure
Unlike stack, the queue data structure works in the FIFO principle where first element stored inthe
queue will be removed frst
It works just like a queue of people in the ticket counter where first person on the queue will get the
ticket first.
pn,
Ina queue, addition and removal are performed from separate ends,4, Linked List Data Structure
In linked list data structure, data elements are connected through a series of nodes. And each node
contains the data items and address to the next node,
Head—> 1 next> 2 next—> 3 next —>NULL
Alinked listNon-linear data structures
Unlike linear data structures, elements in non-linear data structures are not in any sequence. Instead they
are arranged in a hierarchical manner where one element will be connected to one or more elements.
Non-linear data structures are further divided into graph and tree-based data structures.
1. Graph Data Structure
In graph data structure, each node is called vertex and each vertex is connected to other
vertices through edges.
Graph data structure exampleNon-linear data structures
2. Trees Data Structure
Like a graph, a tree is also a collection of vertices and edges. However, in tree data
structure, here can only be one edge between two vertices.
Tree data structure exampleLinear Vs Non-linear Data Structures
Now that we know about linear and non-linear data structures, let's see the major differences between
‘them.
Non-Linear Data Structures
Ue eee ucesIurerec) The data tems are arrange in non-
Ceres sequential order (hierarchical manne
‘ilthe items are presentonthesingle (UCC erento tera
i layers.
(ence tie P i iain 9) itrequres mutple runs. Tati, we start
is ifwe start from the firstelement,we Feu iee ni uct)
Bree raat srt) possible to travers altho elements ina
De single pass.
Different stuctues uiize memory in
USS uronic cM diferent effcient ways depending on the
eed.
Tee EME
Se USS Example: Tee, Graph, Map
%