alg0183 algorithms & data structures lecture 12 taxonomies of sorting algorithms 8/25/20091...

18
ALG0183 Algorithms & Data Structures Lecture 12 Taxonomies of Sorting Algorithms 8/25/2009 1 ALG0183 Algorithms & Data Structures by Dr Andy Brooks xonomy/flokkunarkerfi Sorting Algorithms @ YouTube http://www.youtube.com/watch?v=INHF_5RIxTE

Upload: merilyn-burke

Post on 02-Jan-2016

221 views

Category:

Documents


3 download

TRANSCRIPT

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

1

ALG0183 Algorithms & Data Structures

Lecture 12Taxonomies of Sorting Algorithms

8/25/2009

taxonomy/flokkunarkerfi

Sorting Algorithms @ YouTubehttp://www.youtube.com/watch?v=INHF_5RIxTE

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

28/25/2009

case study article

“An inverted taxonomy of sorting algorithms”Susan M. MerritCommunications of the ACM, Volume 28, Number 1, (1985), pp 96-99

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

3

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

Traditional Taxonomy due to D.E.Knuth

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

4

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.

8/25/2009

Traditional Taxonomy due to D.E.Knuth

if a[j] > a[j+1] then swap(a[j],a[j+1])

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

5

Selection Sort

• The smallest (largest) item is located and separated from the rest. The next smallest (largest) is selected and so on.

8/25/2009

Traditional Taxonomy due to D.E.Knuth

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.

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

6

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

Traditional Taxonomy due to D.E.Knuth

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

7

Merge Sort• Two already sorted lists are merged into a single,

sorted list.

8/25/2009

Traditional Taxonomy due to D.E.Knuth

http://www.sparknotes.com/cs/recursion/examples/section3.rhtml

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

8

Figure 3. The Traditional Taxonomy of Sorting Algorithms©ACM

8/25/2009

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

9

An Inverted Taxonomytop-down rather than bottom-up

8/25/2009

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.

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

10

Figure 1. A Top-Down High-Level Sort Process ©ACM

8/25/2009

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

11

Figure 2. Merge Sort and Quicksort ©ACM

8/25/2009

easy

easy

hard

hard

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

12

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 worse-case

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

singleton/einstaksmengi

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

13

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

14

Sinking Sortfrom Remarks on “A Synthesis of Serving Sorting Algorithms” by John Darlington by David R. Barstow, Acta Informatica 13, 225-227 (1980) © Springer-Verlag

8/25/2009

Input to the right of the ^ and output to the left of the ^.

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

15

Bubble Sortfrom Remarks on “A Synthesis of Serving Sorting Algorithms” by John Darlington by David R. Barstow, Acta Informatica 13, 225-227 (1980) © Springer-Verlag

8/25/2009

Input to the left of the ^ and output to the right of the ^.

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

16

Figure 4. Inverted Taxonomy of Sorting Algorithms©ACM

8/25/2009

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

17

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

18

Figure 6. A logical inverted taxonomy of sorting algorithmsPresented in an article with the same title.

8/25/2009