數據結構與算法系列之數組

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"什麼是數組?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"忘了在哪本書看見過一句話“理解了概念性的東西,你就學會了70%”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"回到主題,什麼是數組?"},{"type":"text","marks":[{"type":"strong"}],"text":"數組(Array)是一種線性表數據結構。它用一組連續的內存空間,來存儲一組具有相同類型的數據"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"概念中有兩個關鍵的地方:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"數組是一種線性數據結構"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"數組中存儲的是連續的內存空間和相同類型的數據"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"什麼是線性數據結構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有數據結構基礎的小夥伴都應該知道,線性結構就是數據排成一條線一樣的數據結構,也就意味着它僅有前後兩個方向,比如隊列、單鏈表、棧等,也是線性結構"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/07/078feb8f3368e05ab7f5b5690d069a7a.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"與它相對的就是非線性表,最典型的就是樹和圖。他們的特點就是並不是只有前後這種關係"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f7/f7bd14a2f4eedf2a4705c0a05822813e.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"連續的內存空間和相同類型的數據"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正因爲有了這個特性,使得數組可以進行“隨機訪問”。雖然訪問數組中某個元素變得很快,但缺點就是在修改(刪除、插入)數組的時候,操作會變得很麻煩,因爲要保證數組內存空間的連續性,所以不得不進行繁瑣的數據移動"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設有一個長度爲5的int類型的數組var a [5]int,假設給這個數組分配的內存空間首地址是1000,則這個數組分配的內存空間爲1000~1019,看下圖"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/67/670471f18c82fa58a7df34290933ab0a.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"操作系統會爲每一個內存單眼分配一個地址,通過這個地址來訪問內存中的數據。當操作系統需要隨機訪問數組中的某一個元素時,它會通過下邊這個尋址公式來計算出某個元素的存儲地址"}]},{"type":"codeblock","attrs":{"lang":"php"},"content":[{"type":"text","text":"a[i]_address = base_address + i * data_type_size\n\ndata_type_size:表示數組中每個元素的大小。比如int類型,它佔用的是4個字節,所以這裏data_type_size就是4"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"數組和鏈表的區別是什麼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過上邊對數組的介紹,可以看出來,數組適合查找操作。但是查找的時間複雜度並不爲O(1)。即便是排好序的數組,你用二分查找,時間複雜度也是O(logn)。所以,數組支持隨機訪問,根據下標隨機訪問的時間複雜度爲O(1)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"數組相關操作"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"插入元素"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數組頭部插入元素"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在數組的頭部插入元素,爲了保證空間的連續性,需要將數組中所有的元素向後移一位,然後將待插入元素放入到首部位置"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9e/9e32948a0cedc8f821b67a130f5cedec.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼實現"}]},{"type":"codeblock","attrs":{"lang":"php"},"content":[{"type":"text","text":"function insertIntoHead($elem, $arr)\n {\n echo \"原始數組:\".PHP_EOL;\n print_r($arr);\n\n $i = 0;\n $len = count($arr);\n while ($i < $len) {\n $arr[$len-$i] = $arr[$len-$i-1];\n $i++;\n }\n\n $arr[0] = $elem;\n\n echo \"頭部插入元素之後結果:\".PHP_EOL;\n print_r($arr);\n }"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數組中間插入元素"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在數組的中間某個位置插入元素,需要將待插入位置以後的元素均向後移動一位"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b6/b615a04b0a15cac945b08a4a7ff9f31f.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼實現"}]},{"type":"codeblock","attrs":{"lang":"php"},"content":[{"type":"text","text":"function insertIntoMid($elem, $position, $arr)\n {\n echo \"原始數組:\".PHP_EOL;\n print_r($arr);\n $len = count($arr);\n\n $i = $position-1;\n $value = $arr[$i];\n $arr[$i] = $elem;\n $head = $i+1;\n $offset = $position-1;\n\n while ($i < $len) {\n $arr[$len-$i+$offset] = $arr[$len-$i-1+$offset];\n $i++;\n }\n $arr[$head] = $value;\n\n echo \"頭部插入元素之後結果:\".PHP_EOL;\n print_r($arr);\n }"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數組尾部插入元素"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在尾部插入元素,不需要移動元素,直接放在當前的末尾元素後邊即可"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"刪除元素"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數組頭部刪除元素"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"刪除數組頭部元素,將所有的元素向前移動一位即可"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5d/5db4466d193df10a1586d28fd8dac6f2.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼實現:"}]},{"type":"codeblock","attrs":{"lang":"php"},"content":[{"type":"text","text":"function delHead($arr)\n{\n echo \"刪除前:\".PHP_EOL;\n print_r($arr);\n\n for ($i=0; $i
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章