What is an Array in Data Structures?
##Read Operation --- efficient
What does an array do during a read operation? When you create an array, you need to tell computer how long the array is going to be and where the array starts from in memory box. JavaScript handles all under the hood so anyone using JavaScript doesn't have to worry about it.
(This could be the reason the program runs slower compared to the programming language C, which the program has to deal with the length and location of the array very specifically)
Reading an array is 'fast' because the computer remembers where the array starts. The important thing about arrays is that they have an index. To get specific data from an array, you just need to tell the computer the index number of the data. Because the computer remembers where the array starts from in its memory, it is fast to find the associated data from the index.
- Conclusion: Arrays are good for reading lots of data.
##Search Operation --- inefficient
'Read' and 'Search' are two different operations, using a read operation when the index number of the data is known and a search operation when the index number is unknown. Read operations require less time because the computer already knows where the memory is located. Search operations, however, take more time because the user does not know if the data exists or where it is stored if it exists, and the computer has to search memory one by one.
Memory is a 'closed' box. Searching takes time because the computer doesn't know what's in the box before open it. If the data you are looking for doesn't exist, the computer needs to go and look through all items in the array.
- Conclusion: Searching array isn't efficient.
##Insert Operation --- it depends
The time of insert operations vary depending on where you want to append the data. Here are some examples of insert operations:
- Best when data is added to the very last index
- So so.. when data is added to the middle of the array
- Worst when data is added to the front of the array or when adding data when the index is full
##Delete Operation --- it depends (same as inserting)
The time complexity of delete operations is same as insert operations. Deleting the last item in the array is the best case scenario, and deleting the first item is the worst case scenario. When you delete the item in the middle of the array, the computer needs to move the exsiting data to fill the gap.
##Conclusion
- Arrays are good data structures to read.
- It needs algorithm to help search, insert, and delete operations.
- If the array is sorted, it allows a binary search algorithm to be used, reducing the number of steps to retrieve the data. Inserting data into sorted array takes more time.