![]() |
VOOZH | about |
A sparse array or sparse matrix is an array in which most of the elements are zero.
Sparse arrays can be represented in two ways:
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.
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.
Below is the representation of the sparse array:
Representation of Sparse array using arrays : 0 1 2 2 3 3 2 0 0 2 1 3 7 1 2 5 8 4