1. 程式人生 > 實用技巧 >資料結構 向量和陣列_資料結構和陣列

資料結構 向量和陣列_資料結構和陣列

資料結構 向量和陣列

Firstly, what is a data structure?

首先,什麼是資料結構?

It is a collection of values that can have relationships among them and functions applied to them. Each data structure is good and specialized for its own particular issue as well as storing and finding particular data — fast.

它是值的集合,這些值之間可以具有相互關係以及應用於它們的功能。 每個資料結構都很好,並且專門針對自己的特定問題以及快速儲存和查詢特定資料。

There is a vast list of data structures that can be found here: All data structures.

在這裡可以找到大量的資料結構:所有資料結構。

However, there are only a few main ones that you really need to know that are used 90% of the time. They are:

但是,您真正需要知道的是90%的時間都使用了幾個主要的東西。 他們是:

  • Arrays

    陣列
  • Stacks

    堆疊
  • Queues

    Queue列
  • Linked Lists

    連結串列
  • Trees

    樹木
  • Tries

    嘗試
  • Graphs

    圖表
  • Hash Tables

    雜湊表

Please bear in mind that each language has its own data structures, an example of a table can be found here. But if the language you are using does not contain the specific data structure, do not worry, as you can build one. For example, if Javascript doesn’t have Stacks — you can build one.

請記住,每種語言都有自己的資料結構,可以在此處找到表的示例。 但是,如果您使用的語言不包含特定的資料結構,請不要擔心,因為您可以構建一個。 例如,如果Javascript沒有堆疊-您可以構建一個。

Arrays

陣列

Arrays organize data sequentially. So, they are stored one after another in memory. Arrays are one of the most commonly used data structures and have one of the smallest footprints of any data structure.

陣列按順序組織資料。 因此,它們一個接一個地儲存在記憶體中。 陣列是最常用的資料結構之一,在任何資料結構中的佔用空間均最小。

Having come from a coding Bootcamp background, I knew what arrays were and how to use them, but having learned the basics of Big O Notation, which can be found in my previous blog here, I have realized that simple inbuilt methods actually have a variety of different Big O Notations.

來自Bootcamp的編碼背景,我知道什麼是陣列以及如何使用它們,但是瞭解了Big O Notation的基礎知識(可以在我以前的部落格中找到) ,我已經意識到,簡單的內建方法實際上有各種各樣的方法不同的大O符號。

Take this simple array below and the two inbuilt methods of pop() and unshift():

請看下面的這個簡單陣列以及pop()和unshift()的兩個內建方法:

Image for post

What would you expect their Big O to be? Previously, I never would have thought about this, thinking… well .pop() takes the last element of the array and unshift just inserts an element at the beginning. Simple…

您希望他們的Big O是什麼? 以前,我從沒想過,想想……好吧.pop()接受陣列的最後一個元素,而unshift只是在開始處插入一個元素。 簡單…

However, now you need to look at HOW the method works.

但是,現在您需要檢視該方法的工作原理。

Image for post

Why? Well pop() is simple. No matter what the length of the input, the method simply returns the last element. Therefore it is constant.

為什麼? pop()很簡單。 不管輸入的長度是多少,該方法都會簡單地返回最後一個元素。 因此,它是恆定的。

However, unshift adds an element to an array. But? That simply just puts an element at the beginning every time, so surely that is constant as well?

但是,unshift將元素新增到陣列。 但? 只是每次都只是將一個元素放在開頭,因此確定它也是恆定的嗎?

That is partly correct, however, the index of the array needs to change. Therefore:

這是部分正確的,但是,陣列的索引需要更改。 因此:

Image for post

Unshift needs to loop through the array and assign each element a new index. As we know from the previous blog, a simple loop function is O(n) or linear time.

Unshift需要遍歷陣列併為每個元素分配一個新索引。 從上一篇部落格中我們知道,一個簡單的迴圈函式是O(n)或線性時間。

This is an example that should then make you think about if a different type of data structure would better suit adding a new element.

這是一個示例,然後應讓您考慮是否其他型別的資料結構更適合新增新元素。

This should give you a basic understanding of why looking something up in the array is fast O(1) and adding something into the array is slower O(n).

這應該使您對為什麼在陣列中查詢某些內容的速度快O(1)而在陣列中新增某些內容的速度慢O(n)有了基本的瞭解。

Let’s use an array to solve a simple question of reversing a string.

讓我們使用陣列來解決一個簡單的反轉字串的問題。

Image for post

Here, we are using the .split() method. According to MDN ‘the split() method divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array.’

在這裡,我們使用.split()方法。 根據MDN的說法,“ split()方法將字串分成子字串的有序列表,然後將這些子字串放入陣列中,然後返回該陣列。”

We then use the .join() method. Which ‘creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.’

然後,我們使用.join()方法。 通過連線陣列(或類似陣列的物件)中的所有元素(用逗號或指定的分隔符字串分隔),建立並返回新字串。 如果陣列只有一個專案,則該專案將不使用分隔符而返回。

This was just a basic example of using the array data structure to achieve a specific output. We put the string into an array which then allowed us to achieve the desired outcome.

這只是使用陣列資料結構實現特定輸出的一個基本示例。 我們將字串放入一個數組中,然後使我們能夠實現所需的結果。

Arrays are great for fast lookups, accessing which index you want to look up, or adding onto the end of an array or taking an element off the end of an array, it is also ordered in memory. However, it is slow for inserts and deletes and finally, if you are using a static array then it is a fixed size.

陣列非常適合快速查詢,訪問要查詢的索引,新增到陣列的末尾或將元素從陣列的末尾移開,它也可以在記憶體中排序。 但是,插入和刪除速度很慢,最後,如果您使用的是靜態陣列,則它的大小是固定的。

翻譯自: https://levelup.gitconnected.com/data-structures-and-arrays-dcb0745d2e4f

資料結構 向量和陣列