shell and merge sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/coms262/lectures/spring...

35
Shell and Merge Sorts Lecture 33 Sections 13.1, 13.4 Robb T. Koether Hampden-Sydney College Fri, Apr 9, 2010 Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 1 / 23

Upload: trinhkhanh

Post on 09-Jun-2019

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Shell and Merge SortsLecture 33

Sections 13.1, 13.4

Robb T. Koether

Hampden-Sydney College

Fri, Apr 9, 2010

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 1 / 23

Page 2: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Outline

1 Sorting

2 Comparison of Run Times

3 The Shell Sort

4 The Merge Sort

5 Assignment

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 2 / 23

Page 3: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Outline

1 Sorting

2 Comparison of Run Times

3 The Shell Sort

4 The Merge Sort

5 Assignment

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 3 / 23

Page 4: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Sorting

Definition (Sort)To sort a list is to arrange the members into either increasing order ordecreasing order.

Definition (Total Order Relation)A total order relation on a set is a relation that has the following twoproperties.

Transitivity: If a < b and b < c, then a < c.Trichotomy: Either a = b, or a < b, or a > b.

The order is determined by an order operator: <, >, <=, or >=.This operator must define a total order on the class.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 4 / 23

Page 5: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Inefficient Sorting Algorithms

Most elementary sorting algorithms are inefficient for long lists.Examples

I Bubble Sort.I Selection Sort.I Insertion Sort.

These algorithms have run times of order O(n2).

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 5 / 23

Page 6: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Efficient Sorting Algorithms

The efficient sorting algorithms are more complicated.Examples

I Shell SortI Merge SortI Quick Sort

These algorithms have run times of order O(n log n).

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 6 / 23

Page 7: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Outline

1 Sorting

2 Comparison of Run Times

3 The Shell Sort

4 The Merge Sort

5 Assignment

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 7 / 23

Page 8: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Comparison of Algorithms

How much fast is O(n log n) than O(n2)? Let’s compare.Let A be an algorithm of order O(n2).Let B be an algorithm of order O(n log n).Suppose both algorithms require 1 µsec to process a list of sizen = 100.How long will they take to process lists of sizes 103, 104, 105, 106,107, 108, and 109?

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 8 / 23

Page 9: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Comparison of Algorithms

Algorithm A has run time

n2

1002 = 0.0001n2.

Algorithm B has run time

n log n100 log 100

= 0.005n log n.

Evaluate these functions when n = 102, 103, 104, 105, 106, 107,108, and 109.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 9 / 23

Page 10: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Comparison of Algorithms

n Algorithm A Algorithm B102 1 µs 1 µs103 100 µs 15 µs104 10 ms 200 µs105 1 s 2.5 ms106 100 s 30 ms107 2.8 h 350 ms108 11.6 d 4 s109 3.2 y 45 s

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 10 / 23

Page 11: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Outline

1 Sorting

2 Comparison of Run Times

3 The Shell Sort

4 The Merge Sort

5 Assignment

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 11 / 23

Page 12: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Shell Sort

The Shell Sort is a variation of the Insertion Sort.The run time is O(n log n) with probability near 100%.The worst case has run time O(n2) with probability near 0%.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 12 / 23

Page 13: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Shell Sort Algorithm

Start with a “gap” equal to half the size of the list.Perform Insertion Sorts on the interwoven sublists (k = gap).

{a0,ak ,a2k , . . .}{a1,ak+1,a2k+1, . . .}{a2,ak+2,a2k+2, . . .}

...{ak−1,a2k−1,a3k−1, . . .}.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 13 / 23

Page 14: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Shell Sort

Example (The Shell Sort)Let the list be

{30,60,80,20,90,50,10,70,40}.

Begin with a list of size n = 9.Initialize gap = 4.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 14 / 23

Page 15: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Example: The Shell Sort

Example (The Shell Sort)

60 80 20 50 10 70

60 80 20 50 10 70

30 90 40

30 40 90

0 84

0 84

Pass #1, 1st sublist.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 15 / 23

Page 16: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Example: The Shell Sort

Example (The Shell Sort)

30 80 20 40 10 70 90

30 80 20 40 10 70 90

5

5

60 50

50 60

1

1

Pass #1, 2nd sublist.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 15 / 23

Page 17: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Example: The Shell Sort

Example (The Shell Sort)

30 50 20 40 60 70 90

30 50 20 40 60 70 90

2

2 6

80 10

10 80

6

Pass #1, 3rd sublist.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 15 / 23

Page 18: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Example: The Shell Sort

Example (The Shell Sort)

30 50 10 40 60 80 90

30 50 10 40 60 80 90

20 70

20 70

3 7

3 7

Pass #1, 4th sublist.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 15 / 23

Page 19: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Example: The Shell Sort

Example (The Shell Sort)Divide gap by 2 and repeat the above procedure.Quit when gap = 0.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 16 / 23

Page 20: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Shell Sort

Example (The Shell Sort)

50 20 60 70

50 20 60 70

30 10 40 80 90

10 30 40 80 90

2 60 4

2 60 4 8

8

Pass #2, 1st sublist.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 17 / 23

Page 21: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Shell Sort

Example (The Shell Sort)

10 30 40 80 90

10 30 40 80 90

60 20 50 70

20 50 60 70

3 71 5

3 71 5

Pass #2, 2nd sublist.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 17 / 23

Page 22: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Shell Sort

Example (The Shell Sort)

10 20 30 50 40 60 80 70 90

10 20 30 40 50 60 70 80 90

3 71 5

3 71 5

2 60 4 8

2 60 4 8

Halve the gap: gap = 1.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 17 / 23

Page 23: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Outline

1 Sorting

2 Comparison of Run Times

3 The Shell Sort

4 The Merge Sort

5 Assignment

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 18 / 23

Page 24: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Merging two sorted lists of length n has run time O(n).The run time of the Merge Sort is O(n log n).

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 19 / 23

Page 25: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort Algorithm

Begin by considering the list to be a collection of sublists each oflength 1.

50 30 70 20 80 40 10 60

Merge adjacent sublists in pairs.Continue to merge adjacent sublists until there remains only onesublist.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 20 / 23

Page 26: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

50 30 70 20 80 40 10 60

Begin with a list of size n = 8.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 27: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

30 50 70 20 80 40 10 60

Pass #1: Sublist size = 1.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 28: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

30 50 20 70 80 40 10 60

Pass #2: Sublist size = 2.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 29: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

30 50 20 70 40 80 10 60

Pass #2: Sublist size = 2.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 30: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

30 50 20 70 40 80 10 60

Pass #2: Sublist size = 2.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 31: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

20 30 50 70 40 80 10 60

Pass #3: Sublist size = 4.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 32: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

20 30 50 70 10 40 60 80

Pass #3: Sublist size = 4.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 33: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

The Merge Sort

Example (The Merge Sort)

10 20 30 40 50 60 70 80

Pass #4: Sublist size = 8.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 21 / 23

Page 34: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Outline

1 Sorting

2 Comparison of Run Times

3 The Shell Sort

4 The Merge Sort

5 Assignment

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 22 / 23

Page 35: Shell and Merge Sorts - people.hsc.edupeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2010/Lecture...Outline 1 Sorting 2 Comparison of Run Times 3 The Shell Sort 4 The Merge

Assignment

HomeworkRead Section 13.1, pages 721 - 732.Read Section 13.4, pages 762 - 769.

Robb T. Koether (Hampden-Sydney College) Shell and Merge Sorts Fri, Apr 9, 2010 23 / 23