Intro of DS
Intro of DS
Presented By:-
Dr Suraj Srivastava
Data Structure
• A Data Structure is a particular way of
organizing and storing data in a computer so
that it can be accessed and modified
efficiently.
Data Structure
• In Computer Science, A data structures is a data organization, management , and storage
format that enables efficient access and modification.
• In simple words, Data Structure (DS) is the systematization of data in memory. Under this,
any raw data is converted into some information. It serves to organize the data in a
sequence. The meaning of data structure is to save different types and groups of data in a
systematic way in the computer, that is, to manage it. You can also call it a type of
mathematical model, using which data is saved, so that it can be easily accessed when the
data is in a systematic form.
• Data Structure is the organized form of an Algorithm that works to organize a data. With
the help of this, we can define any information in its meaning or we can divide that
information according to some parameters.
Continue..
• Data structure is a way to store and organize data in a computer system.
So that we can use the data easily . That is, data is stored and organized
in such a way that it can be easily accessed at any time later.
• Data Structure is not a programming language like C, C++, Java, but it
is a set of algorithms that we use to structure data in programming
languages.
• Data structure is a main part of many computer science algorithms by
which programmers can handle data properly. It plays a very important
role in improving the performance of the program or software .
Why DS?
• This makes the processing of data very easy.
• If the programmer uses the right data structure, then he saves a
lot of his time and along with this he is able to save storage and
processing time as well.
• The data structure that is specified by an abstract data type
(ADT) provides abstraction. Due to which the client cannot see
the internal working of the data structure, so it does not need to
worry about the working part. The client can only see the
interface.
• Data structure provides reusability which means that many
clients can use the data structure.
Operation of DS
• Searching - The process of finding an element is called searching. There are
two algorithms to complete the search, first binary search and second linear
search.
• Sorting - The process of arranging the data structure in a particular order is
called sorting. There are many algorithms to perform sorting such as -
insertion sort, selection sort, bubble sort, radix sort etc.
• Insertion - The process of adding elements to a location is called insertion. If
the size of a data structure is n, then we can insert only n-1 elements in it.
• Deletion - The process of removing an element is called deletion. We can
delete data from any location.
• Traversing - Traversing means traversing each element of the data structure
to perform a particular task.
• Merging - Merging is the process of joining two lists containing similar data
elements together. From which we get a third list.
Explain what are the primary uses of
data structures?
Data structure is a way to store and organize
data in a computer system so that we can use the
data easily, that is, data is stored and organized
in such a way. that after that it can be easily
accessed at any time
Types of DS
• Data structures are closely linked with
primitive and non-primitive data types. Data
structures provide a way for programmers to
organize and manage data efficiently, by
choosing the appropriate data structure for the
task at hand. This can help optimize the code
and improve the performance of the
application.
Continue..
Continue..
Primitive Data Type
Primitive Data Type
These are basic data types that are built into a programming language and are generally considered the most basic data types. They
are called primitive because they are not composed of any other data types. Examples of primitive data types include integers, float,
characters, and boolean values.
Integer: It is a whole number that can be positive, negative, or zero. These are represented by the "int" data type.
Example of primitive data type:
C++:
int num = 10;
Float: It is a number with a decimal point that can be positive, negative, or zero. Floats are represented by the "float" or "double"
data type.
Example:
C++:
float num = 3.14;
Character: It is a single symbol, letter, or digit. These are represented by the "char" data type.
Example:
C++:
char letter = 'a';
Boolean: It is a data type that can have one of two values: true or false. These are represented by the "bool" data type.
Example:
C++:
bool isTrue = true;
Non-Primitive Data Type
• These are more complex data types that are composed of primitive data types or
other non-primitive data types. They are also referred to as composite data types or
reference data types. Examples of non-primitive data types include arrays, stacks,
queues, and trees.
Here, are some examples of non-primitive data types:
• Array: An array is a collection of elements of the same data type stored in
contiguous memory locations. Elements in an array can be accessed using their
index value.
5. It is Immutable 5. It is mutable
• Time Complexity:
• Best Case: In the best case, the key might be present
at the first index. So the best case complexity is O(1)
• Worst Case: In the worst case, the key might be
present at the last index i.e., opposite to the end from
which the search has started in the list. So the worst-
case complexity is O(N) where N is the size of the list.
• Average Case: O(N)
• Auxiliary Space: O(1) as except for the variable to
iterate through the list, no other variable is used.
• Advantages of Linear Search:
• Linear search can be used irrespective of whether the array is sorted or not. It
can be used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.
• Drawbacks of Linear Search:
• Linear search has a time complexity of O(N), which in turn makes it slow for
large datasets.
• Not suitable for large arrays.
• When to use Linear Search?
• When we are dealing with a small dataset.
• When you are searching for a dataset stored in contiguous memory.
Binary Search
• In this algorithm,
• Divide the search space into two halves by finding the middle index “mid”.
• Compare the middle element of the search space with the key.
• If the key is found at middle element, the process is terminated.
• If the key is not found at middle element, choose which half will be used as the next search space.
– If the key is smaller than the middle element, then the left side is used for next search.
– If the key is larger than the middle element, then the right side is used for next search.
• This process is continued until the key is found or the total search space is exhausted.
•
Algorithm:
• B SEARCH(A,N,item)
1- LOC=-1
2-B=1,E=N
3-while B<=E
4- mid=[(B+E)/2]
5-if item=A[mid] then
Loc=mid[exit loop]
Else if item>A[mid]
B=mid+1
Else
E=mid-1
6- Return Loc
Complexity Analysis of Binary Search:
• Time Complexity:
– Best Case: O(1)
– Average Case: O(log N)
– Worst Case: O(log N)
• Auxiliary Space: O(1), If the recursive call
stack is considered then the auxiliary space
will be O(logN).
• Advantages of Binary Search:
• Binary search is faster than linear search, especially for large arrays.
• More efficient than other searching algorithms with a similar time
complexity, such as interpolation search or exponential search.
• Binary search is well-suited for searching large datasets that are
stored in external memory, such as on a hard drive or in the cloud.
• Drawbacks of Binary Search:
• The array should be sorted.
• Binary search requires that the data structure being searched be
stored in contiguous memory locations.
• Binary search requires that the elements of the array be
comparable, meaning that they must be able to be ordered.
• Applications of Binary Search:
• Binary search can be used as a building block for
more complex algorithms used in machine learning,
such as algorithms for training neural networks or
finding the optimal hyperparameters for a model.
• It can be used for searching in computer graphics
such as algorithms for ray tracing or texture
mapping.
• It can be used for searching a database.