VOOZH about

URL: https://www.geeksforgeeks.org/dsa/what-is-meant-by-sparse-array/

⇱ What is meant by Sparse Array? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

What is meant by Sparse Array?

Last Updated : 27 Oct, 2022

A sparse array or sparse matrix is an array in which most of the elements are zero.

  • The sparse array is an array in which most of the elements have the same value(the default value is zero or null).
  • Sparse matrices are those array that has the majority of their elements equal to zero.
  • A sparse array is an array in which elements do not have contiguous indexes starting at zero.
  • Sparse arrays are used over arrays when there are lesser non-zero elements. Sparse arrays require lesser memory to store the elements and the computation time can be saved. 

  • Storage: When there is the maximum number of zero elements and the minimum number of non-zero elements then we use a sparse array over a simple array as it requires less memory to store the elements. In the sparse array, we only store the non-zero elements. 
  • Computation Time: In the sparse array we only store non-zero elements and hence, traversing only non-zero elements takes less computation time.

Sparse arrays can be represented in two ways:

  • Array Representation
  • Linked List Representation

1. Array Representation:

To represent a sparse array 2-D array is used with three rows namely: Row, Column, and Value.

Row: Index of the row where non-zero elements are present.
Column: Index of the column where the non-zero element is present.
Value: The non-zero value which is present in (Row, Column) index.

👁 Image

2. Linked List Representation:

To represent a sparse array using linked lists, each node has four fields namely: Row, Column, Value, and Next node.

Row: Index of the row where non-zero elements are present.
Column: Index of the column where the non-zero element is present.
Value: The non-zero value which is present in (Row, Column) index.
Next node: It stores the address of the next node.

👁 Image
👁 Image

Below is the representation of the sparse array:


Output
Representation of Sparse array using arrays : 
 0 1 2 2 3 3
 2 0 0 2 1 3
 7 1 2 5 8 4
Comment
Article Tags: