DSA
DSA
A [1] = 10
A [2] = 20
A [3] = 30
A [4] = 40
A [5] = 50
Insertion into
Array
⚫ Insertion: It is used to add a new data item in the given
collection of data items.
E.g.We have linear array A as below:
1 2 3 4 5
10 20 50 30 1
5
New element to be inserted is 100 and location for insertion is
3. So shift the elements from 5th location to 3rd location
downwards by 1 place.And then insert 100 at 3rd location. It is
shown below:
Deletion from Array
⚫ Deletion: It is used to delete an existing data item from
the given
collection of data items.
Searching in
⚫ Arrays
Searching:
in the given
It is used to find out the location of the data item if it exists
3)Compare 20 with 35
20 #35, go to next element.
4)Compare 20 with 20
20 = 20, so 20 is found and its location is 4.
2
Linear
Search
Binary
Search
⚫ The binary search
algorithm can be used with
only sorted list of
elements.
⚫ Binary Search first divides
a large array into two
smaller sub-arrays and
then recursively operate
the sub-arrays.
⚫ Binary Search basically
reduces the search space to
half at each step
Binary
Search
Binary
Search
Searching
Sortin
g
Insertion Sort
⚫ ALGORITHM: Insertion Sort (A, N) A is an
array with N
unsorted elements.
◦ Step 1: for I=1 to N-1
◦ Step 2: J = I
While(J >= 1)
if ( A[J] < A[J-1] ) then
Temp = A[J];
A[J] = A[J-1];
A[J-1] =
Temp;
[End if]
J = J-1
[End of While
loop] [End of For
loop]
Merging from Array
⚫ Merging: It is used to combine the data items of
two sorted
files into single file in the sorted form
1 have
We 2 3 linear
sorted 4 array 5A as below:
6
10 40 50 80 95 10
0
Row-Major Col-Major
Method Method
Advantages of Array:
⚫ It is used to represent multiple data items of same
type by using single name.
⚫ It can be used to implement other data structures
like linked lists, stacks, queues, tree, graphs
etc.
⚫ Two-dimensional arrays are used to represent
matrices.
⚫ Many databases include one-dimensional arrays
whose elements are records.
Disadvantages of Array
⚫ We must know in advance the how many
elements are to be stored in array.
⚫ Array is static structure. It means that array is of
fixed size. The memory which is allocated to
array cannot be increased or decreased.
⚫ Array is fixed size; if we allocate more memory
than requirement then the memory space will
be wasted.
⚫ The elements of array are stored in consecutive
memory locations. So insertion and deletion
are very difficult and time consuming.
Stack
⚫ Stack is a linear data structure which follows a
particular order in which the operations are
performed.
⚫ Insertion of element into stack is called PUSH and
deletion of element from stack is called POP.
⚫ The order may be LIFO(Last In First Out) or
FILO(First In Last Out).
Representation of Stack in Memory
⚫ Thestack can be implemented into
two ways:
4
Queue
⚫ Thequeue can be implemented into
two ways:
◦Using arrays (Static implementation)
◦Using pointer (Dynamic
implementation)
4
Prof. K. 9
Types of Queues
Step 4: Return
Queue Deletion
Operation
(DEQUEUE)
ALGORITHM: DEQUEUE (QUEUE, REAR, FRONT, ITEM)
QUEUE is the array with N elements. FRONT is the pointer that contains
the location of the element to be deleted and REAR contains the location of
the inserted element. ITEM is the element to be inserted.
Step 1: if FRONT = NULL then [Check Whether Queue is
empty] PRINT “QUEUE is Empty or Underflow”
Exit [End
if]
Step 2:
ITEM =
QUEUE[F
RONT]
Step 3: if FRONT = REAR then [if QUEUE has only one
element] FRONT = NULL
REAR = NULL
Step 4: Return
else
Application of Queue
⚫ Simulation
⚫ Various features of Operating system
⚫ Multi-programming platform systems.
⚫ Different types of scheduling
algorithms
⚫ Round robin technique algorithms
⚫ Printer server routines
⚫ Various application software’s is also
based on queue data structure.
Lists
⚫A lists (Linear linked list) can be defined as
a
collection of variable number of data
items called nodes.
⚫ Lists are the most commonly used
non- primitive data structures.
⚫ Each nodes is divided into two parts:
◦ The first part contains the information of the
element.
◦ o The second part contains the memory address
of the next node in the list. Also called Link part.
Lists
⚫ Types of linked lists:
Step 5: link(P2)
link(P1) While NULL
end
Free(P2)
Step 4: Step
PRINT6: STOP
data(p2)
Deleting node from
end
Non-Linear Data
⚫structures
A Non-Linear Data structures is a data structure
in which data item is connected to several
other data items.
⚫ The data items in non-linear data structure
represent hierarchical relationship.
⚫ Each data item is called node.
⚫ The different non-linear data structures
are
◦ Trees
◦ Graphs.