ุงู„ู€ Array ู‡ูˆ ุนุจุงุฑุฉ ุนู† ู…ุฌู…ูˆุนุฉ ู…ู† ุงู„ุจูŠุงู†ุงุช ุงู„ู…ุชุฌุงูˆุฑุฉ (continues block of data) ูˆุงู„ู„ูŠ ุจุชุชุฎุฒู† ุจู†ูุณ ุงู„ุดูƒู„ ููŠ ุงู„ู€ RAM.

Storing arrays in RAM

  • ูƒู„ ู‚ูŠู…ุฉ ูุฑุฏูŠุฉ ููŠ ุงู„ู€ Array (ุฒูŠ ุงู„ู€ int) ุจุชุชุฎุฒู† ููŠ 4 bytes (ูŠุนู†ูŠ 32 bit). ูู…ุซู„ู‹ุง ุฑู‚ู… 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

OperationTime Complexity
ReadO(1)
SearchO(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 ู„ุนู†ุตุฑ ุฌุฏูŠุฏุŒ ุฅูŠู‡ ุงู„ู„ูŠ ุจูŠุญุตู„ุŸ
    1. ุจูŠุชู… ุฅู†ุดุงุก Array ุฌุฏูŠุฏุฉ ุฎุงู„ุต ุจู€ Capacity ุถุนู ุงู„ู‚ุฏูŠู…ุฉ.
    2. ุจูŠุชู… ู†ุณุฎ ูƒู„ ุงู„ุนู†ุงุตุฑ ู…ู† ุงู„ู€ Array ุงู„ู‚ุฏูŠู…ุฉ ู„ู„ุฌุฏูŠุฏุฉ.
    3. ุจูŠุชู… ู…ุณุญ ุงู„ู€ 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.

OperationTime Complexity
ReadO(1)
SearchO(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();
    }
}