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

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

PPTX
Arrays
shillpi29
 
PDF
M v bramhananda reddy dsa complete notes
Malikireddy Bramhananda Reddy
 
PDF
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPT
Lecture 2a arrays
Victor Palmar
 
PPTX
ARRAYS.pptx
MamataAnilgod
 
PPTX
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
PDF
1-Intoduction ------------- Array in C++
ab6399671
 
PPTX
DSA Unit II array.pptx
sayalishivarkar1
 
PPTX
DS Module1 (1).pptx
AnuJoseph95
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PPTX
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
PPTX
Data Structure Introduction- Arrays, Matrix, Linked List
JasmineJinitha
 
PPTX
Different type of Arrays and multidimensional arrays
prathwinidevadiga1
 
PDF
Data-Structure-using-C-Rajesh-Pandey.pdf
mohanaprakasht
 
PPTX
arrays.pptx
NehaJain919374
 
PDF
DSA UNIT II ARRAY AND LIST - notes
swathirajstar
 
PPTX
arrays.pptx
HarmanShergill5
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Arrays
Trupti Agrawal
 
PDF
arrays-130116232821-phpapp02.pdf
MarlonMagtibay2
 
Arrays
shillpi29
 
M v bramhananda reddy dsa complete notes
Malikireddy Bramhananda Reddy
 
Lecture 2a arrays
Victor Palmar
 
ARRAYS.pptx
MamataAnilgod
 
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
1-Intoduction ------------- Array in C++
ab6399671
 
DSA Unit II array.pptx
sayalishivarkar1
 
DS Module1 (1).pptx
AnuJoseph95
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
Data Structure Introduction- Arrays, Matrix, Linked List
JasmineJinitha
 
Different type of Arrays and multidimensional arrays
prathwinidevadiga1
 
Data-Structure-using-C-Rajesh-Pandey.pdf
mohanaprakasht
 
arrays.pptx
NehaJain919374
 
DSA UNIT II ARRAY AND LIST - notes
swathirajstar
 
arrays.pptx
HarmanShergill5
 
Data structure array
MajidHamidAli
 
arrays-130116232821-phpapp02.pdf
MarlonMagtibay2
 

Recently uploaded (20)

PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
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.