Array


Definition

  • Arrays are collections of data elements of the same type stored in contiguous memory regions.
  • Arrays are a derived data type in the C programming language that may hold primitive data types like int, char, double, float, etc.
  • The simplest data structure is an array, which allows each data element to be accessed at random using its index number.
  • For example, if we want to record a student's grades in six subjects, we don't need to create separate variables for each subject's grades. Instead, we can create an array that can hold the marks in each subject in contiguous memory locations.

The array markings[10] contains the student's marks in ten separate subjects, each of which is represented by a different subscript in the array, such as marks[0] for the first subject, marks[1] for the second subject, and so on.

The Array's Properties

  • Each element has the same data type and size (int = 4 bytes).
  • The array's elements are stored in contiguous memory regions, starting with the smallest memory place.
  • Because we can calculate the address of each element of the array using the specified base address and data element size, we can access the elements of the array at random.

Time Complexity

Algorithm Average Case Worst Case
Access O(1) O(1)
Search O(n) O(n)
Insertion O(n) O(n)
Deletion O(n) O(n)

Space Complexity

In an array, the worst-case space complexity is O. (n).

The Benefits of Array

  • Because an array gives a single name for a set of variables of the same type, it is simple to remember the names of all the array's elements.
  • Traversing an array is a simple task; we have to increase the array's base address to visit each element one by one.
  • The index can be used to access any element in the array directly.