diff --git a/content/3_Bronze/Intro_DS.mdx b/content/3_Bronze/Intro_DS.mdx index 6294151..4489978 100644 --- a/content/3_Bronze/Intro_DS.mdx +++ b/content/3_Bronze/Intro_DS.mdx @@ -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 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 v(30); +vector v(30); // one way +vector v; v.resize(30); // another way ``` -```java -ArrayList list = new ArrayList(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. + + +There is no Java equivalent; just use a for loop. + + + +```java +ArrayList list = new ArrayList(); +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 v; v.reserve(30); ``` ```java -list.ensureCapacity(50); +ArrayList list = new ArrayList(30); // one way +ArrayList list = new ArrayList(); 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 v; @@ -164,15 +176,18 @@ For example, `vector.begin()` returns an iterator pointing to the first element ```cpp for (vector::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::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: - vectors, more details about dynamic arrays + ow dynamic arrays work assumes prior experience