it is about data structures and algorithms. this ppt has all data structures like linkedlist, trees, graph,it is about data structures and algorithms. this ppt has all data structures like linkedlist, trees, graph,it is about data structures and algorithms. this ppt has all data structures like linkedlist, trees, graph,
2. Data Structures
Introduction: Data Structure is a way to store and organize data so
that it can be used efficiently. A data structure is a specialized
format for organizing, processing, retrieving and storing data. There
are several basic and advanced types of data structures, all designed
to arrange data to suit a specific purpose. Data structures make it
easy for users to access and work with the data they need in
appropriate ways. Most importantly, data structures frame the
organization of information so that machines and humans can better
understand it.
The data structure name indicates itself that organizing the
data in memory. There are many ways of organizing the data in the
memory as we have already seen one of the data structures, i.e.,
array in C language. This organization of data is done with the help
of an array of data structures. There are also other ways to organize
the data in memory.
4. There are two types of data structures:
◦ Primitive data structure
◦ Non-primitive data structure
1. Primitive Data structure
The primitive data structures are primitive data
types. The int, char, float, double, and pointer are the
primitive data structures that can hold a single value.
2. Non-Primitive Data structure
The non-primitive data structure is divided into two
types:
a. Linear data structure
❑ b. Non-linear data structure
5. � Linear Data Structure
The arrangement of data in a sequential manner is known as a
linear data structure. The data structures used for this purpose are
Arrays, Linked list, Stacks, and Queues. In these data structures,
one element is connected to only one another element in a linear
form.
When one element is connected to the 'n' number of elements
known as a non-linear data structure. The best example is trees and
graphs. In this case, the elements are arranged in a random manner.
We will discuss the above data structures in brief in the
coming topics. Now, we will see the common operations that we
can perform on these data structures.
The types of linear data structures are Array, Queue, Stack,
Linked List.
6. � Array: An array consists of data elements of a same data type.
� Stack: It is linear data structure that uses the LIFO (Last In-First
Out) rule in which the data added last will be removed first. The
addition of data element in a stack is known as a push operation,
and the deletion of data element form the list is known as pop
operation.
� Queue: It is a data structure that uses the FIFO rule (First In-First
Out). In this rule, the element which is added first will be
removed first. There are two terms used in the queue front end
and rear The insertion operation performed at the back end is
known ad enqueue, and the deletion operation performed at the
front end is known as dequeue.
� Linked list: It is a collection of nodes that are made up of two
parts, i.e., data element and reference to the next node in the
sequence.
7. � Non – Linear Data Structure:
A non-linear data structure is also another type of data
structure in which the data elements are not arranged in a
contiguous manner. As the arrangement is non-sequential, so the
data elements cannot be traversed or accessed in a single run. In the
case of linear data structure, element is connected to two elements
(previous and the next element), whereas, in the non-linear data
structure, an element can be connected to more than two elements.
Trees and Graphs are the types of non-linear data structure.
� Tree
It is a non-linear data structure that consists of various linked
nodes. It has a hierarchical tree structure that forms a parent-child
relationship.
� Graph
A graph is a non-linear data structure that has a finite number
of vertices and edges, and these edges are used to connect the
vertices. The vertices are used to store the data elements, while the
edges represent the relationship between the vertices.
8. � Data structures can also be classified as:
� 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.
� 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.
9. � Operations on data structure
� 1) Traversing: Every data structure contains the set of data
elements. Traversing the data structure means visiting each
element of the data structure in order to perform some specific
operation like searching or sorting.
� 2) Insertion: Insertion can be defined as the process of adding
the elements to the data structure at any location.
If the size of data structure is n then we can only
insert n-1 data elements into it.
� 3) Deletion: The process of removing an element from the data
structure is called Deletion. We can delete an element from the
data structure at any random location.
If we try to delete an element from an empty data
structure then underflow occurs.
10. � 4) Searching: The process of finding the location of an
element within the data structure is called Searching. There are
two algorithms to perform searching, Linear Search and Binary
Search. We will discuss each one of them later in this tutorial.
� 5) Sorting: The process of arranging the data structure in a
specific order is known as Sorting. There are many algorithms
that can be used to perform sorting, for example, insertion sort,
selection sort, bubble sort, etc.
� 6) Merging: When two lists List A and List B of size M and N
respectively, of similar type of elements, clubbed or joined to
produce the third list, List C of size (M+N), then this process is
called merging
11. � Characteristics of data structures
Data structures are often classified by their characteristics. The
following three characteristics are examples:
� Linear or non-linear. This characteristic describes whether the
data items are arranged in sequential order, such as with an array,
or in an unordered sequence, such as with a graph.
� Homogeneous or heterogeneous. This characteristic describes
whether all data items in a given repository are of the same type.
One example is a collection of elements in an array, or of various
types, such as an abstract data type defined as a structure in C or a
class specification in Java.
� Static or Dynamic: This characteristic describes how the data
structures are compiled. Static data structures have fixed sizes,
structures and memory locations at compile time. Dynamic data
structures have sizes, structures and memory locations that can
shrink or expand, depending on the use.
12. � Abstract Data Type:
Abstract Data type (ADT) is a type (or class) for objects
whose behaviour is defined by a set of value 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.
13. Various operations on ADT are:
� Stack −
◦ isFull(), This is used to check whether stack is full or not
◦ isEmpty(), This is used to check whether stack is empty or not
◦ push(x), This is used to push x into the stack
◦ pop(), This is used to delete one element from top of the stack
◦ top(), This is used to get the top most element of the stack
◦ size(), this function is used to get number of elements present
into the stack
14. � Queue −
◦ isFull(), This is used to check whether queue is full or not
◦ isEmpty(), This is used to check whether queue is empty or not
◦ insert(x), This is used to add x into the queue at the rear end
◦ delete(), This is used to delete one element from the front end
of the queue
◦ size(), this function is used to get number of elements present
into the queue
� List −
◦ size(), this function is used to get number of elements present
into the list
◦ insert(x), this function is used to insert one element into the list
◦ remove(x), this function is used to remove given element from
the list
◦ get(i), this function is used to get element at position i
◦ replace(x, y), this function is used to replace x with y value.
15. � Preliminaries of 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.
� Space Complexity:
Space complexity of an algorithm represents the amount of
memory space required by the algorithm in its life cycle. The space
required by an algorithm is equal to the sum of the following two
components −
A fixed part that is a space required to store certain data and
variables, that are independent of the size of the problem. For
example, simple variables and constants used, program size, etc.
A variable part is a space required by variables, whose size
depends on the size of the problem. For example, dynamic memory
allocation, recursion stack space, etc.
Space complexity S(P) of any algorithm P is
S(P) = C + SP(I),
where C is the fixed part and S(I) is the variable part of the
algorithm, which depends on instance characteristic I.
16. � Time Complexity:
Time complexity of an algorithm represents the amount of
time required by the algorithm to run to completion. Time
requirements can be defined as a numerical function T(n), where
T(n) can be measured as the number of steps, provided each step
consumes constant time.
� 1. Input / Output:
� 2. Effectiveness:
� 3. Definiteness:
� 4. Finiteness:
17. � Searching Techniques:
Searching is an operation or a technique that helps finds the
place of a given element or value in the list. Any search is said to be
successful or unsuccessful depending upon whether the element that is
being searched is found or not. Some of the standard searching
technique that is being followed in the data structure is listed below:
a. Linear Search or Sequential Search
b. Binary Search
c. Fibonacci Search
1. Linear Search or Sequential Search:
This is the simplest method for searching. In this technique of
searching, the element to be found is searched sequentially in the
list. This method can be performed on a sorted or an unsorted list
(usually arrays). In case of a sorted list searching starts from
0th element and continues until the element is found from the list or
the element whose value is greater than (assuming the list is sorted
in ascending order), the value being searched is reached.
18. Algorithm Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
19. Binary Search:
Binary search is a very fast and efficient searching technique.
It requires the list to be in sorted order. In this method, to search an
element you can compare it with the present element at the center of
the list. If it matches, then the search is successful otherwise the list
is divided into two halves: one from the 0th element to the middle
element which is the center element (first half) another from the
center element to the last element (which is the 2nd half) where all
values are greater than the center element
20. Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set low = 0
Set high = n-1
Set flag = 0
while (low <= high ), false goto EXIT.
{
set mid = (low + high ) / 2
if A[midPoint] < x
set low = mid + 1
if A[midPoint] > x
set high = mid - 1
if A[mid] = x
print(Search is Successful);
}
EXIT: Search is Unsuccessful.
21. Fibonacci Search:
The Fibonacci search algorithm is another variant of binary
search based on divide and conquer technique. The binary search as
you may have learned earlier that split the array to be searched,
exactly in the middle recursively to search for the key. Fibonacci
search on the other hand is bit unusual and uses the Fibonacci
sequence or numbers to make a decision tree and search the key.
Like binary search, the Fibonacci search also work on sorted
array (non-decreasing order).
22. Sorting Techniques:
Sorting refers to arranging data in a particular format.
Sorting algorithm specifies the way to arrange data in a
particular order. A Sorting Algorithm is used to rearrange a
given array or list elements according to a comparison
operator on the elements. The comparison operator is used to
decide the new order of element in the respective data
structure.
The Complexity of a Sorting Algorithm:
The complexity of sorting algorithm calculates the
running time of a function in which 'n' number of items are to
be sorted. The choice for which sorting method is suitable for
a problem depends on several dependency configurations for
different problems.
The most noteworthy of these considerations are:
� The length of time spent by the programmer in programming
a specific sorting program.
� Amount of machine time necessary for running the program
� The amount of memory necessary for running the program
23. The Efficiency of Sorting Algorithm:
To get the amount of time required to sort an array of 'n'
elements by a particular method, the normal approach is to
analyze the method to find the number of comparisons (or
exchanges) required by it. Most of the sorting techniques are data
sensitive, and so the metrics for them depends on the order in
which they appear in an input array.
Various sorting techniques are analyzed in various cases
and named these cases as follows:
� Best case
� Worst case
� Average case
Hence, the result of these cases is often a formula giving
the average time required for a particular sort of size 'n.' Most of
the sort methods have time requirements that range from O(nlog
n) to O(n2).
�
24. The various Sorting Techniques used are:
1.Bubble Sort:
Bubble Sort is the simplest sorting algorithm that
works by repeatedly swapping the adjacent elements. Bubble
Sort Algorithm is used to arrange N elements in ascending
order, and for that, you have to begin with 0th element and
compare it with the first element. If the 0th element is found
greater than the 1st element, then the swapping operation will
be performed, i.e., the two values will get interchanged. In
this way, all the elements of the array get compared. The
time complexity of Bubble sort is given as O ( n2 ).
25. Algorithm:
Step 1: Algorithm Bubble_Sort(list)
Step 2: Read the Array list of elements.
Step 3: for ( i=0; i < lim; i++)
Step 4: for ( j=0; j< lim – i - 1; j++)
Step 5: Compare if( list [ j ] > list [ j + 1 ] ),
Step 6: Swap ( list [ j ], list [ j+1 ] )
Step 7: end if
Step 8: end of for i.
Step 9: end of for j.
Step 10: Print the array in sorted format.
Step 11: End of Bubble sort.
26. � Advantage:
� It is the simplest sorting approach.
� Best case complexity is of O(n) [for optimized approach]
while the array is sorted.
� Using optimized approach, it can detect already sorted
array in first pass with time complexity of O(1).
� Stable sort: does not change the relative order of
elements with equal keys.
� Disadvantage:
� Bubble sort is comparatively slower algorithm.
27. 2. Selection Sort:
The selection is a straightforward process of sorting
values. In this method, to sort the data in ascending order,
the 0th element is compared with all other elements. If the
0th element is found to be greater than the compared element,
the two values get interchanged. In this way after the first
iteration, the smallest element is placed at 0th position. The
technique is repeated until the full array gets sorted. The
time complexity of Selection sort is given as O ( n2 ).
28. Algorithm:
Step 1: Algorithm Selection_Sort(list)
Step 2: Read the Array list of elements.
Step 3: for ( i=0; i < lim; i++)
Step 4: for ( j=i+1; j< lim; j++)
Step 5: Compare if( list [ i ] > list [ j ] ),
Step 6: Swap ( list [ i ], list [ j ] )
Step 7: end if
Step 8: end of for i.
Step 9: end of for j.
Step 10: Print the array in sorted format.
Step 11: End
29. � Advantage:
� It can also be used on list structures that make add and
remove efficient, such as a linked list. Just remove the
smallest element of unsorted part and at the end of sorted
part.
� The number of swaps reduced. O(n) swaps in all cases.
� Disadvantage:
� Time complexity in all cases is O(n2), no best case
scenario.
30. 3.Instertion Sort:
As the name suggests, the Insertion Sort inserts each
element of the array to its proper place at the time of reading the
element. It is a very simple sorting method. Its time complexity is
given as O( n2 ).
Algorithm:
Step 1: Algorithm Insertion_Sort(list)
Step 2: Initilise a[ -1 ] = 0.
Step 3: for ( i=0; i< lim; i++)
Step 4: Read an element ‘x’ into the list.
Step 5: Consider j = i - 1
Step 6: repeat ( x < a [ j ] )
Step 7: a [ j + 1 ] = a [ j ]
Step 8: decrement j--, go to step 6.
Step 9: a [ j + 1 ] = x.
Step 10: end of for i.
Step 11: End of Insertion Sort.
31. � Advantage:
� It can be easily computed.
� Best case complexity is of O(n) while the array is
already sorted.
� Number of swaps reduced than bubble sort.
� For smaller values of ‘n’, insertion sort performs
efficiently like other quadratic sorting algorithms.
� Stable sort.
� Adaptive: total number of steps is reduced for partially
sorted array.
� Disadvantage:
� It is generally used when the value of ‘n’ is small.
For larger values of ‘n’, it is inefficient.
32. 4. Quick Sort:
The name "Quick Sort" comes from the fact that, quick
sort is capable of sorting a list of data elements significantly
faster (twice or thrice faster) than any of the common sorting
algorithms. It is one of the most efficient sorting algorithms
and is based on the splitting of an array (partition) into
smaller ones and swapping (exchange) based on the
comparison with 'pivot' element selected. Due to this, quick
sort is also called as "Partition Exchange" sort. Like Merge
sort, Quick sort also falls into the category of divide and
conquer approach of problem-solving methodology.
33. Algorithm Quicksort(i, j)
{
// sorts the array from a[i] through a[j]
If ( i <j) then //if there are more than one element
{
//divide P into two sub-programs K: partition (a, i, j+1);
//Here K denotes the position of the partitioning element
//solve the sub problems Quicksort(i, K-1);
Quicksort(K=1, j);
// There is no need for combining solution
}
}
34. Algorithm Partition (a, left, right)
{
// The element from a[left] through a[right] are rearranged in such a manner that if
initially
// pivot =a[left] then after completion a[j]= pivot, then return. Here j is the position
where
// pivot partition the list into two partitions. Note that a[right]= ¥.
pivot: a[left];
i:= left; j:=right; repeat
{
repeat
i: =i+1;
until (a[i] ≥ pivot); repeat
j: =j-1;
until (a[j] < pivot); if( i<j) then Swap (a, i, j);
}until (i ≥ j); a[left]: = a[j];
a[j]: = pivot;
return j;
}
35. Algorithm Swap (a, i, j)
{
//Example a[i] with a[j] temp:= a[i];
a[i]: = a[j];
a[j]:= temp;
}
Quick Sort
Best Case O(n log n)
Average Case O(n log n)
Worst Case O(n2)
36. 5. Merge Sort:
Merge sort follows divide and conquer approach in which,
the list is first divided into the sets of equal elements and then
each half of the list is sorted by using merge sort. The sorted list
is combined again to form an elementary sorted array.
Consider an array A of n number of elements. The
algorithm processes the elements in 3 steps.
If A Contains 0 or 1 elements then it is already sorted,
otherwise, Divide A into two sub-array of equal number of
elements.
Conquer means sort the two sub-arrays recursively using
the merge sort.
Combine the sub-arrays to form a single final sorted array
maintaining the ordering of the array.
The main idea behind merge sort is that, the short list takes
less time to be sorted.
The Time Complexity of a Merge sort is given as O ( n log
n).
37. Algorithm-(Divide algorithm)
Algorithm Divide (a, low, high)
{
// a is an array, low is the starting index and high is the end
index of a
If( low < high) then
{
Mid: = (low + high) /2;
Divide( a, low, mid);
Divide( a, mid +1, high); Merge(a, low, mid, high);
}
}
38. Algorithm Merge( a, low, mid, high)
{
L:= low;
H:= high;
J:= mid +1;
K:= low;
While (low <= mid AND j <= high) do
{
If (a[low < a[j]) then
{
B[k] = a[low]; K:= k+1;
Low:= low+1;
}
Else
{
B[k]= a[j];
K: = k+1;
J: = j+1;
}
}
39. While (low <= mid) do
{
B[k]=a[low]; K: = k+1;
Low: =low + 1;
}
While (j <= high) do
{
B[k]=a[j];
K: = k+1;
j: =j + 1;
}
//copy elements of b to a
For i: = l to n do
{
A[i]: =b[i];
}
}
40. 6. Radix Sort:
Radix sort processes the elements the same way in which the
names of the students are sorted according to their alphabetical
order. In the case of integers, radix sort sorts the numbers according
to their digits. The comparisons are made among the digits of the
number from LSB to MSB. The number of passes depend upon the
length of the number with the most number of digits.
41. Example:
Consider the array of length 6 given below.
Sort the array by using Radix sort.
A = {10, 2, 901, 803, 1024}
Pass 1: (Sort the list according to the digits at 0's place)
10, 901, 2, 803, 1024.
Pass 2: (Sort the list according to the digits at 10's place)
901, 02, 803, 10, 1024
Pass 3: (Sort the list according to the digits at 100's place)
002, 010, 1024, 803, 901.
Pass 4: (Sort the list according to the digits at 1000's place)
0002, 0010, 0803, 0901, 1024
Therefore, the list generated in the step 4 is the sorted
list, arranged from radix sort.
42. The Radix sort algorithm is performed using the following
steps...
Step 1 - Define 10 queues each representing a bucket for
each digit from 0 to 9.
Step 2 - Consider the least significant digit of each number
in the list which is to be sorted.
Step 3 - Insert each number into their respective queue based
on the least significant digit.
Step 4 - Group all the numbers from queue 0 to queue 9 in
the order they have inserted into their respective queues.
Step 5 - Repeat from step 3 based on the next least
significant digit.
Step 6 - Repeat from step 2 until all the numbers are
grouped based on the most significant digit.
43. Algorithm
Step 1:Find the largest number in ARR as LARGE
Step 2: [INITIALIZE] SET NOP = Number of digits
in LARGE
Step 3: SET PASS =0
Step 4: Repeat Step 5 while PASS <= NOP-1
Step 5: SET I = 0 and INITIALIZE buckets
Step 6:Repeat Steps 7 to 9 while I<n-1< li=""></n-
1<>
Step 7: SET DIGIT = digit at PASSth place in A[I]
Step 8: Add A[I] to the bucket numbered DIGIT
Step 9: INCREMENT bucket count for bucket
numbered DIGIT
[END OF LOOP]
Step 10: Collect the numbers in the bucket
[END OF LOOP]
Step 11: END