initially Treat data as N sorted collections that are each one datum long. merge Merge each...

32
initially Treat data as N sorted collections that are each one datum long. merge Merge each consecutive pair of collections to form sorted collections twice as large repeat repeat the merge step over and over until a single sorted collection is formed. A more efficient sorting algorithm results from repeatedly merging small sorted lists to make larger sorted lists. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Transcript of initially Treat data as N sorted collections that are each one datum long. merge Merge each...

Page 1: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initiallyTreat data as N sorted collections that are each one datum long. mergeMerge each consecutive pair of collections to form sorted collections twice as large

repeatrepeat the merge step over and over until a single sorted collection is formed.

A more efficient sorting algorithm results from repeatedly merging small sorted lists to make larger sorted lists.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 2: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Treat data as N sorted collections that are each one datum long.

1st merge

Example (of merge sort)

5

5 8 6 2 9 4 3 0

8 6 2 9 4 3 0

5 8 2 6 4 9 0 3

2nd merge

2 5 6 8 0 3 4 9

3rd merge

0 2 3 4 5 6 8 9

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 3: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 4: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 5: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 6: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 7: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 8: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 9: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 10: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 11: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 12: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 13: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4 5

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 14: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4 5

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 15: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4 5 6

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 16: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4 5 6

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 17: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4 5 6 8

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 18: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

What is the most efficient way to merge two sorted lists to make a larger sorted list?

Example

2 5 6 8 0 3 4 9

0 2 3 4 5 6 8 9

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 19: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

Number of merges5 8 6 2 9 4 3 0

5 8 2 6 4 9 0 3

2 5 6 8 0 3 4 9

0 2 3 4 5 6 8 9

2 5 6 8 0 3 4 9

0 2 3 4 5 6 8 9

Number of Probes per merge

AVERAGE Number of Probes per merge

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 20: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

Partitioning is done with respect to a pivot, which is one value of the collection being partitioned.

Another efficient sorting algorithm results from recursively partitioning collections into two smaller groups until every group is of size one (singleton groups), then concatenating all of the singleton groups.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 21: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 22: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 23: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

2nd pivot left == 2

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 24: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

2nd pivot left == 2

0 4 3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 25: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

2nd pivot right == 8

2nd pivot left == 2

0 4 3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 26: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

2nd pivot right == 8

6 9

2nd pivot left == 2

0 4 3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 27: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

2nd pivot right == 8

6 9

2nd pivot left == 2

3rd pivot == 4

0 4 3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 28: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

2nd pivot right == 8

6 9

2nd pivot left == 2

3rd pivot == 4

0 4 3

3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 29: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

2 4 3 0 8 6 9

2nd pivot right == 8

6 9

concatenate

2nd pivot left == 2

3rd pivot == 4

0 4 3

3

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 30: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

2 4 3 0 8 6 9

2nd pivot right == 8

6 9

concatenate

0 2 3 4 5 6 8 9

2nd pivot left == 2

3rd pivot == 4

0 4 3

3

initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5

Example (of quicksort)

5 8 6 2 9 4 3 0

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 31: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

public Group quickSort(Group g) { Comparable pivot; Group small, large;

if (sizeOf(g) <= 1) { return g; } else { pivot = selectedPivot(g); //partition g into small and large return concatenate( quickSort(small), pivot, quickSort(large)); }}

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 32: initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

Quicksort is more difficult to anlyze.

However, in practice ...

Worst Case?

Best Case?

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.