Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Linear array or an array is a linear data structure that stores the elements of similar data type in contiguous memory. Arrays acts based on index values, and accessing or modifying elements in the array depends on these index values, which play an important role. Multiple data elements of the same data type are stored in an array. It helps to manage operations in large datasets.
In this article, we will thoroughly discuss linear arrays. We will learn about the types of arrays, their definitions, their initialization, and multiple operations that are performed on arrays.
Need of an Array in Data Structure?
Consider a scenario when you want to insert the marks obtained by 100 students in a class. In the absence of an array, to achieve the above scenario, you will need to create each element every time. This will be hectic and time-consuming. So to overcome this, and many other problems, linear arrays came into the picture.
By using an array, you can simply store all the marks in a single array. By doing this, you will be able to save time and also achieve better readability of the code.
Below is the representation of how you can use the linear array to store the marks.
int marks[100];
Characteristics of Array Data Structure:
Zero Based Indexing: Array Indexing in C takes its index from 0. The first element starts with an index of 0 while the second element with index 1 and so forth.
Contiguous Memory : The elements of an array in C are stored in contiguous memory locations which allows for efficient access to array elements using index notations.
Direct Memory Access: Elements can be directly accessed from their indexes using which array elements can access randomly and efficiently.
Fixed Size: Arrays in C are of fixed size. It means once you define the size of the array, then it cannot be changed during runtime.
Homogeneous Elements: Arrays in C store homogeneous data. All elements need to have the same type which is declared at declaration time.
Types of Arrays in Data Structure
There are generally two types of arrays.
Single Dimensional Arrays
Multidimensional Arrays
1. Single Dimensional Arrays
Single dimensional arrays or linear arrays is the type of array that stores the data of the same data type in a continuous memory allocation.
Below is the syntax to declare a 1-D array.
data_type array_name[array_size];
Here ‘data_type’ is the type of the data, such as int, float.
‘array_name’ is the name of the array you wish to give.
‘array_size’ is the size of the array.
2. Multidimensional array
A multidimensional array is an array with more than one dimension. Data in the multidimensional array is stored in a grid-like structure. There are different types of multi-dimensional arrays such as 2-dimensional arrays, and three-dimensional arrays.
Two Dimensional Array
2-D arrays are the most used arrays in multi-dimensional arrays. These are generally stored in the form of rows and columns and are used to represent matrices.
Syntax
data_type array_name[rows][columns];
Here rows and columns are the sizes of rows and columns.
Three Dimensional Array
Three-dimensional arrays are special forms of multidimensional arrays that are used to represent three-dimensional coordinates in a programming language. The three-dimensional array is an extension of two-dimensional arrays.
Syntax
dataType arrayName[depth][rows][columns];
Here
The first parameter represents layers (or depth).
Here rows and columns are the sizes of rows and columns.
How Do You Declare an Array?
There are multiple ways to declare an array. Below are some of the declarations that can be used to declare an array:
There are multiple ways to initialize an array. Below we have discussed the methods to initialize an array.
Initialize the size of an array
int arr[3] = {1,2,3};
In the above code, we first initialize the size of an array and in the, next we also initialize the values in the array for the given size.
Declare the elements in the array
int arr[] = {1, 2, 3, 4};
In the above code, when the size of an array is not defined, the elements count is automatically taken and the array occupies space for each element of the array.
Initialize each Element Separately
int arr[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
In the above code, we first declare the size of an array, and later initialize each element of an array.
Taking input from the user
Different programming languages have different methods to take input from the user.
C: scanf()
C++: cin
Java: Scanner. nextInt() // If data type is int
Python : input()
Javascript : prompt()
How Can You Access Elements of Arrays in Data Structures?
To access an element in an array there are different ways. Traversal in the entire array works through the index. The index of the first element in an array starts with 0.
Below is the code for taking input from the user and to print the data elements of the array.
C
C++
Java
Python
JavaScript
C
#include <stdio.h> int main() { int size; printf("Enter the size of an array: "); scanf("%d", &size); int arr[size]; printf("Enter %d elements:\n", size); for(int i = 0; i < size; i++) { scanf("%d", &arr[i]); } printf("Array elements are: "); for(int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of an array: "); int size = scanner.nextInt(); int[] arr = new int[size]; System.out.println("Enter " + size + " elements:"); for (int i = 0; i < size; i++) { arr[i] = scanner.nextInt(); } System.out.print("Array elements are: "); for (int i = 0; i < size; i++) { System.out.print(arr[i] + " "); } } }
You can also try this code with Online Java Compiler
let size = parseInt(prompt("Enter the size of an array:")); let arr = []; for (let i = 0; i < size; i++) { arr.push(parseInt(prompt(`Enter element ${i + 1}:`))); } console.log("Array elements are:", arr.join(" "));
You can also try this code with Online Javascript Compiler
public class Main { public static void main(String[] args) { int[] arr = {10, 20, 30, 40}; int size_arr = arr.length; for (int i = 0; i < size_arr; i++) { System.out.println(arr[i]); } } }
You can also try this code with Online Java Compiler
Search is a process of finding an element in an array. To search an array, traverse the array and match each element with the given element.
Two searching methods can be used to search an element in an array.
Linear Search
Linear Search is a sequential search process in which an array is traversed till the end of an array. In linear search, every element of the array is compared with the given specific value one by one. Search continues until the value is found or does not reach the end of the array.
C
C++
Java
Python
JavaScript
C
#include <stdio.h>
int linear_search(int arr[], int length, int x) { for (int i = 0; i < length; i++) { if (arr[i] == x) { return 0; } } return 1; }
int main() { int arr[4] = {40, 20, 30, 10}; int search = 30; int size_arr = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < size_arr; i++) { printf("%d\n", arr[i]); } int find = linear_search(arr, size_arr, search); if (find == 0) { printf("Element found\n"); } else { printf("Element not found\n"); } return 0; }
public class Main { public static int linear_search(int[] arr, int length, int x) { for (int i = 0; i < length; i++) { if (arr[i] == x) { return 0; } } return 1; }
public static void main(String[] args) { int[] arr = {40, 20, 30, 10}; int search = 30; int size_arr = arr.length; for (int i = 0; i < size_arr; i++) { System.out.println(arr[i]); } int find = linear_search(arr, size_arr, search); if (find == 0) { System.out.println("Element found"); } else { System.out.println("Element not found"); } } }
You can also try this code with Online Java Compiler
A binary search algorithm is an algorithm to find an element’s position in a sorted array. It works by repeatedly dividing the search interval in half. If the element is found in the middle of the interval, the search is complete.
C
C++
Java
Python
JavaScript
C
#include <stdio.h>
int binary_search(int arr[], int left, int right, int x) { while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == x) { return 0; } if (arr[mid] < x) { left = mid + 1; } else { right = mid - 1; } } return 1; }
int main() { int arr[4] = {10, 20, 30, 40}; int search = 30; int size_arr = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < size_arr; i++) { printf("%d\n", arr[i]); } int find = binary_search(arr, 0, size_arr - 1, search); if (find == 0) { printf("Element found\n"); } else { printf("Element not found\n"); } return 0; }
public class Main { public static int binary_search(int[] arr, int left, int right, int x) { while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == x) { return 0; } if (arr[mid] < x) { left = mid + 1; } else { right = mid - 1; } } return 1; }
public static void main(String[] args) { int[] arr = {10, 20, 30, 40}; int search = 30; int size_arr = arr.length; for (int i = 0; i < size_arr; i++) { System.out.println(arr[i]); } int find = binary_search(arr, 0, size_arr - 1, search); if (find == 0) { System.out.println("Element found"); } else { System.out.println("Element not found"); } } }
You can also try this code with Online Java Compiler
Deletion of an element is a process to remove an element of an array. You can delete an element from an array from any position. Beginning, end, or any specified position.
C
C++
Java
Python
JavaScript
C
#include <stdio.h>
int main() { int arr[5] = {40, 20, 30, 10}; int size = 4; int position = 2;
for (int i = position; i < size - 1; i++) { arr[i] = arr[i + 1]; } size--;
for (int i = 0; i < size; i++) { printf("%d ", arr[i]); }
Sorting is an algorithm that helps to sort the data of the elements in an ascending order. There are various sorting algorithms such as Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort, Bucket Sort, Heap Sort, Counting Sort, and many more.
C
C++
Java
Python
JavaScript
C
#include <stdio.h>
void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }
int main() { int arr[5] = {40, 20, 30, 10}; int size = 4;
bubbleSort(arr, size);
for (int i = 0; i < size; i++) { printf("%d ", arr[i]); }
public class Main { public static void bubbleSort(int[] arr, int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }
public static void main(String[] args) { int[] arr = {40, 20, 30, 10}; int size = 4;
bubbleSort(arr, size);
for (int i = 0; i < size; i++) { System.out.print(arr[i] + " "); } } }
You can also try this code with Online Java Compiler
function bubbleSort(arr) { let n = arr.length; for (let i = 0; i < n - 1; i++) { for (let j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { let temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }
let arr = [40, 20, 30, 10];
bubbleSort(arr);
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
You can also try this code with Online Javascript Compiler
Below is the time and space complexity for the operations performed in the array.
Time complexity
Time complexity for the operations performed in the linear array.
Operation
Best Case
Average Case
Worst Case
Insertion
O(n)
O(n)
O(n)
Traversal
O(n)
O(n)
O(n)
Deletion
O(1)
O(n)
O(n)
Searching
O(1)
O(n)
O(n)
Space complexity
Space complexity for the operations performed in the linear array.
Operation
Best Case
Average Case
Worst Case
Insertion
O(1)
O(n)
O(n)
Traversal
O(1)
O(1)
O(1)
Deletion
O(1)
O(n)
O(n)
Searching
O(1)
O(1)
O(1)
Advantages of Linear Array
Arrays are simple to use. When a user wants to store multiple values of the same data type arrays are the best option to choose from a data structure.
Arrays perform best with loops. When processing multiple elements, arrays allow you to efficiently use loops. For example, you can use a single loop to perform the same action on every element, which can greatly speed up your program when dealing with large amounts of data.
Multidimensional arrays are beneficial while handling the arrays within arrays. When the user wants to store the data in a tabular format, multi-dimensional arrays come into play.
Sorting of data in a linear array is easy and simple to implement.
The readability of code is improved as the same function can be used in multiple ways.
Reliable performance is achieved when elements are accessed by the index value.
Disadvantages of Linear Array
The major drawback of using a linear array is its fixed size. When an array size is declared, it cannot be modified, or resizing the array is not supported.
Certain operations like insertion or deletion are difficult because of their fixed size.
Arrays lack built-in methods for operations like sorting or searching, requiring manual implementation.
When you want to store data of multiple data types in a single structure, an array cannot be used as it can store data of similar data types.
Arrays require contiguous memory allocation, leading to potential memory wastage and fragmentation.
Applications of Linear Array
There are various applications of linear arrays. Some of them are:
It is used to store database records as it stores data in contiguous memory.
Linear arrays are widely used to implement other data structures like vectors, stack, heap, etc.
Arrays are also used to store the scheduled events of the calendar. The date and time in an array can be easily modified.
Arrays are used in graphical programming as they can store information like pixels, color ranges, and many others.
Arrays are used in the implementation of searching and sorting algorithms.
Frequently Asked Questions
What is linear array used for?
Linear arrays are used in the software industry to manage and organize data efficiently. It is widely used to for easy insertion and manipulation of data. Complex data structure algorithms and data processing tasks make use of linear arrays.
What are the features of linear array?
A linear array or an array is a linear data structure that stores the elements of similar data types in contiguous memory. Elements of the same data type are stored in the linear array providing easy flexibility to search and perform operations on the array.
How do arrays differ from linked lists?
Arrays allocate memory in contiguous blocks and allow random access, making them faster for accessing elements. Linked lists, however, store elements in nodes that can be anywhere in memory, with each node pointing to the next, which supports easier insertions and deletions.
Conclusion
In conclusion, linear arrays are foundational in programming and data structures, offering efficient data storage and operations for elements of the same type. Despite limitations like fixed size and lack of dynamic features, their simplicity and versatility make them a crucial tool for managing structured data, implementing algorithms, and supporting advanced data structures.
Live masterclass
Python + AI Skills to ace 30 LPA+ Data roles
by Prerita Agarwal
17 Nov, 2025
01:30 PM
Build ChatGPT-like search live with Google SDE3 using RAG
by Saurav Prateek
16 Nov, 2025
08:30 AM
No CS Degree? How to Get a ₹15LPA+ Data Job
by Anand
16 Nov, 2025
06:30 AM
Build GenAI Projects that can get you Amazon interview
by Anubhav Sinha
17 Nov, 2025
03:00 PM
Google SDE tips to crack GenAI Technical Interviews
by Saurav Prateek
18 Nov, 2025
03:00 PM
Python + AI Skills to ace 30 LPA+ Data roles
by Prerita Agarwal
17 Nov, 2025
01:30 PM
Build ChatGPT-like search live with Google SDE3 using RAG