DEV Community

Cover image for Array Ops in Depth 🎰
Shrutik
Shrutik

Posted on • Edited on

Array Ops in Depth 🎰

Release .2

First of all..What is an Array ?
Arrays are used to store a collection of data of the same type. They are often used to store lists of numbers, strings, or objects. Arrays can be used to perform a variety of operations on data.

Here are some specific examples of how arrays are used:

  • In a video game, an array might be used to store the positions of all the enemies on the screen.
  • In a spreadsheet program, an array might be used to store the values of all the cells in a column.
  • In a database, an array might be used to store the records of all the customers in a table.

Advantages of using arrays:

  • Arrays can store a large amount of data in a compact space.
  • Arrays can be accessed quickly and efficiently.

Static and Dynamic Arrays:
Static arrays – Size cannot be changed
Dynamic arrays – Size can be changed

Operations on an Array:
Focus on the primary array operations first. They are essential for understanding more complex operations later on.
An array supports the following operations:

  • Traversal: Visiting every element of an array once is known as traversing the array.
  • Insertion: An element can be inserted in an array at a specific position.
  • Deletion: An element at a specified position can be deleted.
  • Searching: Searching can be done by traversing the array until the element to be searched is found.
  • Other operations include sorting ascending, sorting descending, etc.

Linear Vs Binary Search:
Let's say that you have a pile of your books lined up on the floor. You want to find your favorite book, but you don't remember where it is. You could start at the beginning of the line and look at each book one by one until you find your favorite. This is called a linear search.

Linear search is a simple algorithm that works well for small arrays. However, it can be slow for large arrays. This is because you have to look at every element in the array, even if your favorite book is not there.

Binary search is a more efficient algorithm for searching large arrays. It works by dividing the array in half and then looking at the middle element. If the middle element is your favorite book, you've found it! If the middle element is not your favorite book, you can then discard the half of the array that it is in and continue searching the other half.

Binary search is much faster than linear search because it only has to look at half of the array on each iteration. This means that it will find your favorite book much faster, even if the array is very large.

Here is a table that summarizes the differences between linear search and binary search:

Linear Search -

  • Starts at the beginning of the array and looks at each element one by one until the desired element is found.
  • Slow for large arrays.

Binary search -

  • Divides the array in half and then looks at the middle element. If the middle element is the desired element, the search is finished. Otherwise, the half of the array that the middle element is not in is discarded and the search continues on the other half.
  • Fast for large arrays.

In this post, we have learned about the basics of arrays and some of the most common operations that can be performed on them. We have also seen how linear and binary search can be used to find elements in an array.
I hope this post has been informative and helpful. If you have any questions, please feel free to leave a comment below.

Happy Coding πŸ‘πŸ»!
Thank You

Top comments (0)