(0,0) | |||||||
---|---|---|---|---|---|---|---|
(1,0) | |||||||
(2,3) |
address (2,3) = address (0,0) + element_size * ((2-0) * 7 + (3-0))
(0,0) |
---|
(0,2) |
(1,1) |
(1,2) |
Add | Remove | |
---|---|---|
Beginning | O(n) | O(n) |
End | O(1) | O(1) |
Middle | O(n) | O(n) |
Store a pointer to a dynamically allocated array, and replace it with a newly-allocated array as needed.
In the definition of the abstract class of Dynamic Array
**Dynamic Array Resizing: **We have a pointer pointing our array, we have size indicating the current usage of the array and capacity indicating the available size of the array. When the size is equal to the capacity, we just make a bigger array and copy elements from the past and then let our pointer to point at the new array.
public static void insertionSort(char[] data){
int n = data.length;
for(int k=1; k<n; k++){
char cur =data[k];
int j = k;
while (j>0&&data[j-1]>cur){
data[j] = data[j-1];
j--;
}
data[j] = cur;
}
}