SlideShare a Scribd company logo
UNIT-1 ARRAY
Array Data Structure
• An array data structure is a fundamental concept in computer science
that stores a collection of elements in a contiguous block of memory. It
allows for efficient access to elements using indices and is widely used
in programming for organizing and manipulating data.
Array Definition
• An array is a collection of items of the same variable type that are stored at
contiguous memory locations. It’s one of the most popular and simple data
structures and is often used to implement other data structures. Each item in an
array is indexed starting with 0 . Each element in an array is accessed through its
index.
• Array is a linear data structure where all elements are arranged sequentially. It is a
collection of elements of same data type stored at contiguous memory locations.
Need of Array Data Structures
• Arrays are a fundamental data structure in computer science. They
are used in a wide variety of applications, including:
• Storing data for processing
• Implementing data structures such as stacks and queues
• Representing data in tables and matrices
• Creating dynamic data structures such as linked lists and trees
Types of Array
• There are two main types of arrays:
• One-dimensional arrays: These arrays store a single row of elements.
• Multidimensional arrays: These arrays store multiple rows of
elements.
Array Operations
• Common operations performed on arrays include:
• Traversal : Visiting each element of an array in a specific order (e.g.,
sequential, reverse).
• Insertion : Adding a new element to an array at a specific index.
• Deletion : Removing an element from an array at a specific index.
• Searching : Finding the index of an element in an array.
Applications of Array
• Arrays are used in a wide variety of applications, including:
• Storing data for processing
• Implementing data structures such as stacks and queues
• Representing data in tables and matrices
• Creating dynamic data structures such as linked lists and trees
Array element location
• This makes it easier to calculate the position of each element by
simply adding an offset to a base value, i.e., the memory location of
the first element of the array (generally denoted by the name of the
array). The base value is index 0 and the difference between the two
indexes is the offset.
• Remember: “Location of next index depends on the data type we
use”.
Is the array always of a fixed size?
• the array has a fixed size meaning once the size is given to it, it cannot
be changed i.e. you can’t shrink it nor can you expand it. The reason
was that for expanding if we change the size we can’t be sure ( it’s not
possible every time) that we get the next memory location to us for
free. The shrinking will not work because the array, when declared,
gets memory statically allocated, and thus compiler is the only one
that can destroy it.
Basic terminologies of Array
• Array Index: In an array, elements are identified by their indexes.
Array index starts from 0.
• Array element: Elements are items stored in an array and can be
accessed by their index.
• Array Length: The length of an array is determined by the number of
elements it can contain.
Types of Arrays
1. One-dimensional Array(1-D Array): You can imagine a 1d array as a
row, where elements are stored one after another.
• Multi-dimensional Array: A multi-dimensional array is an array with
more than one dimension. We can use multidimensional array to store
complex data in the form of tables, etc. We can have 2-D arrays, 3-D
arrays, 4-D arrays and so on.
• Two-Dimensional Array(2-D Array or Matrix): 2-D Multidimensional
arrays can be considered as an array of arrays or as a matrix consisting
of rows and columns.
• Three-Dimensional Array(3-D Array): A 3-D Multidimensional
array contains three dimensions, so it can be considered an array of
two-dimensional arrays.
Operations on Array
• 1. Array Traversal:
• Array traversal involves visiting all the elements of the array once.
• int arr[] = { 1, 2, 3, 4, 5 };
• // Traversing over arr[]
• for (int i = 0; i < arr.length; i++) {
• System.out.print(arr[i] + " ");
•
• 2. Insertion in Array:
• We can insert one or multiple elements at any position in the array.
• static void insertElement(int arr[], int n, int x, int pos)
• {
• // shift elements to the right
• // which are on the right side of pos
• for (int i = n - 1; i >= pos; i--)
• arr[i + 1] = arr[i];
• arr[pos] = x;
• }
• 3. Deletion in Array:
• We can delete an element at any index in an array. Below is the implementation of Deletion
of element in an array:
• // function to search a key to
• // be deleted
• static int findElement(int arr[], int n, int key)
• {
• int i;
• for (i = 0; i < n; i++)
• if (arr[i] == key)
• return i;
• // Return -1 if key is not found
• return -1;
• }
•
• // Function to delete an element
• static int deleteElement(int arr[], int n, int key)
• {
• // Find position of element to be
• // deleted
• int pos = findElement(arr, n, key);
•
• if (pos == -1) {
• System.out.println("Element not found");
• return n;
• }
•
• // Deleting element
• int i;
• for (i = pos; i < n - 1; i++)
• arr[i] = arr[i + 1];
•
• return n - 1;
• }
• 4. Searching in Array:
• We can traverse over an array and search for an element.
• // Function to implement search operation
• int findElement(int arr[], int n, int key)
• {
• for (int i = 0; i < n; i++)
• if (arr[i] == key)
• return i;
• // If the key is not found
• return -1;
• }
Complexity Analysis of Operations on Array
Operation Best Case Average Case Worst Case
Traversal Ω(N) θ(N) O(N)
Insertion Ω(1) θ(N) O(N)
Deletion Ω(1) θ(N) O(N)
Searching Ω(1) θ(N) O(N)
Time Complexity:
Space Complexity:
Operation Best Case Average Case Worst Case
Traversal Ω(1) θ(1) O(1)
Insertion Ω(1) θ(N) O(N)
Deletion Ω(1) θ(N) O(N)
Searching Ω(1) θ(1) O(1)
• Advantages of Array
• Arrays allow random access to elements. This makes accessing elements by position faster.
• Arrays have better cache locality which makes a pretty big difference in performance.
• Arrays represent multiple data items of the same type using a single name.
• Arrays are used to implement the other data structures like linked lists, stacks, queues, trees, graphs, etc.
• Disadvantages of Array
• As arrays have a fixed size, once the memory is allocated to them, it cannot be increased or decreased,
making it impossible to store extra data if required. An array of fixed size is referred to as a static array.
• Allocating less memory than required to an array leads to loss of data.
• An array is homogeneous in nature so, a single array cannot store values of different data types.
• Arrays store data in contiguous memory locations, which makes deletion and insertion very difficult to
implement. This problem is overcome by implementing linked lists, which allow elements to be accessed
sequentially.
• Applications of Array
• They are used in the implementation of other data structures such as array lists, heaps, hash tables,
vectors, and matrices.
• Database records are usually implemented as arrays.
• It is used in lookup tables by computer.
Calculation of address of element of 1-D, 2-D, and 3-D using
row-major and column-major order
• Calculating the address of any element In the 1-D array:
• A 1-dimensional array (or single-dimension array) is a type of
linear array. Accessing its elements involves a single subscript that can
either represent a row or column index.
• Example:
To find the address of an element in an array the following formula is used-
Address of A[Index] = B + W * (Index – LB)
Where:
•Index = The index of the element whose address is to be found (not the value of
the element).
•B = Base address of the array.
•W = Storage size of one element in bytes.
•LB = Lower bound of the index (if not specified, assume zero).
• Example: Given the base address of an array A[1300 ………… 1900] as 1020 and the size of
each element is 2 bytes in the memory, find the address of A[1700].
• Solution:
• Given:
• Base address (B) = 1020
• Lower bound (LB) = 1300
• Size of each element (W) = 2 bytes
• Index of element (not value) = 1700
• Formula used:
• Address of A[Index] = B + W * (Index – LB)
• Address of A[1700] = 1020 + 2 * (1700 – 1300)
• = 1020 + 2 * (400)
• = 1020 + 800
• Address of A[1700] = 1820
• Calculate the address of any element in the 2-D array:
• The 2-dimensional array can be defined as an array of arrays. The 2-
Dimensional arrays are organized as matrices which can be represented
as the collection of rows and columns as array[M][N] where M is the
number of rows and N is the number of columns.
• Example:
• To find the address of any element in a 2-Dimensional array there are the following two ways-
• Row Major Order
• Column Major Order
• 1. Row Major Order:
• Row major ordering assigns successive elements, moving across the rows and then down the
next row, to successive memory locations. In simple language, the elements of an array are
stored in a Row-Wise fashion.
• To find the address of the element using row-major order uses the following formula:
• Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
• I = Row Subset of an element whose address to be found,
• J = Column Subset of an element whose address to be found,
• B = Base address,
• W = Storage size of one element store in an array(in byte),
• LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
• LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero),
• N = Number of column given in the matrix.
• Example: Given an array, arr[1………10][1………15] with base value 100 and the size of each element is 1 Byte in
memory. Find the address of arr[8][6] with the help of row-major order.
• Solution:
• Given:
• Base address B = 100
• Storage size of one element store in any array W = 1 Bytes
• Row Subset of an element whose address to be found I = 8
• Column Subset of an element whose address to be found J = 6
• Lower Limit of row/start row index of matrix LR = 1
• Lower Limit of column/start column index of matrix = 1
• Number of column given in the matrix N = Upper Bound – Lower Bound + 1
• = 15 – 1 + 1
• = 15
• Formula:
• Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
• Solution:
• Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
• = 100 + 1 * ((7) * 15 + (5))
• = 100 + 1 * (110)
• Address of A[I][J] = 210
2. Column Major Order:
If elements of an array are stored in a column-major fashion means moving across the column and then to the next
column then it’s in column-major order. To find the address of the element using column-major order use the
following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it as zero),
M = Number of rows given in the matrix.
Example: Given an array arr[1………10][1………15] with a base value of 100 and the size of each element is 1 Byte in
memory find the address of arr[8][6] with the help of column-major order.
• Solution:
• Given:
• Base address B = 100
• Storage size of one element store in any array W = 1 Bytes
• whose address to be found J = 6
• Lower Limit of row/start row index of matrix LR = 1
• Lower Limit of column/start column index of matrix = 1
• Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1
• = 10 – 1 + 1
• = 10
• Formula: used
• Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
• Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
• = 100 + 1 * ((5) * 10 + (7))
• = 100 + 1 * (57)
• Address of A[I][J] = 157
• Calculate the address of any element in the 3-D Array:
• A 3-Dimensional array is a collection of 2-Dimensional arrays. It is specified by using three
subscripts:
• Block size
• Row size
• Column size
• More dimensions in an array mean more data can be stored in that array.
• Example:
• To find the address of any element in 3-Dimensional arrays there are the following two ways-
• Row Major Order
• Column Major Order
• 1. Row Major Order:
• To find the address of the element using row-major order, use the following formula:
• Address of A[i][j][k] = B + W *(P* N * (i-x) + P*(j-y) + (k-z))
• Here:
• B = Base Address (start address)
• W = Weight (storage size of one element stored in the array)
• M = Row (total number of rows)
• N = Column (total number of columns)
• P = Width (total number of cells depth-wise)
• x = Lower Bound of Row
• y = Lower Bound of Column
• z = Lower Bound of Width
• Example: Given an array, arr[1:9, -4:1, 5:10] with a base value of 400 and the size of each element is 2
Bytes in memory find the address of element arr[5][-1][8] with the help of row-major order?
Solution:
Given:
Block Subset of an element whose address to be found I = 5
Row Subset of an element whose address to be found J = -1
Column Subset of an element whose address to be found K = 8
Base address B = 400
Storage size of one element store in any array(in Byte) W = 2
Lower Limit of blocks in matrix x = 1
Lower Limit of row/start row index of matrix y = -4
Lower Limit of column/start column index of matrix z = 5
M(row) = Upper Bound – Lower Bound + 1 = 1 – (-4) + 1 = 6
N(Column)= Upper Bound – Lower Bound + 1 = 10 – 5 + 1 = 6
Formula used:
Address of[I][J][K] =B + W (M * N(i-x) + N *(j-y) + (k-z))
Solution:
Address of arr[5][-1][8] = 400 + 2 * {[6 * 6 * (5 – 1)] + 6 * [(-1 + 4)]} + [8 – 5]
= 400 + 2 * (6*6*4)+(6*3)+3
= 400 + 2 * (165)
= 730
• 2. Column Major Order:
• To find the address of the element using column-major order, use the following
formula:1
• Address of A[i][j][k]= B + W(M * N(i – x) + M *(k – z) + (j – y))
• Here:
• B = Base Address (start address)
• W = Weight (storage size of one element stored in the array)
• M = Row (total number of rows)
• N = Column (total number of columns)
• P = Width (total number of cells depth-wise)
• x = Lower Bound of block (first subscipt)
• y = Lower Bound of Row
• z = Lower Bound of Column
• Example: Given an array arr[1:8, -5:5, -10:5] with a base value of 400 and the size of
each element is 4 Bytes in memory find the address of element arr[3][3][3] with the
help of column-major order?
• Solution:
• Given:
• Row Subset of an element whose address to be found I = 3
• Column Subset of an element whose address to be found J = 3
• Block Subset of an element whose address to be found K = 3
• Base address B = 400
• Storage size of one element store in any array(in Byte) W = 4
• Lower Limit of blocks in matrix x = 1
• Lower Limit of row/start row index of matrix y = -5
• Lower Limit of column/start column index of matrix z = -10
• M (row)= Upper Bound – Lower Bound + 1 = 5 +5 + 1 = 11
• N (column)= Upper Bound – Lower Bound + 1 = 5 + 10 + 1 = 16
• Formula used:
• Address of A[i][j][k]=B+W×(M×P×(k−z)+M×(j−y)+(i−x))
• Solution:
• Address of arr[3][3][3] = 400 + 4 * ((11*16*(3-1)+11*(3-(-10)+(3-(-5)))
• = 400 + 4 * ((176*2 + 11*13 + 8)
• = 400 + 4 * (503)
• = 400 + 2012
• = 2412
Memory Representation of Array
In an array, all the elements are stored in contiguous memory locations. So, if we initialize an array, the
elements will be allocated sequentially in memory. This allows for efficient access and manipulation of
elements.

More Related Content

PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
PPTX
Unit 2 linear data structures
PPTX
DS Module1 (1).pptx
PPTX
data structure unit -1_170434dd7400.pptx
PPT
Unit 1.ppt
PPTX
DSA Unit II array.pptx
PPTX
Arrays.pptx
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
Unit 2 linear data structures
DS Module1 (1).pptx
data structure unit -1_170434dd7400.pptx
Unit 1.ppt
DSA Unit II array.pptx
Arrays.pptx

Similar to Data Structures - Array presentation .pptx (20)

PPTX
Data structures - unit 1
PPTX
Basic of array and data structure, data structure basics, array, address calc...
PPTX
ARRAYS.pptx
PDF
INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
PDF
INTRODUCTION TO DATA STRUCTURES AND ALGORITHM
PPT
lecture 02.2.ppt
PDF
java.pdf
PPTX
ds bridge.pptx
DOCX
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
PPT
Fundamentals of data structure syallabus
PPTX
unit 2.pptx
PPTX
Data Structure Introduction- Arrays, Matrix, Linked List
PPT
Presentation of array
PDF
cluod.pdf
PPTX
Arrays In C++
PDF
PPTX
ALGORITHM ANALYSIS AND LISTS ABSTACTS DT
PPTX
Data structure chapter 1.pptx
PPT
DS.ppt Datatastructures notes presentation
PPTX
Data structfghz€zdsrgnhlhlfdshllures.pptx
Data structures - unit 1
Basic of array and data structure, data structure basics, array, address calc...
ARRAYS.pptx
INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
INTRODUCTION TO DATA STRUCTURES AND ALGORITHM
lecture 02.2.ppt
java.pdf
ds bridge.pptx
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Fundamentals of data structure syallabus
unit 2.pptx
Data Structure Introduction- Arrays, Matrix, Linked List
Presentation of array
cluod.pdf
Arrays In C++
ALGORITHM ANALYSIS AND LISTS ABSTACTS DT
Data structure chapter 1.pptx
DS.ppt Datatastructures notes presentation
Data structfghz€zdsrgnhlhlfdshllures.pptx
Ad

Recently uploaded (20)

PPTX
Practice Questions on recent development part 1.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
DOCX
573137875-Attendance-Management-System-original
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPT
Project quality management in manufacturing
PPTX
“Next-Gen AI: Trends Reshaping Our World”
PPTX
anatomy of limbus and anterior chamber .pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Road Safety tips for School Kids by a k maurya.pptx
Practice Questions on recent development part 1.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Arduino robotics embedded978-1-4302-3184-4.pdf
Lesson 3_Tessellation.pptx finite Mathematics
573137875-Attendance-Management-System-original
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Project quality management in manufacturing
“Next-Gen AI: Trends Reshaping Our World”
anatomy of limbus and anterior chamber .pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Geodesy 1.pptx...............................................
Structs to JSON How Go Powers REST APIs.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
bas. eng. economics group 4 presentation 1.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
UNIT-1 - COAL BASED THERMAL POWER PLANTS
ETO & MEO Certificate of Competency Questions and Answers
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
CH1 Production IntroductoryConcepts.pptx
Road Safety tips for School Kids by a k maurya.pptx
Ad

Data Structures - Array presentation .pptx

  • 2. Array Data Structure • An array data structure is a fundamental concept in computer science that stores a collection of elements in a contiguous block of memory. It allows for efficient access to elements using indices and is widely used in programming for organizing and manipulating data.
  • 3. Array Definition • An array is a collection of items of the same variable type that are stored at contiguous memory locations. It’s one of the most popular and simple data structures and is often used to implement other data structures. Each item in an array is indexed starting with 0 . Each element in an array is accessed through its index. • Array is a linear data structure where all elements are arranged sequentially. It is a collection of elements of same data type stored at contiguous memory locations.
  • 4. Need of Array Data Structures • Arrays are a fundamental data structure in computer science. They are used in a wide variety of applications, including: • Storing data for processing • Implementing data structures such as stacks and queues • Representing data in tables and matrices • Creating dynamic data structures such as linked lists and trees
  • 5. Types of Array • There are two main types of arrays: • One-dimensional arrays: These arrays store a single row of elements. • Multidimensional arrays: These arrays store multiple rows of elements.
  • 6. Array Operations • Common operations performed on arrays include: • Traversal : Visiting each element of an array in a specific order (e.g., sequential, reverse). • Insertion : Adding a new element to an array at a specific index. • Deletion : Removing an element from an array at a specific index. • Searching : Finding the index of an element in an array.
  • 7. Applications of Array • Arrays are used in a wide variety of applications, including: • Storing data for processing • Implementing data structures such as stacks and queues • Representing data in tables and matrices • Creating dynamic data structures such as linked lists and trees
  • 8. Array element location • This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index 0 and the difference between the two indexes is the offset. • Remember: “Location of next index depends on the data type we use”.
  • 9. Is the array always of a fixed size? • the array has a fixed size meaning once the size is given to it, it cannot be changed i.e. you can’t shrink it nor can you expand it. The reason was that for expanding if we change the size we can’t be sure ( it’s not possible every time) that we get the next memory location to us for free. The shrinking will not work because the array, when declared, gets memory statically allocated, and thus compiler is the only one that can destroy it.
  • 10. Basic terminologies of Array • Array Index: In an array, elements are identified by their indexes. Array index starts from 0. • Array element: Elements are items stored in an array and can be accessed by their index. • Array Length: The length of an array is determined by the number of elements it can contain.
  • 11. Types of Arrays 1. One-dimensional Array(1-D Array): You can imagine a 1d array as a row, where elements are stored one after another.
  • 12. • Multi-dimensional Array: A multi-dimensional array is an array with more than one dimension. We can use multidimensional array to store complex data in the form of tables, etc. We can have 2-D arrays, 3-D arrays, 4-D arrays and so on. • Two-Dimensional Array(2-D Array or Matrix): 2-D Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and columns.
  • 13. • Three-Dimensional Array(3-D Array): A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-dimensional arrays.
  • 14. Operations on Array • 1. Array Traversal: • Array traversal involves visiting all the elements of the array once. • int arr[] = { 1, 2, 3, 4, 5 }; • // Traversing over arr[] • for (int i = 0; i < arr.length; i++) { • System.out.print(arr[i] + " "); •
  • 15. • 2. Insertion in Array: • We can insert one or multiple elements at any position in the array. • static void insertElement(int arr[], int n, int x, int pos) • { • // shift elements to the right • // which are on the right side of pos • for (int i = n - 1; i >= pos; i--) • arr[i + 1] = arr[i]; • arr[pos] = x; • }
  • 16. • 3. Deletion in Array: • We can delete an element at any index in an array. Below is the implementation of Deletion of element in an array: • // function to search a key to • // be deleted • static int findElement(int arr[], int n, int key) • { • int i; • for (i = 0; i < n; i++) • if (arr[i] == key) • return i; • // Return -1 if key is not found • return -1; • } •
  • 17. • // Function to delete an element • static int deleteElement(int arr[], int n, int key) • { • // Find position of element to be • // deleted • int pos = findElement(arr, n, key); • • if (pos == -1) { • System.out.println("Element not found"); • return n; • } • • // Deleting element • int i; • for (i = pos; i < n - 1; i++) • arr[i] = arr[i + 1]; • • return n - 1; • }
  • 18. • 4. Searching in Array: • We can traverse over an array and search for an element. • // Function to implement search operation • int findElement(int arr[], int n, int key) • { • for (int i = 0; i < n; i++) • if (arr[i] == key) • return i; • // If the key is not found • return -1; • }
  • 19. Complexity Analysis of Operations on Array Operation Best Case Average Case Worst Case Traversal Ω(N) θ(N) O(N) Insertion Ω(1) θ(N) O(N) Deletion Ω(1) θ(N) O(N) Searching Ω(1) θ(N) O(N) Time Complexity: Space Complexity: Operation Best Case Average Case Worst Case Traversal Ω(1) θ(1) O(1) Insertion Ω(1) θ(N) O(N) Deletion Ω(1) θ(N) O(N) Searching Ω(1) θ(1) O(1)
  • 20. • Advantages of Array • Arrays allow random access to elements. This makes accessing elements by position faster. • Arrays have better cache locality which makes a pretty big difference in performance. • Arrays represent multiple data items of the same type using a single name. • Arrays are used to implement the other data structures like linked lists, stacks, queues, trees, graphs, etc. • Disadvantages of Array • As arrays have a fixed size, once the memory is allocated to them, it cannot be increased or decreased, making it impossible to store extra data if required. An array of fixed size is referred to as a static array. • Allocating less memory than required to an array leads to loss of data. • An array is homogeneous in nature so, a single array cannot store values of different data types. • Arrays store data in contiguous memory locations, which makes deletion and insertion very difficult to implement. This problem is overcome by implementing linked lists, which allow elements to be accessed sequentially. • Applications of Array • They are used in the implementation of other data structures such as array lists, heaps, hash tables, vectors, and matrices. • Database records are usually implemented as arrays. • It is used in lookup tables by computer.
  • 21. Calculation of address of element of 1-D, 2-D, and 3-D using row-major and column-major order • Calculating the address of any element In the 1-D array: • A 1-dimensional array (or single-dimension array) is a type of linear array. Accessing its elements involves a single subscript that can either represent a row or column index. • Example:
  • 22. To find the address of an element in an array the following formula is used- Address of A[Index] = B + W * (Index – LB) Where: •Index = The index of the element whose address is to be found (not the value of the element). •B = Base address of the array. •W = Storage size of one element in bytes. •LB = Lower bound of the index (if not specified, assume zero).
  • 23. • Example: Given the base address of an array A[1300 ………… 1900] as 1020 and the size of each element is 2 bytes in the memory, find the address of A[1700]. • Solution: • Given: • Base address (B) = 1020 • Lower bound (LB) = 1300 • Size of each element (W) = 2 bytes • Index of element (not value) = 1700 • Formula used: • Address of A[Index] = B + W * (Index – LB) • Address of A[1700] = 1020 + 2 * (1700 – 1300) • = 1020 + 2 * (400) • = 1020 + 800 • Address of A[1700] = 1820
  • 24. • Calculate the address of any element in the 2-D array: • The 2-dimensional array can be defined as an array of arrays. The 2- Dimensional arrays are organized as matrices which can be represented as the collection of rows and columns as array[M][N] where M is the number of rows and N is the number of columns. • Example:
  • 25. • To find the address of any element in a 2-Dimensional array there are the following two ways- • Row Major Order • Column Major Order • 1. Row Major Order: • Row major ordering assigns successive elements, moving across the rows and then down the next row, to successive memory locations. In simple language, the elements of an array are stored in a Row-Wise fashion. • To find the address of the element using row-major order uses the following formula: • Address of A[I][J] = B + W * ((I – LR) * N + (J – LC)) • I = Row Subset of an element whose address to be found, • J = Column Subset of an element whose address to be found, • B = Base address, • W = Storage size of one element store in an array(in byte), • LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero), • LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero), • N = Number of column given in the matrix.
  • 26. • Example: Given an array, arr[1………10][1………15] with base value 100 and the size of each element is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major order. • Solution: • Given: • Base address B = 100 • Storage size of one element store in any array W = 1 Bytes • Row Subset of an element whose address to be found I = 8 • Column Subset of an element whose address to be found J = 6 • Lower Limit of row/start row index of matrix LR = 1 • Lower Limit of column/start column index of matrix = 1 • Number of column given in the matrix N = Upper Bound – Lower Bound + 1 • = 15 – 1 + 1 • = 15 • Formula: • Address of A[I][J] = B + W * ((I – LR) * N + (J – LC)) • Solution: • Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1)) • = 100 + 1 * ((7) * 15 + (5)) • = 100 + 1 * (110) • Address of A[I][J] = 210
  • 27. 2. Column Major Order: If elements of an array are stored in a column-major fashion means moving across the column and then to the next column then it’s in column-major order. To find the address of the element using column-major order use the following formula: Address of A[I][J] = B + W * ((J – LC) * M + (I – LR)) I = Row Subset of an element whose address to be found, J = Column Subset of an element whose address to be found, B = Base address, W = Storage size of one element store in any array(in byte), LR = Lower Limit of row/start row index of matrix(If not given assume it as zero), LC = Lower Limit of column/start column index of matrix(If not given assume it as zero), M = Number of rows given in the matrix. Example: Given an array arr[1………10][1………15] with a base value of 100 and the size of each element is 1 Byte in memory find the address of arr[8][6] with the help of column-major order.
  • 28. • Solution: • Given: • Base address B = 100 • Storage size of one element store in any array W = 1 Bytes • whose address to be found J = 6 • Lower Limit of row/start row index of matrix LR = 1 • Lower Limit of column/start column index of matrix = 1 • Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1 • = 10 – 1 + 1 • = 10 • Formula: used • Address of A[I][J] = B + W * ((J – LC) * M + (I – LR)) • Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1)) • = 100 + 1 * ((5) * 10 + (7)) • = 100 + 1 * (57) • Address of A[I][J] = 157
  • 29. • Calculate the address of any element in the 3-D Array: • A 3-Dimensional array is a collection of 2-Dimensional arrays. It is specified by using three subscripts: • Block size • Row size • Column size • More dimensions in an array mean more data can be stored in that array. • Example:
  • 30. • To find the address of any element in 3-Dimensional arrays there are the following two ways- • Row Major Order • Column Major Order • 1. Row Major Order: • To find the address of the element using row-major order, use the following formula: • Address of A[i][j][k] = B + W *(P* N * (i-x) + P*(j-y) + (k-z)) • Here: • B = Base Address (start address) • W = Weight (storage size of one element stored in the array) • M = Row (total number of rows) • N = Column (total number of columns) • P = Width (total number of cells depth-wise) • x = Lower Bound of Row • y = Lower Bound of Column • z = Lower Bound of Width • Example: Given an array, arr[1:9, -4:1, 5:10] with a base value of 400 and the size of each element is 2 Bytes in memory find the address of element arr[5][-1][8] with the help of row-major order?
  • 31. Solution: Given: Block Subset of an element whose address to be found I = 5 Row Subset of an element whose address to be found J = -1 Column Subset of an element whose address to be found K = 8 Base address B = 400 Storage size of one element store in any array(in Byte) W = 2 Lower Limit of blocks in matrix x = 1 Lower Limit of row/start row index of matrix y = -4 Lower Limit of column/start column index of matrix z = 5 M(row) = Upper Bound – Lower Bound + 1 = 1 – (-4) + 1 = 6 N(Column)= Upper Bound – Lower Bound + 1 = 10 – 5 + 1 = 6 Formula used: Address of[I][J][K] =B + W (M * N(i-x) + N *(j-y) + (k-z)) Solution: Address of arr[5][-1][8] = 400 + 2 * {[6 * 6 * (5 – 1)] + 6 * [(-1 + 4)]} + [8 – 5] = 400 + 2 * (6*6*4)+(6*3)+3 = 400 + 2 * (165) = 730
  • 32. • 2. Column Major Order: • To find the address of the element using column-major order, use the following formula:1 • Address of A[i][j][k]= B + W(M * N(i – x) + M *(k – z) + (j – y)) • Here: • B = Base Address (start address) • W = Weight (storage size of one element stored in the array) • M = Row (total number of rows) • N = Column (total number of columns) • P = Width (total number of cells depth-wise) • x = Lower Bound of block (first subscipt) • y = Lower Bound of Row • z = Lower Bound of Column • Example: Given an array arr[1:8, -5:5, -10:5] with a base value of 400 and the size of each element is 4 Bytes in memory find the address of element arr[3][3][3] with the help of column-major order?
  • 33. • Solution: • Given: • Row Subset of an element whose address to be found I = 3 • Column Subset of an element whose address to be found J = 3 • Block Subset of an element whose address to be found K = 3 • Base address B = 400 • Storage size of one element store in any array(in Byte) W = 4 • Lower Limit of blocks in matrix x = 1 • Lower Limit of row/start row index of matrix y = -5 • Lower Limit of column/start column index of matrix z = -10 • M (row)= Upper Bound – Lower Bound + 1 = 5 +5 + 1 = 11 • N (column)= Upper Bound – Lower Bound + 1 = 5 + 10 + 1 = 16 • Formula used: • Address of A[i][j][k]=B+W×(M×P×(k−z)+M×(j−y)+(i−x)) • Solution: • Address of arr[3][3][3] = 400 + 4 * ((11*16*(3-1)+11*(3-(-10)+(3-(-5))) • = 400 + 4 * ((176*2 + 11*13 + 8) • = 400 + 4 * (503) • = 400 + 2012 • = 2412
  • 34. Memory Representation of Array In an array, all the elements are stored in contiguous memory locations. So, if we initialize an array, the elements will be allocated sequentially in memory. This allows for efficient access and manipulation of elements.