[Data Structure] Topic 2: Array

Authored by Tony Feng

Created on March 18th, 2022

Last Modified on March 19th, 2022

Understanding Arrays

Def

  • An array is a collection of data elements with the same type stored at contiguous memory locations.
  • Index & Elements
  • Note: This is different from the List type in Python

Common Operations

  • Access - O(1)
  • Search - O(n)
  • Insert - O(n)
  • Delete - O(n)

Characteristics

  • Easy to read
  • Hard to write

Array Implementation in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Create an array
arr = []

# Add an element - O(1) if the position is empty; 
#                  O(N) if the length of the array changes.
arr.append(1)
arr.append(2)
arr.append(3) # [1,2,3]

# Insert an element - O(N)
arr.insert(2, 100) # [1,2,100,3]

# Remove an element
arr.remove(100) # case 1: [1,2,3] - O(N), where arg is an element
arr.pop(1)      # case 2: [1,3] - O(N), where arg is an index
arr.pop()       # case 3: [1] - O(1), where the last element is removed

# Traverse an array - O(N)
for item in arr: print(item)
for id, item in enumerate(arr): print("Index at ", id, " is: ", item)
for id in range(0, len(arr)): print(arr[id])

# Find an element - O(N)
arr[1,2,3]
id = arr.index(3) # 2, where arg is an element

# Sort an array - O(N*log(N))
arr = [3,1,2]
arr.sort()           # [1,2,3]
a.sort(reverse=True) # [3,2,1]

Array Implementation in Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Create an array
int[] a = {1,2,3};
int[] b = new int[]{1,2,3};
int[] c = new int[3]; // The initilization is [0,0,0]
for (int i=0, i < c.length; i++){
    c[i] = i + 1;
}
ArrayList<Integer> d = new ArrayList<>(); // Integer is an object
for (int i = 0; i < 3; i++){
    d.add(i+1)
}

// Add an element
// An addiotnal empty array should be declared for a, b, c.
// For ArrayList, the length of the array could be ignored in declaration.
d.add(100);     // [1,2,3,100] - O(1)
d.add(3, 99);   // [1,2,3,99,100] - O(N), i.e. 100 should be moved backwards for 99.

// Access an element - O(1)
int item1 = a[1];
int item2 = d.get(1);

// Update an element - O(1)
a[1] = 0;    // [1,0,3,100]
d.set(1, 0); // [1,0,3,99,100], where the 1st arg is the index; the 2nd arg is the new value.

// Remove an element for ArrayList - O(N)
d.remove(99) // [1,2,3,100]

// Get the length of array - O(1), i.e. there is a cnt var inside
int aLen = a.length; // String.length(), Array.length
int dLen = d.size();

// Traverse an array - O(N)
for (int i = 0; i < a.length; i++) { ... }
for (int i = 0; i < d.size(); i++) { ... }

// Find an element - O(N)
// Loop should be adopted for a, b, c;
// For ArrayList, use .contains()
boolean is100 = d.contains(100)

// Sort an array - O(N*log(N))
Array.sort(a) // Read from the last to reverse the array
Collections.sort(d)
Collections.sort(d, Collections.reverseOrder())

Reference


MIT License
Last updated on Apr 22, 2022 08:58 EDT
Built with Hugo
Theme Stack designed by Jimmy