initially treat data as n sorted collections that are each one datum long. merge merge each...

Post on 20-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

top related