What is an Array in Data Structures?






Data structure is a tool for organizing a collection of data. To learn data structures is important because the way you organize the data has actual consequences with a speed of the program. Array is one of the most basic data structure which is a collection of items stored at contiguous memory locations. In this post, I will talk about which operations arrays are optimized for.




##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.

* If the array is sorted?
Sorted array allows a binary search algorithm to be used, reducing the number of steps to retrieve the data. 



##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
          As explained eariler, the computer knows where the array starts and how long the array supposed  to be, all it has to do is to jump to the end of the array and add the data there.


  • So so..   when data is added to the middle of the array
          The computer needs to move exsiting datas to make a room in the middle of the array. For example, to add data to the index '1' (which means second index) of the array with 5 indexes you need to move all datas after index 1 to the next index. 



  • Worst   when data is added to the front of the array or when adding data when the index is full
          As mentioned above, the computer needs to move exsiting data to make a room for the new data if it's not added to the last index. When data is added to the first index, more steps are required as all data must be moved after the first index. The larger the array, the more steps.


When adding data when the index is full, the computer creates a bigger array by copying the existing array and appends new data to the new array.



* Inserting data into sorted array takes more time because it compares each of exsiting data to find right index when you add a new data (adding by sorting) But if you spend more time adding and sorting, you can save time when searching.





##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.