ุงูู Array ูู ุนุจุงุฑุฉ ุนู ู
ุฌู
ูุนุฉ ู
ู ุงูุจูุงูุงุช ุงูู
ุชุฌุงูุฑุฉ (continues block of data) ูุงููู ุจุชุชุฎุฒู ุจููุณ ุงูุดูู ูู ุงูู RAM.
Storing arrays in RAM
- ูู ููู
ุฉ ูุฑุฏูุฉ ูู ุงูู
Array(ุฒู ุงููint) ุจุชุชุฎุฒู ูู 4bytes(ูุนูู 32bit). ูู ุซููุง ุฑูู 1 ููุชุฎุฒู ุนูู ุดูู00...1. - ุงูู
RAMุจุชุชููู ู ู ุญุงุฌุชูู ุฃุณุงุณูุชูู: ุงูููู ุฉ (Value) ูุงูุนููุงู (Address) ุจุชุงุนูุง.

Static Arrays
Reading Data from an Array
- ุงูุนู ููุฉ ุฏู ุจุชุงุฎุฏ ููุช Big O(1)ุ ูุนูู ููุช ุซุงุจุช (constant time).
- ูู
ุง ุจูุญุชุงุฌ ููุฑุฃ ุฃู ุนูุตุฑ ู
ู ุงูู
Arrayุ ุฅุญูุง ุจููุตูู ุนู ุทุฑูู ุงูุนููุงู ุจุชุงุนูุ ูุงูุจุฑูุงู ุฌ ุจูุฑูุญ ูููRAMููุฌูุจ ุงูููู ุฉ ุงููู ูู ุงูุนููุงู ุฏู. - ูู ุงูุญูููุฉุ ุฅุญูุง ู
ุด ุจูุณุชุฎุฏู
ุงูุนููุงู ุจุดูู ู
ุจุงุดุฑุ ููู ุจูุณุชุฎุฏู
ุญุงุฌุฉ ุงุณู
ูุง ุงูู indexุ ูุฏู ุจูู
ุซู ุชุฑุชูุจ ุงูุนูุตุฑ ูู ุงูู
Arrayูุจูุจุฏุฃ ู ู0. - ููู ุนุงูุฒูู ููุตู ูุฃูู ุนูุตุฑุ ููุณุชุฎุฏู
index = 0. - ููุฏุฑ ููุตู ููู ุงูุจูุงูุงุช ุงููู ูู ุงูู
Arrayุจุงุณุชุฎุฏุงูforุฃูwhile loops.

Writing Data to an Array (Fixed)
- ุงูุนู ููุฉ ุฏู ูู ุงู ุจุชุงุฎุฏ ููุช Big O(1).
- ุงูู
Arraysูููุง ููุนูู ุฃุณุงุณููู: ุซุงุจุชุฉ (Fixed) ูุฏููุงู ูููุฉ (Dynamic). - ูู ุงูู
Fixed Arrayุ ู ุด ุจููุฏุฑ ูุถูู ุนูุงุตุฑ ุฌุฏูุฏุฉ ุจุนุฏ ู ุง ูุญุฏุฏ ุญุฌู ูุงุ ูุฅููุง ู ุด ุจูุจูู ุนุงุฑููู ุฅุฐุง ูุงู ุงูู ูุงู ุงููู ุจุนุฏ ุงููArrayูู ุงููRAMูุงุถู ููุง ูุฃ. - ููู
ุงู ู
ุด ุจููุฏุฑ ูุถูู ููู
ุฉ ูู ุฃู ุนููุงู ูุงุถู ูุฎูุงุตุ ูุงุฒู
ุชููู ุงูุฅุถุงูุฉ ุจุนุฏ ุขุฎุฑ ุนูุตุฑ ู
ูุฌูุฏ ูู ุงูู
Array.

