6 -1 Chapter 6 Sorting. 6 -2 Sorting A file of size n is a sequence of n items. Each item in the file is called a record. KeyOther fields Record 14DDD.

Slides:



Advertisements
Similar presentations
FCU, Department of ECE, IC Design Research Lab. TEL: # 4945 Pre-SIm , Post-Sim.
Advertisements

Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
資料的搜尋 搜尋的基本概念 循序搜尋法(Sequential Search) 二元搜尋法(Binary Search)
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
1 Q10276: Hanoi Tower Troubles Again! 星級 : ★★★ 題組: Online-judge.uva.es PROBLEM SET Volume CII 題號: Q10276: Hanoi Tower Troubles Again! 解題者:薛祖淵 解題日期: 2006.
Section 1.2 Describing Distributions with Numbers 用數字描述分配.
3Com Switch 4500 切VLAN教學.
指導教授:陳淑媛 學生:李宗叡 李卿輔.  利用下列三種方法 (Edge Detection 、 Local Binary Pattern 、 Structured Local Edge Pattern) 來判斷是否為場景變換,以方便使用者來 找出所要的片段。
亂數產生器安全性評估 之統計測試 SEC HW7 姓名:翁玉芬 學號:
Lecture 8 Median and Order Statistics. Median and Order Statistics2 Order Statistics 問題敘述 在 n 個元素中,找出其中第 i 小的元素。 i = 1 ,即為找最小值。 i = n ,即為找最大值。 i = 或 ,即為找中位數。
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
期中考參考解答 Date: 2005/12/14 Multimedia Information Systems.
8.1 何謂高度平衡二元搜尋樹 8.2 高度平衡二元搜尋樹的加入 8.3 高度平衡二元搜尋樹的刪除
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
: The Playboy Chimp ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10611: The Playboy Chimp 解題者:蔡昇宇 解題日期: 2010 年 2 月 28 日 題意:給一已排序的數列 S( 升冪.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
1 第四章 多變數函數的微分學 § 4.1 偏導數定義 定義 極限值 ■. 2 定理 極限值的基本定理 (1) 極限值的唯一性 : 若 存在,則 其值必為唯一。 (2) 若 且 ( 與 為常數 ) , 則 且 為常數且.
:Problem D: Bit-wise Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10232: Problem D: Bit-wise Sequence 解題者:李濟宇 解題日期: 2006 年 4 月 16.
計算機概論 - 排序 1 排序 (Sorting) 李明山 編撰 ※手動換頁.
3-3 使用幾何繪圖工具 Flash 的幾何繪圖工具包括線段工具 (Line Tool) 、橢圓形工具 (Oval Tool) 、多邊星形 工具 (Rectangle Tool) 3 種。這些工具畫出 來的幾何圖形包括了筆畫線條和填色區域, 將它們適當地組合加上有技巧地變形與配 色, 不但比鉛筆工具簡單,
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
Chapter 2 Getting Started Insertion Sort: 能有效率地排序小數字的演算法 範例 :
: Playing War ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11061: Playing War 解題者:陳盈村 解題日期: 2008 年 3 月 14 日 題意:在此遊戲中,有一類玩家一旦開始攻擊, 就會不停攻擊同一對手,直到全滅對方或無法再.
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
Marble on tree ★★★☆☆ 題組: ACM Programming Contest World Finals, 1998 題號: Marble on tree 解題者:呂為萱 解題日期: 2011 年 3 月 16 日 題意: 有 n 個箱子,被擺放在 rooted.
資料結構實習-一 參數傳遞.
第六章 陣列.
Lecture 7 Sorting in Linear Time. Sorting in Linear Time2 7.1 Lower bounds for sorting 本節探討排序所耗用的時間複雜度下限。 任何一個以比較為基礎排序的演算法,排序 n 個元 素時至少耗用 Ω(nlogn) 次比較。
: Efficient Solutions ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11020: Efficient Solutions 解題者:陳宜佐 解題日期: 2007 年 4 月 24 日 題意:給定 M 個 case.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
公用品.  該物品的數量不會因一人的消費而受到 影響,它可以同時地被多人享用。 角色分配  兩位同學當我的助手,負責:  其餘各人是投資者,每人擁有 $100 , 可以投資在兩種資產上。  記錄  計算  協助同學討論.
: Problem G e-Coins ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10306: Problem G e-Coins 解題者:陳瀅文 解題日期: 2006 年 5 月 2 日 題意:給定一個正整數 S (0
: A-Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10930: A-Sequence 解題者:陳盈村 解題日期: 2008 年 5 月 30 日 題意: A-Sequence 需符合以下的條件, 1 ≤ a.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
: Automatic correction of misspellings ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11048: Automatic correction of misspellings 解題者:陳宜佐 解題日期:
: Flip Sort ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10327: Flip Sort 解題者:歐子揚 解題日期: 2010 年 2 月 26 日 題意:在這個問題中使用一種排序方式 (Flip) ,意思就是 只能交換相鄰的.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 2. Recurrence Relations (遞迴關係)
Chapter 10 m-way 搜尋樹與B-Tree
演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 7 貪婪法則 Greedy Approach.
: Help My Brother ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11033: Help My Brother 解題者: 呂明璁 解題日期: 2007 年 5 月 14 日.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
2005/7 Linear system-1 The Linear Equation System and Eliminations.
INFORMATION RETRIEVAL AND EXTRACTION 作業: Program 1 第十四組 組員:林永峰、洪承雄、謝宗憲.
: Problem E Antimatter Ray Clearcutting ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11008: Problem E Antimatter Ray Clearcutting 解題者:林王智瑞.
資料結構實習-六.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
: Construct the wall maze ★★★★★ 題組: Problem Set Archive with Online Judge 題號: 11050: Construct the wall maze 解題者:宋柏儀 解題日期: 2007 年 6 月 12 日.
:Count the Trees ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10007:Count the Trees 解題者:楊家豪 解題日期: 2006 年 3 月 題意: 給 n 個點, 每一個點有自己的 Label,
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
幼兒行為觀察與記錄 第八章 事件取樣法.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
第七章 計算複雜度概論:排序問題 7.1計算複雜度 7.2插入排序與選擇排序 7.3每次比較至多移除一個導致之演算法的下限
Presentation transcript:

6 -1 Chapter 6 Sorting

6 -2 Sorting A file of size n is a sequence of n items. Each item in the file is called a record. KeyOther fields Record 14DDD Record 22BBB Record 31AAA Record 45EEE Record 53CCC 1AAA 2BBB 3CCC 4DDD 5EEE original file sorted file

6 -3 Original pointer table 4DDD 2BBB 1AAA 5EEE 3CCC Record 1 Record 2 Record 3 Record 4 Record 5 File Sorted pointer table It is easier to search a particular element after sorting. (e.g. binary search)

6 -4 internal sorting: data stored in main memory ( more than 20 algorithms ) external sorting: data stored in auxiliary storage. stable sorting : the records with the same key have the same relative order as they have before sorting. Types of sorting

6 -5 n ,000 5,000 10,000 50, , ,000 a = 0.01n ,500 10, ,000 1,000,000 25,000, ,000,000 2,5000,000,000 b = 10n ,000 5,000 10,000 50, , ,000 1,000,000 5,000,000 a+b ,100 7,500 20, ,000 1,100,000 25,500, ,000,000 2,505,000,000 (a+b) n Time and space efficiency

6 -6 O notation f(n) is O(g(n)) if there exist positive integers a and b such that f(n) ≦ a . g(n) for all n ≧ b e.g. 4n n = O(n 2 ) ∵ n ≧ 100, 4n n ≦ 5n 2 4n n = O(n 3 ) ∵ n ≧ 10, 4n n ≦ 2n 3 f(n)= c 1 n k + c 2 n k-1 +…+ c k n + c k+1 = O(n k+j ), for any j ≧ 0 f(n)= c = O(1), c is a constant log m n = log m k . log k n,for some constants m and k log m n = O(log k n) = O(log n)

6 -7 Time complexity polynomial order: O(n k ), for some constant k. exponential order: O(d n ), for some d >1. NP-complete(intractable) problem: requiring exponential time algorithms. best sorting algorithm with comparisons: O(nlogn)

6 -8 nlog 10 n and n 2 n 1 × × × × × × × × × × × × × 10 7 nlog 10 n 1.0 × × × × × × × × × × × × × 10 7 n × × × × × × × × × × × × × 10 14

6 -9 Bubble sort 相鄰兩個資料相比, 若未符合順序, 則對調 (exchange) 之. e.g ( 由大而小 sort) pass 1 pass 2 pass 3 decreasing order nonincreasing order

6 -10 如果在某一個 pass 中,沒有任何相鄰兩項資料對調, 表示已經 sort 完畢 best case : 未 sort 之前, 已按順序排好, 需 1 pass worst case: 需 n-1 個 pass ( n 為資料量 ) 比較 (comparison) 次數最多為 : (n-1)+(n-2) = = O(n 2 ) Time complexity: O(n 2 ) n(n-1) 2 Time complexity of bubble sort

6 -11 void bubble(int x[], int n) { int hold, j, pass; int switched = TRUE; for (pass=0; pass < n-1 && switched == TRUE; pass++){ /*outer loop controls the number of passes */ switched = FALSE; /* initially no interchanges have */ /* been made on this pass */ for (j = 0; j < n-pass-1; j++) /* inner loop governs each individual pass */ if (x[j] > x[j+1]){ /* elements out of order */ /* an interchange is necessary */ switched = TRUE; hold = x[j]; x[j] = x[j+1]; x[j+1] = hold; } /* end if */ } /* end for */ } /* end bubble */

6 -12 Quicksort (partition exchange sort) e.g. 由小而大 sort (nondecreasing order) [ ] [ ] [ ] [ ] 26 [ ] [ ] 26 [ ] [ 1 5] 11 [19 15] 26 [ ] [ ] [ ] [48 37] 59 [61]

6 -13 Quicksort 方法 : 每組的第一個資料為基準 (pivot), 比它小的 資料放在左邊, 比它大的資料放在右邊, 然後以 pivot 中心, 將這組資料分成兩部份. worst case: 每次的基準資料恰為最大, 或最小 比較次數 :

log 2 n n ×2 = n n 2 ×4 = n n 4 Best case of quicksort best case: 每次分割 (partition) 時, 均分成大約相同個數的兩 部份.

6 -15 T(n): n 個資料所需時間 T(n) ≦ cn+2T(n/2), for some constant c. ≦ cn+2(c . n/2 + 2T(n/4)) ≦ 2cn + 4T(n/4) ≦ cnlog 2 n + nT(1) = O(nlogn)... Mathematical analysis of best case

6 -16 void partition(int x[], int lb, int ub, int *pj) { int a, down, temp, up; a = x[lb]; /* a is the element whose final position */ /* is sought */ up = ub; down = lb; while (down < up){ while (x[down] <= a && down < ub) down++; /* move up the array */ while (x[up] > a) up--; /* move down the array */ if (down < up){ /* interchange x[down] and x[up] */ temp = x[down]; x[down] = x[up]; x[up] = temp; } /* end if */ } /* end while */ x[lb] = x[up]; x[up] = a; *pj = up; } /* end partition */

6 -17 main program: if (lb >= ub) return; // array is sorted partition(x, lb, ub, j); // partition the elements of the // subarray such that one of the // elements(possibly x[lb]) is // now at x[j] (j is an output // parameter) and: // 1. x[i] <= x[j] for lb <= i < j // 2. x[i] >= x[j] for j < i <= ub // x[j] is now at its final position quick(x, lb, j-1); // recursively sort the subarray // between posiitons lb and j-1 quick(x, j+1, ub); // recursively sort the subarray // between positions j+1 and ub

6 -18 Selection sort e.g. 由大而小 sort pass 1 pass 2 pass 4 pass 3 方法 : 每次均從剩餘未 sort 部份之資料, 找出最大者 ( 或最 小者 ), 然後對調至其位置 比較次數 : (n-1)+(n-2) = =O(n 2 ) Time complexity: O(n 2 ) n(n-1) 2

6 -19 Binary tree sort e.g. input data: 8, 2, 9, 5, 6 建立 binary search tree: inorder traversal: output: worst case: input data: 2, 5, 6, 8, 9 比較次數 : best case: time complexity: i*2 i = O(nlogn), d  log 2 n

6 -20 Heapsort e.g. input data: 將 input data 存入 almost complete binary tree Step 1: Construct a heap

Step 2: Adjust the heap (a) x[7] = 最大值 (b) x[6] = 第二大

(c) x[5] = 第三大 (d) x[4] = 第四大 (e) x[3] = 第五大 (f) x[2] = 第六大

6 -23 Final: x[0] x[2] x[6] x[5]x[4] x[7] x[3] x[1] The heapsort should be implemented by an array, not by a binary tree time complexity: O(nlogn) (in the worst case) (g) x[1] = 第七大

6 -24 Insertion sort e.g. ( 由大而小 sort) pass 1 pass 2 pass 3 pass 4

6 -25 方法 : 每次處理一個新的資料時, 一定 insert 至適當 的位置才停止. 需要 n-1 個 pass best case: 未 sort 前, 已按順序排好, 每個 pass 僅需一次比較, 共需 (n-1) 次比較. worst case: 未 sort 前, 按相反順序排好, 比較次數 為 : Time complexity: O(n 2 ) Insertion sort

6 -26 void insertsort(int x[], int n) { int i, k, y; //initially x[0] may be thought of as a sorted //file of one element. After each repetition of //the following loop, the elements x[0] through //x[k] are in order for (k = 1; k < n; k++){ /* Insert x[k] into the sorted file */ y = x[k]; /* Move down 1 position all elements greater*/ /* than y */ for (i = k-1; i >= 0 && y < x[i]; i--) x[i+1] = x[i]; /* Insert y at proper position */ x[i+1] = y; } /* end for */ } /* end insertsort */

6 -27 Shell sort (diminishing increment sort) 方法 : insertion sort 是相鄰兩個資料做比較, 再決定是否互換. Shell sort 則是相距為 d 的 兩個 " 比較與互換 "(compare and exchange). d 為任意大於 1 的整數, 但在最後一個 pass, d 必須為 1.

6 -28 e.g. 由大到小 sort pass 1: d 1 = = d1d pass 2: d 2 = = = 3 d d2d2 pass 3: d 3 = = = 2 d d3d3 pass 4: d 4 = = = 1 d d4d4

6 -29 每個 pass 均進行多組的 insertion sort. 若一開始 d=1, 則與 insertion sort 完全一樣 Knuth 證明 : d i-1 = 3d i +1, 即 d i = 為最好 d i time complexity: O(nlog 2 n)~O(n 3/2 ) 適合數百個資料之 sorting

6 -30 void shellsort (int x[], int n, int incrmnts[], int numinc) { int incr, j, k, span, y; for (incr = 0;incr < numic; incr++){ /* span is the size of the increment */ span = incrmnts[incr]; for (j = span; j< n;j++){ /* Insert element x[j] into its proper */ /* position within its subfile */ y = x[j]; for (k = j-span; k >= 0 && y < x[k]; k -= span) x[k+span] = x[k]; x[k+span] = y; } /* end for */ } /* end shellsort */

6 -31 address caculation sort (sorting by hashing) e.g. 由小到大 sort input data: 分成 10 個 subfile, 每個 subfile 是一個 linked list, 其資料由小而大排列 假設有 n 個資料, m 個 subfile best case: 1, 且 uniform distribution time complexity: O(n) n m n m worst case: >>1, 或 not uniform distribution time complexity: O(n 2 ) ~ ~

6 -32 Two-way merge Merge two sorted sequences into a single one. e.g. [ ][ ] [ ] merge 設兩個 sorted lists 長度各為 m, n time complexity: O(m+n)

6 -33 Merge sort e.g. ( 由小而大 ) [25] [57] [48] [37] [12] [92] [86] [33] [25 57] [37 48] [12 92] [33 86] [ ] [ ] [ ] pass 1 pass 2 pass 3 需要 log 2 n 個 pass time complexity: O(nlogn) It can be implemented by a recursive function.

6 -34 Radix sort e.g. 由小到大的 sort ) 1) 01,31,11,21 2) 02 3) 13 4) 5) 05 6) 26,16 7) 27 8) 9) 19, ) 01,02,05,09 1) 11,13,16,19 2) 21,26,27 3) 31 4) 5) 6) 7) 8) 9) input data pass 1 merge pass 2 merge

6 -35 方法 : (1) 每個資料不與其它資料做比較, 只看自己放在 何處 (2)pass 1: 從個位數開始處理, 若是個位數為 1, 則放在 bucket 1, 以此類推 (3)pass 2: 處理十位數 好處 : 速度快, time complexity: O(nlog p k) k: input data 之最大數 p: 以 p 為基底 log p k: 位數之長度 缺點 : 需要額外的 memory( 可使用 linked list, 將所需 memory 減至最少, 但會增加時間 ).