correct intro-ds

This commit is contained in:
Benjamin Qi 2020-07-06 21:52:56 -04:00
parent a91b3cb226
commit 428493f390

View file

@ -1,7 +1,7 @@
---
id: intro-ds
title: Introduction to Data Structures
author: Darren Yao
author: Darren Yao, Benjamin Qi
description: Introduces data structures, dynamic arrays, sorting.
frequency: 4
---
@ -54,9 +54,9 @@ Collections data types always contain an `add` method for adding an element to t
## Dynamic Arrays
You're probably already familiar with regular (static) arrays. Now, there are also dynamic arrays ([`vector`](http://www.cplusplus.com/reference/vector/vector/) in C++, [`ArrayList`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) in Java) that support all the functions that a normal array does, and can resize itself to accommodate more elements. In a dynamic array, we can also add and delete elements at the end in $O(1)$ time.
You're probably already familiar with regular (static) arrays. Now, there are also dynamic arrays ([`vector`](http://www.cplusplus.com/reference/vector/vector/) in C++, [`ArrayList`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) in Java) that support all the functions that a normal array does, and can repeatedly reallocate storage to accommodate more elements as they are added.
For example, the following code creates a dynamic array and adds the numbers $1$ through $10$ to it:
In a dynamic array, we can also add and delete elements at the end in $O(1)$ time. For example, the following code creates a dynamic array and adds the numbers $1$ through $10$ to it:
```cpp
vector<int> v;
@ -71,25 +71,37 @@ for(int i = 1; i <= 10; i++){
}
```
When declaring a dynamic array we can give it an initial size, so it doesn't resize itself as we add elements to it. The following code initializes a dynamic array with initial size $30$:
In C++ we can give a dynamic array an initial size. The code below creates a dynamic array with $30$ zeroes.
```cpp
vector<int> v(30);
vector<int> v(30); // one way
vector<int> v; v.resize(30); // another way
```
```java
ArrayList<Integer> list = new ArrayList<Integer>(30);
```
Note that declaring a dynamic array with an initial size does NOT initialize its elements. For example, if you declare a dynamic array with initial size 30 and try to edit the element at index 5, this will throw an error. This is because declaring with an initial size only serves to prevent unnecessary resizing later.
We can also update the size of a dynamic array to ensure it can hold at least $x$ elements for some $x$. If the size of the dynamic array is less than $x$, it is updated to $x$. If it is greater than or equal to $x$, the size update does nothing.
<code-comment lang="java">
There is no Java equivalent; just use a for loop.
</code-comment>
```java
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 30; i++) list.add(0);
```
We can ensure that a dynamic array's **capacity** must be at least $x$, meaning that it will not reallocate storage until we add more than $x$ elements to it. If the dynamic array's capacity is already at least $x$, then nothing occurs. Note that capacity is **not** the same thing as size. For example, if you declare a dynamic array with initial capacity $30$ and try to edit the element at index $5$, this will throw an error since the size of the dynamic array is still zero.
The following code initializes a dynamic array with initial capacity $30$:
```cpp
v.reserve(50);
vector<int> v; v.reserve(30);
```
```java
list.ensureCapacity(50);
ArrayList<Integer> list = new ArrayList<Integer>(30); // one way
ArrayList<Integer> list = new ArrayList<Integer>(); list.ensureCapacity(30); // another way
```
However, we need to be careful that we only add elements to the end of the `vector`; insertion and deletion in the middle of the `vector` is $O(n)$.
We can add and remove at any index of a dynamic array in $O(n)$ time.
```cpp
vector<int> v;
@ -164,15 +176,18 @@ For example, `vector.begin()` returns an iterator pointing to the first element
```cpp
for (vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
cout << *it; //prints the values in the vector using the pointer
cout << *it;
}
```
C++11 and later versions can automatically infer the type of an object if you use the keyword `auto`. This means that you can replace `vector<int>::iterator` with `auto` or `int` with `auto` in the for-each loop.
C++11 and later versions can automatically infer the type of an object if you use the keyword `auto`, so the following are okay:
```cpp
for (auto it = myvector.begin(); it != myvector.end(); ++it) {
cout << *it;
}
for(auto element : v) {
cout << element; //prints the values in the vector
cout << element;
}
```
@ -217,7 +232,7 @@ Two ways to avoid this:
<resources>
<resource source="CPH" title="4.1 - Dynamic Arrays" starred></resource>
<resource source="PAPS" title="3.1, 6.1">vectors, more details about dynamic arrays</resource>
<resource source="PAPS" title="3.1 - Vectors, 6.1 - Dynamic Arrays">ow dynamic arrays work</resource>
<resource source="CPC" title="2 - Data Structures" url="02_data_structures">assumes prior experience</resource>
</resources>