Remove a value at the end
- ูู
ุง ุจูุชููู
ุนู ู
ุณุญ ุนูุตุฑ ูู
Static Arrayุ ูุงูู ูุตูุฏ ู ุด ุฅููุง ุจูุนู ููdeallocateู ู ุงูุฐุงูุฑุฉุ ูุฅู ุฏู ู ุณุชุญูู ูุญุตู ูู ุงูููุน ุฏู. - ุงูู ุนูู ุงูุญูููู ูู ุฅููุง ุจูุนู ู overriding ููููู ุฉ ุฏูุ ูุนูู ุจููุชุจ ูููููุง ููู ุฉ ุชุงููุฉุ ุฒู ุตูุฑ ู ุซููุงุ ูุจูุนุชุจุฑ ุงูู ูุงู ุฏู ู ุด ู ูู ุจุงููุณุจุฉ ููุง ุจุนุฏ ูุฏู.
- ุงูุนู ููุฉ ุฏู ุจุชุงุฎุฏ Big O(1)ุ ูุฅู ูู ุงููู ุจูุนู ูู ูู ุนู ููุฉ ูุชุงุจุฉุ ูุฒู ู ุง ูููุงุ ุงููุชุงุจุฉ ุจุชุงุฎุฏ ููุช ุซุงุจุช.

