ppt

advertisement
taxonomy/flokkunarkerfi
ALG0183 Algorithms & Data Structures
Lecture 12
Taxonomies of Sorting Algorithms
Sorting Algorithms @ YouTube
http://www.youtube.com/watch?v=INHF_5RIxTE
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
1
case study article
“An inverted taxonomy of sorting algorithms”
Susan M. Merrit
Communications of the ACM, Volume 28, Number 1, (1985), pp 96-99
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
2
Traditional Taxonomy due to D.E.Knuth
Insertion Sort
• Items are considered one at a time. Each new item is
inserted into the appropriate place relative to the
previously sorted items.
• Having a sorted subarray A[1 . . j -1], we insert the
single element A[j] into its proper place, yielding the
sorted subarray A[1 . . j].
• Insertion sort is used when sorting a hand of cards.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
3
Traditional Taxonomy due to D.E.Knuth
Exchange Sort
• If two elements are out of order, they are
exchanged. The process repeats until all
possible comparisons have been made or until
no more exchanges are possible.
if a[j] > a[j+1] then
swap(a[j],a[j+1])
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
4
Traditional Taxonomy due to D.E.Knuth
Selection Sort
• The smallest (largest) item is located and
separated from the rest. The next smallest
(largest) is selected and so on.
In the example in the video, additional space
is used to perform the sort.
Sorting can be done in-place by swapping
the least remaining item with the item in the
next position to be filled.
A sorting algorithm is described as in-place if
the sorted items occupy the same storage as
the original ones.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
5
Traditional Taxonomy due to D.E.Knuth
The focus of Insertion Sort, Exchange Sort,
and Selection Sort.
• “The focus is on an operation on an individual
element or pair of elements.” Merrill
• “The method of choosing the individual element
or elements is the key to the algorithm.” Merrill
• “The algorithm is built by constructing an
iteration about the focal operation.” Merrill
• The focus of the traditional taxonomy is low-level
and bottom-up.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
6
Traditional Taxonomy due to D.E.Knuth
Merge Sort
• Two already sorted lists are merged into a single,
sorted list.
http://www.sparknotes.com/cs/recursion/examples/section3.rhtml
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
7
Figure 3. The Traditional Taxonomy of Sorting Algorithms
©ACM
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
8
An Inverted Taxonomy
top-down rather than bottom-up
Sorting can be viewed as a three-step process:
1. Split the set S to be sorted into parts, say S1 and S2.
2. Sort each part, giving S1´ and S2´.
3. Join the parts to produce the sorted set S´.
The process has a split procedure and a join procedure.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
9
Figure 1. A Top-Down High-Level Sort Process
©ACM
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
10
Figure 2. Merge Sort and Quicksort ©ACM
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
11
singleton/einstaksmengi
Insertion Sort and Selection Sort
• What if we do not split into equal-size parts?
• Suppose set S2 has only one element, then merge sort
collapses into insertion sort, and quicksort collapses into
selection sort.
• Knuth recognised that merging a singleton sequence with
an arbitrary sequence is essentially insertion sort.
– Neverthless he went ahead and placed these sorts into different
categories.
• Several authors have recognised that quicksort´s worsecase performance occurs when it degrades into selection
sort by partitioning a singleton set at each split.
– Yet quicksort and selection sort are in different categories in the
traditional taxonomy.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
12
Exchange Sorts
• Sinking sort can be seen as an in-place version
of insertion sort.
• Bubble sort can be seen as an in-place version
of selection sort.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
13
Sinking Sort
from Remarks on “A Synthesis of Serving Sorting Algorithms” by John Darlington by
David R. Barstow, Acta Informatica 13, 225-227 (1980) © Springer-Verlag
Input to the right of the ^ and output to the left of the ^.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
14
Bubble Sort
from Remarks on “A Synthesis of Serving Sorting Algorithms” by John Darlington by
David R. Barstow, Acta Informatica 13, 225-227 (1980) © Springer-Verlag
Input to the left of the ^ and output to the right of the ^.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
15
Figure 4. Inverted Taxonomy of Sorting Algorithms
©ACM
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
16
What about distributive sorting algorithms?
• Distributive sorting algorithms do not fit into
the inverted taxonomy presented in Figure 4.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
17
Figure 6. A logical inverted taxonomy of sorting algorithms
Presented in an article with the same title.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
18
Download