Code-Memo

Arrays

An array is a linear data structure that stores elements in contiguous memory locations. It allows random access to elements using an index. Arrays are fixed in size (in low-level languages), but Python’s lists provide dynamic resizing.

Types of Arrays

  1. Static Array: Fixed-size arrays using array module or third-party packages like NumPy.
  2. Dynamic Array: Python’s built-in list type, which resizes automatically.

Basic Operations

1. Insertion

arr.insert(0, 10)  # Insert 10 at index 0
arr.append(20)  # Append 20 to the end
arr.insert(2, 15)  # Insert 15 at index 2

2. Deletion

arr.pop(0)  # Remove element at index 0
arr.pop()  # Removes and returns the last element
arr.remove(15)  # Removes the first occurrence of 15

3. Access

print(arr[1])  # Access element at index 1

4. Update

arr[2] = 99  # Update the element at index 2 to 99

5. Traversal

for item in arr:
    print(item, end=' ')

6. Searching

def linear_search(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i
    return -1

7. Slicing

sub_array = arr[1:4]  # From index 1 to 3

8. Length

n = len(arr)  # Number of elements

9. Sorting

arr.sort()  # In-place sort
sorted_arr = sorted(arr)  # Returns new sorted list

10. Reversing

arr.reverse()  # In-place reverse
rev_arr = arr[::-1]  # Sliced reverse