ch11-sort - Ch. 11 Sorting: 11.1 Introduction: Def....

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 1 Ch. 11 Sorting: 11.1 Introduction: Def. sorting: a process by which a collection of items is placed into ascending or descending order on one of the fields (the key) of the items. Type of Sorts: 1. Internal sort - whole file in main memory. b a data structure problem. 2. External sort - Whole file in secondary memory. - Handled by - bring subsets into mm, sort via internal methods, then merge results. b a file structure problem.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 2 Best method chosen by current circumstances / factors: 1. Length of list. 2. Length of record – vis-a-vis methods requiring swapping of records. i.e. > length > time. 3. Special features – already partially sorted? 4. Avg. vs. best-case performance – some algorithms do dramatically better in best-case than for avg.-case. Does applic n warrant this algorithm? 5. Avg. vs. worst-case performance – some algorithms degrade dramatically when encountering worst-case. How likely is worst-case? How bad is the ‘degrading’? 6. Duplicate keys – if occurring, should stay in relative order. ( a stable sort) Not all alg’m are stable. 7. Machine architecture – may have slow swaps, but fast comparisons dictates method.
Background image of page 2
2001-2009 M. D. Evans All Rights Reserved 3 Note: 1. Large records - avoid predominantly swapping alg’ms. - swap pointers to data fields. - 2. Best depends on - time used to sort records. - space req’d i.e. a ‘holding’ array. - 3. Comparison of methods based on time: - code algorithms, run them for experimental data. But won’t give absolute times because system times also included. ie. heavy system used. Total time = sort time + context switch time. - mathematical analysis – - count operations – comparisons + swaps. Ex. ¾(n 2 - 2n), 6(n 2 + 4n) O(n 2 ). 4. Has been shown that best sort algorithm on avg. can be no better than O(nlog 2 n).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 4 The Sorting Techniques: 3 ‘broad’ categories: 1. Exchange algorithms. 2. Selection algorithms. 3. Insertion algorithms. For each method: - example - algorithm - analysis - time order - advantages - disadvantages
Background image of page 4
2001-2009 M. D. Evans All Rights Reserved 5 11.2 Bubble Sort: Example: 32 4 27 19 10 21 4 32 27 32 19 32 10 32 21 32 pass 1 4 27 19 10 21 32 19 27 10 27 21 27 32 pass 2 4 19 10 21 27 32 pass 3 4 10 19 pass 4 4 10 19 21 27 32 pass 5 Algorithm: FOR pass = 1 to n -1 FOR j = 1 to n – pass IF list [j] > list [j + 1] THEN swap (list [j], list [j + 1];
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 6 Analysis: pass 1 n - 1 comparisons 2 n – 2 comparisons (n – 1) 1 comparison comparisons = (n – 1) + (n – 2) + …. + 1
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

ch11-sort - Ch. 11 Sorting: 11.1 Introduction: Def....

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online