Insert value at the beginning or at middle
- ุฅุถุงูุฉ ุนูุตุฑ ูู ุฃูู ุฃู ูุต ุงูู
Arrayุนู ููุฉ ู ุด ูุนูุงูุฉ (not efficient) ุฒู ุงูุฅุถุงูุฉ ูู ุงูุขุฎุฑ. - ุฏู ูุฅูู ูุชุญุชุงุฌ ุชุนู ู shift ุฃู ุฅุฒุงุญุฉ ููู ุงูุนูุงุตุฑ ุงููู ุจุนุฏ ุงูู ูุงู ุงููู ูุชุถูู ููู.
- ุงูุฃูู ูุชุญุฑู ูู ุงูุนูุงุตุฑ ุงููู ุจุนุฏ ุงูู ูุงู ุงูู ุทููุจ ุฎุทูุฉ ููุฏุงู (ุจุชุจุฏุฃ ู ู ุงูุขุฎุฑ ููุฃูู)ุ ูุจุนุฏูู ุชุญุท ุงูููู ุฉ ุงูุฌุฏูุฏุฉ ูู ู ูุงููุง.
- ุงูุนู
ููุฉ ุฏู ุจุชุงุฎุฏ ููุช Big O(n)ุ ูุฅู ุนุฏุฏ ุนู
ููุงุช ุงูู
shiftุจูุนุชู ุฏ ุนูู ุนุฏุฏ ุงูุนูุงุตุฑn. - ููุณ ุงูู
ุดููุฉ ุฏู ุจุชูุงุฌููุง ูู ุฌููุง ูุญุฐู ุฃู ุนูุตุฑ ู
ู ุฃูู ุฃู ูุต ุงูู
Array.
sequenceDiagram participant User participant Array User->>Array: Insert value 'X' at index 1 Note right of Array: Array: [A, B, C, D] Array->>Array: Shift 'D' to index 4 Note right of Array: Array: [A, B, C, _, D] Array->>Array: Shift 'C' to index 3 Note right of Array: Array: [A, B, _, C, D] Array->>Array: Shift 'B' to index 2 Note right of Array: Array: [A, _, B, C, D] Array->>Array: Place 'X' at index 1 Note right of Array: Final Array: [A, X, B, C, D]
Multi Column
Summary
| Operation | Time Complexity |
|---|---|
| Read | O(1) |
| Search | O(n) |
| Insert (at end) | O(1) |
| Insert (at middle) | O(n) |
| Delete (at end) | O(1) |
| Delete (at middle) | O(n) |
Code
Python
# Insert n into arr at the next open position.
# Length is the number of 'real' values in arr, and capacity
# is the size (aka memory allocated for the fixed size array).
def insertEnd(arr, n, length, capacity):
if length < capacity:
arr[length] = n
# Remove from the last position in the array if the array
# is not empty (i.e. length is non-zero).
def removeEnd(arr, length):
if length > 0:
# Overwrite last element with some default value.
# We would also decrease the length by 1.
arr[length - 1] = 0
# Insert n into index i after shifting elements to the right.
# Assuming i is a valid index and arr is not full.
def insertMiddle(arr, i, n, length):
# Shift starting from the end to i.
for index in range(length - 1, i - 1, -1):
arr[index + 1] = arr[index]
# Insert at i
arr[i] = n
# Remove value at index i before shifting elements to the left.
# Assuming i is a valid index.
def removeMiddle(arr, i, length):
# Shift starting from i + 1 to end.
for index in range(i + 1, length):
arr[index - 1] = arr[index]
# No need to 'remove' arr[length - 1], it's now a duplicate but outside the new length.C#
public static class StaticArrayOps
{
// Insert n into arr at the next open position.
// 'length' is the logical size, passed by reference to be modified.
public static void InsertEnd(int[] arr, int n, ref int length)
{
// arr.Length is the physical capacity
if (length < arr.Length)
{
arr[length] = n;
length++; // Increment the logical length
}
}
// Remove from the last logical position.
public static void RemoveEnd(ref int length)
{
if (length > 0)
{
// Just decrement the logical length. The old value is ignored.
length--;
}
}
// Insert n at index i. Assumes there is capacity.
public static void InsertMiddle(int[] arr, int i, int n, ref int length)
{
// Shift elements to the right, from the end towards i
for (int index = length - 1; index >= i; index--)
{
arr[index + 1] = arr[index];
}
// Insert at i
arr[i] = n;
length++; // Increment logical length
}
// Remove value at index i.
public static void RemoveMiddle(int[] arr, int i, ref int length)
{
// Shift elements to the left, from i+1 to the end
for (int index = i; index < length - 1; index++)
{
arr[index] = arr[index + 1];
}
// Decrement logical length
length--;
}
}Dynamic Arrays
ูุบุงุช ุฒู
JavaScriptูPythonูู ุงููArraysูููุงDynamicุจุดูู ุงูุชุฑุงุถู. ูู C# ุจูุณุชุฎุฏูList<T>.
- ุงูู
Dynamic Arraysุญุฌู ูุง ุจูุชุบูุฑุ ูุจููุฏุฑ ูุฒูุฏ ููุถูู ูููุง ุนูุงุตุฑ ุฒู ู ุง ุฅุญูุง ุนุงูุฒูู. - ุจูููู ูููุง ุญุงุฌุฉ ุฒู
Pointerุจูุดุงูุฑ ุนูู ุขุฎุฑindexุฅุญูุง ุถูููุงู.
Push
- ุนู
ููุฉ ุงูู
pushูู ุฅุถุงูุฉ ููู ุฉ ุฌุฏูุฏุฉ ูู ุขุฎุฑ ุงููArray. - ุจุชุงุฎุฏ ููุช O(1)ุ ูุฏู ุจูุถู ูุฌูุฏ ุงูู
Pointerุงููู ุจูุนุฑููุง ุขุฎุฑ ู ูุงู ูุงุถู ููู. - ูู ุงูุจุฏุงูุฉุ ุงูู
Arrayุจูููู ูููุงCapacityู ุนููุฉ (ุณุนุฉ ุชุฎุฒูููุฉ)ุ ุญุชู ูู ูุงูุชDynamic. - ุทูุจ ูู ุงูู
Arrayุงุชู ูุช ุนูู ุขุฎุฑูุง ูุญุงูููุง ูุนู ูpushูุนูุตุฑ ุฌุฏูุฏุ ุฅูู ุงููู ุจูุญุตูุ- ุจูุชู
ุฅูุดุงุก
Arrayุฌุฏูุฏุฉ ุฎุงูุต ุจูCapacityุถุนู ุงููุฏูู ุฉ. - ุจูุชู
ูุณุฎ ูู ุงูุนูุงุตุฑ ู
ู ุงูู
Arrayุงููุฏูู ุฉ ููุฌุฏูุฏุฉ. - ุจูุชู
ู
ุณุญ ุงูู
Arrayุงููุฏูู ุฉ (deallocate) ุนุดุงู ููุถู ู ุณุงุญุชูุง ู ู ุงูุฐุงูุฑุฉ.
- ุจูุชู
ุฅูุดุงุก
- ุนู
ููุฉ ุชุบููุฑ ุงูุญุฌู
ุฏู ุจุชุงุฎุฏ ููุช O(n)ุ ูุฅูุดุงุก
Arrayุฌุฏูุฏุฉ ููุณุฎnู ู ุงูุนูุงุตุฑ ุจูุงุฎุฏ ููุช ู ุชูุงุณุจ ู ุน ุนุฏุฏูู . ููู ุนู ููุฉ ุงููpushููุณูุง ูู ู ููุด ุชุบููุฑ ุญุฌู ุจุชุงุฎุฏ O(1). - ุนุดุงู ูุชุฌูุจ ุงูุชูููุฉ ุฏู ูู ู
ุฑุฉุ ุฅุญูุง ุจูุถุงุนู ุงูู
Capacityูู ุด ุจูุฒูุฏูุง ุจู ูุฏุงุฑ ูุงุญุฏ ุจุณุ ููู ููุณ ุงูููุช ู ุด ุจูุฒูุฏูุง ุฒูุงุฏุฉ ุนู ุงููุฒูู ุนุดุงู ู ูุณุชูููุด ู ุณุงุญุฉ ูุจูุฑุฉ ู ู ุงูุฐุงูุฑุฉ ุนูู ุงููุงุถู. - ุงูุนู
ููุฉ ุฏู ุจูุณู
ููุง Amortized Analysisุ ูุจุณุจุจูุง ุจููุฏุฑ ูุนุชุจุฑ ุฅู ุชูููุฉ ุงูู
pushูู ุงูู ุฌู ู ูู O(1).
graph TD A["Array (Capacity: 4, Length: 4)"] -->|Push 'E'| B{Length == Capacity?}; B -->|Yes| C["Create New Array (Capacity: 8)"]; C --> D[Copy A, B, C, D to New Array]; D --> E[Deallocate Old Array]; E --> F[Add 'E' to New Array]; B -->|No| G[Add element directly];

