DATA STRUCTURE Questions
DATA STRUCTURE Questions
DATA STRUCTURE Questions
Begin
for i = 0 to (n - 1) by 1 do
if (a[i] = item) then
set loc = i
Exit
endif
endfor
set loc = -1
End
b) Binary search
Consider: -
There is a linear array ‘a’ of size ‘n’.
Binary search algorithm is being used to search
an element ‘item’ in this linear array.
If search ends in success, it sets loc to the index
of the element otherwise it sets loc to -1.
Variables beg and end keeps track of the index of
the first and last element of the array or sub
array in which the element is being searched at
that instant.
Variable mid keeps track of the index of the
middle element of that array or sub array in
which the element is being searched at that
instant.
Begin
Set beg = 0
Set end = n-1
Set mid = (beg + end) / 2
while ( (beg <= end) and (a[mid] ≠ item) ) do
if (item < a[mid]) then
Set end = mid - 1
else
Set beg = mid + 1
endif
Set mid = (beg + end) / 2
endwhile
if (beg > end) then
Set loc = -1
else
Set loc = mid
endif
End
c) Bubble sort
The bubble sort algorithm is given below-
d) Insertion sort
Let A be an array with n elements. The insertion sort
algorithm used for sorting is as follows-
Here,
• i = variable to traverse the array A
• key = variable to store the new number to be inserted
into the sorted sub-array
• j = variable to traverse the sorted sub-array
ii. State the time complexity for each of the algorithms in (i).
a. Time Complexity of Linear Search Algorithm is O(n).
Here, n is the number of elements in the linear array.
Output:
iv. Write a program to demonstrate addition and removal of
elements from a linked list with 5 numbers.
Output:
v. Write a program to demonstrate an array list with addition
and removal of elements at a specific position.
Output:
vi. With aid of a diagram demonstrate the two different types of
linked list.
Singly Linked List
It is the most common. Each node has data and a pointer to
the next node.
Enqueue Operation: -
• check if the queue is full
• for the first element, set the value of FRONT to 0
• increase the REAR index by 1
• add the new element in the position pointed to by
REAR
Dequeue Operation: -
• check if the queue is empty
• return the value pointed by FRONT
• increase the FRONT index by 1
• for the last element, reset the values of FRONT and
REAR to -1
viii. With aid of a diagram demonstrate the following types of
queues. a) Simple Queue.
A simple queue is the most basic queue. In this queue, the
enqueue operation takes place at the rear, while the
dequeue operation takes place at the front:
It’s used to switch on and off the lights of the traffic signal
systems. Apart from that, it can be also used in place of a simple
queue in all the applications mentioned above.
c) Priority Queue.
A priority queue is a special kind of queue in which each
item has a predefined priority of service. In this queue, the
enqueue operation takes place at the rear in the order of arrival of
the items, while the dequeue operation takes place at the front
based on the priority of the items.
That is to say that an item with a high priority will be dequeued
before an item with a low priority.
In the case, when two or more items have the same priority, then
they’ll be dequeued in the order of their arrival. Hence, it may or
may not strictly follow the FIFO rule:
II. Generics
You cannot create Arrays of Generic classes of interfaces in Java
so arrays and generics do not go hand in hand making it
impossible to create Generic Array for the one basic reason that
arrays are covariant while generics are invariant. While Array is a
fixed-length data structure, it contains objects of the same class or
primitives of the specific data type. So if you try to store different
data type other than the one specified while creating Array object,
it simply throws “ArrayStoreException”. ArrayList, on the other
hand, does support Generics to ensure type-safety.
III. Primitives
Primitive data types such as int, double, long, and char are not
allowed in ArrayList. It rather holds objects and primitives are not
considered objects in Java. Arrays, on the other hand, can hold
primitives as well as objects in Java because it is one of the most
efficient data structures in Java for storing objects. It’s an
aggregated data type that is designed to hold objects which can
be either of same or different type.
IV. Length
In order to get the length of the Array, the code needs to access
the length attribute because one must know the length to perform
operations on Array. While ArrayList uses size () method to
determine the size of the ArrayList, it is rather different from
determining the length of the Array. The size () method attribute
determines the number of elements in an ArrayList, which in turn
is the capacity of the ArrayList.
V. Implementation
Array is a native programming component in Java that are created
dynamically and they use assignment operator to hold elements,
while ArrayList use add( ) attribute to insert elements. ArrayList is
a class from collection framework in Java which uses a set of
specified methods to access and modify the elements. The size of
an ArrayList can be increased or decreased dynamically. The
elements in an array are stored in contiguous memory location and
its size remains static throughout.