Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between access and search in array?

While studying data structure and algorithm, i come across these two term simultaneously, but i am unable to differentiate between access and search. For example time complexity of array include access time of O(1) while search O(n).

like image 650
Azhar uddin Avatar asked Mar 09 '17 16:03

Azhar uddin


People also ask

What is searching in array?

One of the basic operations to be performed on an array is searching. Searching an array means to find a particular element in the array. The search can be used to return the position of the element or check if it exists in the array.

What is the difference between searching and sorting?

On the one hand, Searching refers to a technique that helps us search a data element out of the given string or array. On the other hand, Sorting refers to the technique used for rearranging the data elements present in a string or an array in any specified order, descending or ascending.

What is searching and sorting in array?

Searching then means finding a record in the array that has a specified value in its key field. Sorting means moving the records around in the array so that the key fields of the record are in increasing (or decreasing) order.

What are the methods of searching an array?

Conclusion. Finding a given element in an array of 'n' elements is referred to as searching in data structures. In searching, there are two types: sequential search and interval search.


Video Answer


2 Answers

Let's imagine a street, we'll call this List Street. On list street, there are houses. The Address of the first house is 101 List Street. The address of the second house is 102 List Street. So on and so forth.

Let's say that we know that our friend Element lives at the 4th house on List Street, well then we know that Element must live at 104 List Street. We can immediately access their house, because we know exactly where it is on the street. We only need to access 1 house in order to find Element.

But what if we didn't know what house Element lived in? We would have to knock on the door of each house and ask if Element lives there. In this case, we would need to access 4 houses until we got to 104 List Street to find Element.

The same idea applies to arrays. An array stores data types. Each data type is of the same size, for example an int is usually 4 bytes. If an int array starts at memory address 0x0001 and we wanted to access any element, it takes the same amount of time to access array[2] as it does array[102] as it does array[9999]. Because we know the starting address, and we know the size of each data type, we can immediately jump to that location in memory. O(1)

However, if you're trying to search for a specific element, then you need to access each element and test if it's the element you're looking for, until you reach the desired element. For an array with 5 elements, you might have to search 5 times. O(5). For an array with 900 elements, you might have to search 900 times until you find your desired element. O(900). For an array with n elements, you would search n times. O(n)

like image 73
amallard Avatar answered Jan 04 '23 23:01

amallard


Access: get A[i] element value
Search: find whether some value exists in array (and get it's index)

like image 26
MBo Avatar answered Jan 04 '23 23:01

MBo