Algorithm of searching Flashcards

1
Q

Linear Searching vs Binary Searching

A

A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another approach to perform the same task is using Binary Search.
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Interpolaton Search

A
The idea of formula is to return higher value of pos
// when element to be searched is closer to arr[hi]. And
// smaller value when closer to arr[lo]
pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Binary Search vs Interpolation Search

A

Interpolation search works better than Binary Search for a sorted and uniformly distributed array.
On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly distributed), where n is the number of elements to be searched. In the worst case (for instance where the numerical values of the keys increase exponentially) it can make up to O(n) comparisons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Ternary Search

A

Ternary search is a divide and conquer algorithm that can be used to find an element in an array. It is similar to binary search where we divide the array into two parts but in this algorithm, we divide the given array into three parts and determine which has the key (searched element). We can divide the array into three parts by taking mid1 and mid2 which can be calculated as shown below. Initially, l and r will be equal to 0 and n-1 respectively, where n is the length of the array.
mid1 = l + (r-l)/3
mid2 = r – (r-l)/3
Time Complexity:

O(\log _{3} n)

, where n is the size of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly