What is Array

The array is a data structure where values or items are placed in a linear order, which means the memory assigned to each item is contiguous. The data type of an array is the same for all the elements present in it.

With the contiguous memory allocation, it becomes easier to find out the memory location of any element in the array by just knowing the first memory location and adding the offset.

For Example:

What is Array

In the above example, we have an integer array, and the memory locations are contiguous, where each integer takes 4 bytes, and the starting address is 4000.

We cannot increase or decrease the size of an array dynamically once it is declared. The memory allocation is static in most of the languages, so we have to declare the array size at the time of array declaration.

Indexing in the array

We can identify and access each value with the help of an index number in an array.

For example, arr[1] will be used to access the second element of the array in zero-based indexing.

The array indexing can be of the following types:

0-based indexing

If the first value of the array gets the index 0, it is called 0-based indexing. Most of the programming languages follow 0-based indexing.

1-based indexing

If the array index starts from 1, it is called 1-based indexing. It means the first value of the array will be assigned index 1.

N-based indexing

When the index of the first element can be anything, even negative values, it is called N-based indexing.

Initialization Methods in an Array:

There are various methods by which we can declare and initialize the array in Java.

1. Using the 'new' keyword

With the help of a new keyword, we can allocate the specific size of memory to the array. In java, when we use the new keyword, it allocates the memory in a heap, so it allocates the memory as static.

For Example:

In the above code, we have declared an array of size 12 of type integer. We have allocated the memory, which can store 12 integers using the new keyword.

2. By direct values

Another way to initialize and declare the array is to insert the values directly in the array without using a new keyword and specifying the array size.

For Example:

In the above example, we have declared the array and initialized it at the same time.

Various operations on an Array

1. Insertion operation

If we have an array, we can insert any value to any index of the array, but the new value must have the same data type.

Example:

Output:

old array is : 1 2 3 4 5 6
new array is : 1 2 98 4 5 6

Explanation:

In the above code, we have declared an array of an integer data type with some values. Now, we have inserted a new value, 98, at index two and changed the array.

Time complexity: O(1) or constant time complexity.

2. Accessing any element

To access any element of the array, we can just use its index number to access it.

Example:

Output:

4th element in the array is 4

Explanation:

In the above code, we have declared and initialized an array then we access the fourth value of an array by using index number 3 (0-based indexing).

Time complexity: O(1)

3. Searching an element

We can search any element in the array. It may or may not be present in the array. We can use either linear or binary search for searching operations.

Example:

Output:

we found the target value

Explanation:

In the above code, we have an array and a target value. We put a loop on the array and search for the target element. Since we got the target value in the array, we came out of the loop and printed the statement.

Time complexity:

O(N), where N is the number of elements in the array.

We can also use binary search methods to reduce the time complexity in a sorted array.

The time complexity of binary search: O(logN)

Binary Search Example:

Output:

target is not present

Explanation:

In the above code, we have used the binary search method to search the target element in the array.

  • First, it will get the mid index for the range varying from lo to high. Then, if the target value is present at the mid index, we will stop.
  • If the target value is lesser than the value at mid, we will discard the right half by assigning high = mid-1.
  • Else we will move to the right half of the array and discard the left half by lo= mid+1.
  • Thus at each iteration, we are dividing the search space by 2, so effectively, the time complexity of the above code will be O(log n), where n represents the number of elements in the array.

4. Sorting the Array

Sorting means basically arranging or reordering the elements of the array in a specific order, either it would be ascending or descending. There are a lot of sorting techniques used in the array, like the bubble sort, insertion sort, merge sort, selection sort, radix sort, heap sort, etc.

Example:

Output:

array before sorting is: 5 3 7 8 4 6 1
array after sorting is: 1 3 4 5 6 7 8

Explanation:

In the above code, we have used the bubble sort method to sort the given array in ascending order. We have an integer array that is unsorted. We have compared each adjacent value and swapped them if they were not in the correct order.

Time complexity: O(n2)

O(n2) time complexity: bubble sort, selection sort, insertion sort

o(nlogn) time complexity: merge sort, quick sort, etc.

Types of the Array

There can be various types of arrays based on their dimensions, so we divide them mainly into two types:

1. One-dimensional Array

When elements of an array are arranged in a linear fashion, it is called a one-dimensional array. It is the basic array we generally use in the problems.

2. Multi-dimensional Arra

When there is more than one dimension of the array, it is called a multi-dimensional array. It can be 2D or 3D, or more dimensional.

Example:

Output:

2D array is :
1 2 3
4 5 6
7 8 9

Explanation:

In the above code, we have created a 2-dimensional array of integer data type.

Advantages of the Array

  • Arrays perform very well in the program because they have a very good ability to cache locality.
  • We can represent many values of the same data type with just one variable name, so it makes it easier to understand the code.
  • We can access any element of the array randomly using index values, so it makes it very easy to operate on arrays.

Disadvantages of the Array

  • Insertion or deletion operation can be costly because the size of the array is fixed, and memory allocation is static in the array.
  • So if we want a data structure where insertion and deletion are easy, we can use the stack data structure, which is based on the LIFO principle.

Applications of the Array

  • We can use the array for CPU scheduling algorithms.
  • Sorting algorithms can be implemented using the array data structure.
  • An array is used to solve the complex problems related to matrices.
  • Database records are arranged using an array data structure.
  • Other data structures, like a queue, stack, hashmap, etc., are implemented by the array.
  • Only one name is used for many variables.
  • It can store the elements which have the same data type.





Latest Courses