Pop
- ุนู
ููุฉ ู
ุณุญ ุขุฎุฑ ุนูุตุฑ ูู ุงูู
Array. - ุจุชุงุฎุฏ ููุช O(1).
Summary
ููุณ ุงูู Static Arrays ู
ุน ุงูุฃุฎุฐ ูู ุงูุงุนุชุจุงุฑ ุฅู ุงูู Insert (at end) ุชูููุชูุง O(1) ุจุดูู amortized.
| Operation | Time Complexity |
|---|---|
| Read | O(1) |
| Search | O(n) |
| Insert (at end) | O(1) Amortized |
| Insert (at middle) | O(n) |
| Delete (at end) | O(1) |
| Delete (at middle) | O(n) |

Code
Python
# Python lists are dynamic by default, but this is an example of resizing.
class Array:
def __init__(self):
self.capacity = 2
self.length = 0
self.arr = [0] * 2 # Array of capacity = 2
# Insert n in the last position of the array
def pushback(self, n):
if self.length == self.capacity:
self.resize()
# insert at next empty position
self.arr[self.length] = n
self.length += 1
def resize(self):
# Create new array of double capacity
self.capacity *= 2
newArr = [0] * self.capacity
# Copy elements to newArr
for i in range(self.length):
newArr[i] = self.arr[i]
self.arr = newArr
# Remove the last element in the array
def popback(self):
if self.length > 0:
self.length -= 1
# Get value at i-th index
def get(self, i):
if i < self.length:
return self.arr[i]
# Here we would throw an out of bounds exception
# Insert n at i-th index
def insert(self, i, n):
if i < self.length:
self.arr[i] = n
return
# Here we would throw an out of bounds exception C#
// C# has List<T> for dynamic arrays, but this class mimics the behavior.
public class DynamicArray
{
private int[] arr;
private int capacity;
private int length;
public DynamicArray()
{
this.capacity = 2;
this.length = 0;
this.arr = new int[this.capacity];
}
// Insert n in the last position of the array
public void Pushback(int n)
{
if (this.length == this.capacity)
{
Resize();
}
// Insert at next empty position
this.arr[this.length] = n;
this.length++;
}
private void Resize()
{
// Create new array of double capacity
this.capacity *= 2;
int[] newArr = new int[this.capacity];
// Copy elements to newArr
for (int i = 0; i < this.length; i++)
{
newArr[i] = this.arr[i];
}
this.arr = newArr;
}
// Remove the last element in the array
public void Popback()
{
if (this.length > 0)
{
this.length--;
}
}
// Get value at i-th index
public int Get(int i)
{
if (i < this.length && i >= 0)
{
return this.arr[i];
}
// Throw an out of bounds exception if index is invalid
throw new IndexOutOfRangeException();
}
// Overwrite value at i-th index
public void Set(int i, int n)
{
if (i < this.length && i >= 0)
{
this.arr[i] = n;
return;
}
// Throw an out of bounds exception if index is invalid
throw new IndexOutOfRangeException();
}
}
