Skip to Main Content
Mathematica 錦囊妙計
book

Mathematica 錦囊妙計

by Sal Mangano
April 2014
Beginner to intermediate content levelBeginner to intermediate
830 pages
15h 21m
Chinese
GoTop Information, Inc.
Content preview from Mathematica 錦囊妙計
100
|
第三章
一個 BinarySearch 的使用範例:
將新元素插入已排序的串列中
延伸資訊
訣竅 3.3 將討論如何不透過對串列排序而取得元素順序。
針對各種排序演算法的概論,請參見
http://bit.ly/2bRckv
3.3 在不排序下取得順序
問題點
你想得到串列元素排列順序,但不想對串列排序,因為這需耗費記憶體以保存多筆不同
順序的串列。[Page-101]
解決方案
Ordering
指令得到元素的排列順序。
ch03.indd 100 2014/4/2 上午 05:18:58
3.3 在不排序下取得順序
|
101
討論說明
Ordering
指令還有兩種用法。第一種是接受整數引數,限制輸出數目,指定
n
輸出前
n
個元素的順序,指定
-n
則輸出後
n
個元素的順序。當你不需對整個串列排序時,這使
Ordering
指令比
Sort
指令更好用。
第二種是同時接受整數引數及排列順序函數。
當取得串列排列順序後,建立排序版的串列便很容易了。
然而,即使不需輸出完整串列順序,
Ordering
Sort
指令使用比較函數的次數仍是相
同的。
以堆積(heap)進行此類運算應該會更有效率,但為了少數使用案例而更改演算法,放
棄將比較次數最小化的原則,似乎有點划不來。畢竟,即使是在我的筆記型電腦上,對
100 萬個元素執行
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ubuntu for Non-Geeks, 4th Edition

Ubuntu for Non-Geeks, 4th Edition

Rickford Grant, Phil Bull

Publisher Resources

ISBN: 9789863470106