6. 4. Delete(index) :-
Linear search:-
• Start from the leftmost element of arr[] and one by
one compare x with each element of arr[]
• If x matches with an element, return the index.
• If x doesn’t match with any of elements, return -1.
• Time complexity is O(n).
7. Improve linear search:-
A linear search or sequential search is a method for finding an
element within a list. It sequentially checks each element of
the list until a match is found or the whole list has been
searched. It is observed that when searching for a key
element, then there is a possibility for searching the same
key element again and again.
The goal is that if the same element is searched again then
the operation must take lesser time. Therefore, in such a
case, Linear Search can be improved by using the following
two methods:
1. Transposition
2. Move to Front
8. Transposition:
In transposition, if the key element is found, it is swapped to the
element an index before to increase in a number of search count for
a particular key, the search operation also optimizes and keep
moving the element to the starting of the array where the searching
time complexity would be of constant time.
For Example: If the array arr[] is {2, 5, 7, 1, 6, 4, 5, 8, 3, 7} and let
the key to be searched is 4, then below are the steps:
• After searching for key 4, the element is found at index 5 of the given array
after 6 comparisons. Now after transposition, the array becomes {2, 5, 7, 1, 4,
6, 5, 8, 3, 7} i.e., the key with value 4 comes at index 4.
• Again after searching for key 4, the element is found at index 4 of the given
array after 6 comparisons. Now after transposition, the array becomes {2, 5, 7,
4, 1, 6, 5, 8, 3, 7} i.e., the key with value 4 comes at index 3.
• The above process will continue until any key reaches the front of the array if
the element to be found is not at the first index
9. Move to Front/Head:
In this method, if the key element is found then it is directly swapped with the
index 0, so that the next consecutive time, search operation for the same key
element is of O(1), i.e., constant time.
For Example: If the array arr[] is {2, 5, 7, 1, 6, 4, 5, 8, 3, 7} and let the key to be
searched is 4, then below are the steps:
• After searching for key 4, the element is found at index 5 of the
given array after 6 comparisons. Now after moving to front
operation, the array becomes {4, 2, 5, 7, 1, 6, 5, 8, 3, 7} i.e., the
key with value 4 comes at index 0.
• Again after searching for key 4, the element is found at index 0 of
the given array which reduces the entire’s search space.
14. 6. get(index):- to get the element of given index
get(index)
{
if(index >=0 && index<length)
return A[index];
}
7. set(index):- to set the value of element of given index
set(index,x)
{
if(index >=0 && index<length)
A[index] = x;
}
24. merging:-
Merging can be done only on sorted array.
In merging we have combine two sorted array and combine them to make a single
sorted array.
2 4 6 8 10
0 1 2 3 4
A
3 5 7 9 11
0 1 2 3 4
B
2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9
C
After merging:-