76
Data Structures and Algorithms Course’s slides: Sorting Algorithms www.mif.vu.lt/~algis

Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Data Structuresand

Algorithms

Course’s slides: Sorting Algorithms

www.mif.vu.lt/~algis

Page 2: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Sorting

• Card players all know how to sort …• First card is already sorted•With all the rest

!Scan back from the end until you find the first card larger than the new one,"Move all the lower ones up one slot #insert it

$

Q

%

2

%

9&

A

&

K

&

10

'

J

'

2&

2

%

9#

Page 3: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

SortingSorting is ordering a list of objects. • Two types of sorting:• If the number of objects is small enough to fit into the main

memory, sorting is called internal sorting. • If the number of objects is so large that some of them reside on

external storage during the sort, it is called external sorting. • The following internal sorting algorithms:• Bucket sort• Bubble sort• Insertion sort• Selection sort• Quicksort• Heapsort•Mergesort

Page 4: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Bucket sorting – O(n) algorithmBucket Sort:

• Suppose we need to sort an array of positive integers {3,11,2,9,1,5}.

• A bucket sort works as follows: create an array of size 11.

• Then, go through the input array and place integer 3 into a second array at index 3, integer 11 at index 11 and so on.

•We will end up with a sorted list in the second array.

• Suppose we are sorting a large number of local phone numbers, for example, all residential phone numbers in the 85 area code region (about 1 million).

Page 5: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Bucket sorting – O(n) algorithm•We sort the numbers without use of comparisons in the following

way: • Create a bit array of size 107 , it takes about 1Mb. • Set all bits to 0. • For each phone number turn-on the bit indexed by that phone

number. • Finally, walk through the array and for each bit 1 record its

index, which is a phone number.• Immediately see two drawbacks to this sorting algorithm:• Firstly, we must know how to handle duplicates. • Secondly, we must know the maximum value in the unsorted

array.

Page 6: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Bucket sorting – O(n) algorithm•We must also have enough memory - it may be impossible to

declare an array large enough on some systems.• The first problem is solved by using linked lists, attached to each

array index. • All duplicates for that bucket will be stored in the list. • Another possible solution is to have a counter. • As an example let us sort 3, 2, 4, 2, 3, 5 start with array of 5

counters set to zero:

•Moving through the array we increment counters:

• Read off the number of each occurrence: 2 2 3 3 4 5

0 1 2 3 4 5 0 0 0 0 0 0

0 1 2 3 4 5 0 0 2 2 1 1

Page 7: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Selection Sort - O(n2) algorithms• Selection sort is a simple sorting algorithm. • This sorting algorithm is an in-place comparison-based

algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. • Initially, the sorted part is empty and the unsorted part is

the entire list.• The smallest element is selected from the unsorted array

and swapped with the leftmost element, and that element becomes a part of the sorted array.

Page 8: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Selection Sort - O(n2) algorithms• This process continues moving unsorted array boundary by

one element to the right.• This algorithm is not suitable for large data sets as its

average and worst case complexities are of Ο(n2), where n is the number of items.• Selection sorting works according to the prescript:• first find the smallest element in the array and exchange it

with the element in the first position,• then find the second smallest element and exchange it with

the element in the second position, etc.

Page 9: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Selection Sort - O(n2) algorithmsThis is a full implementation of this process: procedure selection; var i, j, min, t: integer;

begin

for i := 1 to N -1 do

begin min := i ;

for j := i+1 to N do

if a [j] < a[min] then min := j ; t := a [min]; a [min] := a [i] := t

end; end;

Complexity: O (n2) - about N2/2 comparisons and N exchanges

Page 10: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Insertion sort – O(n2) algorithm• Insertion sort:• Input: sequence of n numbers a1, a2, . . .,an. • Output: A permutation (reordering) of the input sequence

such that the numbers that we wish to sort are also known as the keys.

• Start with insertion sort, which is an efficient algorithm for sorting a small number of elements. • Insertion sort works the way many people sort a hand of

playing cards. •We start with an empty left hand and the cards face down on

the table.

Page 11: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Insertion sort – O(n2) algorithm•We then remove one card at a time from the table and insert

it into the correct position in the left hand.

• To find the correct position for a card, we compare it with each of the cards already in the hand, from right to left.

• At all times, the cards held in the left hand are sorted, and these cards were originally the top cards of the pile on the table.

Page 12: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Insertion sort – O(n2) algorithm2020-02-03 18:34insertionsort.png 562×569 pixels

Page 1 of 1https://media.geeksforgeeks.org/wp-content/uploads/insertionsort.png

Page 13: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Insertion sort – O(n2) algorithm

Insertion-sort (A)for i = 1 to n

key ← A [i]j ← i – 1

while j > = 0 and A[j] > key A[j+1] ← A[j] j ← j – 1

end while A[j+1] ← key

end for

Page 14: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Insertion sort• Complexity

• For each card

• Scan O(n)

• Shift up O(n)

• Insert O(1)

• Total O(n)

• First card requires O(1), second O(2), …

• For n cards - operations O(n2)

Page 15: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Shellsort – O(n2) or O(n3/2) or …Shellsort or Shell's method, is in-place comparison sort, it is a generalization of insertion sort.

The method starts by sorting elements far apart from each other and progressively reducing the gap between them.

The list of elements considering every hth element gives a sorted list.

Such a list is said to be h-sorted.

If the file is then k-sorted for some smaller integer k, then the file remains h-sorted.

Following this idea for a decreasing sequence of h values ending in 1 is guaranteed to leave a sorted list in the end.

Page 16: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Shellsort• An example run of Shellsort with gaps 7, 3 and 1:

3 7 9 0 5 1 6 8 4 2 0 6 1 5 7 3 4 9 8 2

3 7 9 0 5 1 6 3 3 2 0 5 1 58 4 2 0 6 1 5 -> 7 4 4 0 6 1 67 3 4 9 8 2 8 7 9 9 8 2

The new data: 3 3 2 0 5 1 5 7 4 4 0 6 1 6 8 7 9 9 8 2are divided into columns by 3 elements

Page 17: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

ShellsortAlgorithm:h = 1

while h < n, h = 3*h + 1 while h > 0, h = h / 3

for k = 1 : h, insertion sort a[ k : h : n]→ invariant: each h-sub-array is sorted

end

• Not stable

• O(1) extra space

• O(n3/2) time, depend on sequence h (neither tight upper bounds nor the best increment sequence are known)

• Adaptive: O(n · lg (n) ) time when nearly sorted

Page 18: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Bubble sort – O(n2)• Bubble sort is a simple sorting algorithm.

• This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order.

• This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2).

• From the first element

• Exchange pairs if they’re out of order

• Last one must now be the largest

• Repeat from the first to n-1

• Stop when you have only one element to check

Page 19: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Bubble sort – O(n2)Array of numbers " 5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. Three passes will be required:

Pass one: ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), swap 5 > 1( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), swap 5 > 4( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), swap 5 > 2( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), no swap

Pass two: ( 1 4 2 5 8 ) → ( 1 4 2 5 8 )( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), swap 4 > 2( 1 2 4 5 8 ) → ( 1 2 4 5 8 )

Page 20: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Bubble sort – O(n2)Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted:Pass three: ( 1 2 4 5 8 ) → ( 1 2 4 5 8 )

( 1 2 4 5 8 ) → ( 1 2 4 5 8 )

Page 21: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Bubble Sort – O(n2)begin

for i = 1 to n−1 for j = 1 to n − i

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

end • The run-time for the best case input and the worst case

input: irrespective of the nature of input, the number of passes to be made is n − 1. • In case of best case input, there is no swapping and for every

other input swapping may be required during each pass. • So, the number of comparisons is n − 1 + n − 2 + . . . + 2 + 1 =

O(n2) for all inputs.

Page 22: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Divide and Conquer ApproachThe most well known algorithm design strategy:1. Divide the problem into two or more smaller

subproblems.2. Conquer the subproblems by solving them recursively.3. Combine the solutions to the subproblems into the

solutions for the original problem.Divide and conquer is an algorithm design paradigm based on multi-branched recursion. A divide-and-conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly.

Page 23: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

A Typical Divide and Conquer Case

subproblem 2 of size n/2

subproblem 1 of size n/2

a solution to subproblem 1

a solution tothe original problem

a solution to subproblem 2

a problem of size n

Divide/Break

Conquer/Solve

Merge/CombineExamples:Sorting: mergesort, quicksortTree traversalsBinary searchMatrix multiplication-Strassen’s algorithmClosest pair (points)

Page 24: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

QuicksortHighly efficient sorting algorithm Quicksort is based on partitioning of array of data into smaller arrays: Discovered by C.A.R. Hoare

Two phases:

• Partition phase

• Divides the work into half• Sort phase

• Conquers the halves!

A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.

Page 25: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort• Partition• Choose a pivot• Find the position for the pivot so that • all elements to the left are less• all elements to the right are greater

< pivot > pivotpivot

Page 26: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort• Conquer• Apply the same algorithm to each half

< pivot > pivot

pivot< pʼ pʼ > pʼ < p” p” > p”

It works by partitioning a file into two parts, then sorting the parts independently. The exact position of the partition depends on the file, and the algorithm has the following recursive structure:

If the partition method can be made more precise, (for example, choosing right-most element as a partition element), and the recursive call of this procedure may be eliminated, then the implementation of the algorithm looks like this: procedure quicksort (l,r: integer); var v, t, i, j: integer; begin if r>l then begin v:=a[r]; i:=l-1; j:=r; repeat repeat i:=i+1 until a[i]=>v; repeat j:=j-1 until a[j]=<v; t:=a[i]; a[i]:=a[j]; a[j]:=t; until j=<i; a[j]:=a[i]; a[i]:=a[r]; a[r]:=t; quicksort (l,i-1); quicksort (i+1,r) end end; An example of a partitioning of a larger file (choosing the right-most element):

The crux of the method is the partition procedure, which must rearrange the array to make the following conditions hold: � the element a[i] is in its final place in the array for some i; � all the elements left to the a[i] are less then or equal to it; � all the elements right to the a[i] are greater then or equal to it.

The first improvement: Removing Recursion The recursion may be removed by using an explicit pushdown stack, which is containing "work to be done" in the form of subfiles to be sorted. Any time we need a subfile to process, we pop the stack. When we partition, we create two subfiles to be processed which can be pushed on the stack. This leads to the following nonrecursive implementation:

procedure quicksort; var t, i, 1, r: integer; begin l:=1; r:=N; stackinit; push (l); push (r);

Page 27: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Sorting – Quicksort

Implementation:quicksort( void *a, int low, int high )

{int pivot;/* Termination condition! */if ( high > low )

{pivot = partition( a, low, high );quicksort( a, low, pivot-1 );quicksort( a, pivot+1, high );}

}

Divide

Conquer

Page 28: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort - Partition/* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ partition (arr[], low, high) { // pivot (Element to be placed at right position) pivot = arr [high]; i = (low - 1) // Index of smaller element for (j = low; j <= high- 1; j++) { // If current element is smaller than the pivot if (arr[j] < pivot) { i++; // increment index of smaller element swap arr[i] and arr[j] } } swap arr[i + 1] and arr[high]) return (i + 1) }

Page 29: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort – Analysis• Partition• Check every item once O(n)

• Conquer• Divide data in half O(log2n)

• Total• Product O(n log n)

• Quicksort is generally fasterFewer comparisons

Page 30: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort•What happens if we use quicksort on data that’s already

sorted (or nearly sorted)?

• Sorted data

1 2 3 4 5 6 7 8 9

pivot

< pivot

?

> pivot

Page 31: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort• Sorted data• Each partition

produces• a problem of size 0• and one of size n-1!

• Number of partitions?

1 2 3 4 5 6 7 8 9

> pivot

2 3 4 5 6 7 8 9

> pivot

pivot

pivot

Page 32: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort• Sorted data• Each partition

produces• a problem of size 0• and one of size n-1!

• Number of partitions?• n each needing time O(n)• Total n O(n)

or O(n2)

1 2 3 4 5 6 7 8 9

> pivot

2 3 4 5 6 7 8 9

> pivot

pivot

pivot

Page 33: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort• Quicksort’s O (n log n) behaviour• Depends on the partitions being nearly equal( there are O ( log n ) of them

• On average, this will nearly be the caseand quicksort is generally O (n log n)

• Can we do anything to ensure O (n log n) time?• In general, no• But we can improve our chances!!

Page 34: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort – Choice of the pivot• Any pivot will work …

• Choose a different pivot …

• so that the partitions are equal

• then we will see O(n log n) time

1 2 3 4 5 6 7 8 9

pivot

< pivot > pivot

Page 35: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort – Median-of-3 pivot• Take 3 positions and choose the median

(say … First, middle, last(median is 5

(perfect division of sorted data every time!(O(n log n) time

(Since sorted (or nearly sorted) data is common, median-of-3 is a good strategy

• especially if you think your data may be sorted!

1 2 3 4 5 6 7 8 9

Page 36: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort – Random pivot• Choose a pivot randomly

• Different position for every partition(On average, sorted data is divided evenly(O(n log n) time

• Key requirement• Pivot choice must take O(1) time

Page 37: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort

•Quicksort is generally faster

•Fewer comparisons and exchanges

•Some empirical data:

n Quick Heap InsertComp Exch Comp Exch Comp Exch

100 712 148 2842 581 2595 899200 1682 328 9736 9736 10307 3503500 5102 919 53113 4042 62746 21083

Page 38: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Quicksort vs Heap Sort•Quicksort•Generally faster•Sometimes O (n2)•Better pivot selection reduces probability

•Use when you want average good performance•Commercial applications, Information systems

•Heap Sort•Generally slower•Guaranteed O (n log n) … Can design this in!•Use for real-time systems•Time is a constraint

Page 39: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Mergesort• The merge sort algorithm is closely following the divide-and-

conquer paradigm. Intuitively, it operates as follows:

• Divide: Divide the n-element sequence to be sorted into two subsequences of (n/2) elements each.

• Conquer: Sort the two subsequences recursively using mergesort.

• Combine: Merge the two sorted subsequences to produce the sorted answer.

• Suppose we have two sorted arrays a[1...N], b[1...M], and we need to merge them into one. The mergesort algorithm is based on a merge procedure, which can be presented as follows:

Page 40: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Mergesort

Page 41: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Mergesort

procedure merge (a, b);var a,b,c: array [1..M + N)] of integer;begin i:=1; j:=1; a[M+1]:=maxint; b[N+1]:=maxint;

for k:=1 to M+N do if a[i]<b[j] then begin c[k]:=a[i]; i:=i+1 end else begin c[k]:=b[j]; j:=j+1 end; end;

Page 42: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

MergesortTo perform the mergesort, the algorithm for arrays can be as follows: • procedure mergesort(l,r: integer); var i,j,k,m: integer;

begin if r-l>0 then

• begin m:=(r+l) div 2;mergesort(l,m); mergesort(m+1,r);for i:=m downto l do b[i]:=a[i];for j:=m+1 to r do b[r+m+1-j]:=a[j];

• for k:=l to r do if b[i]<b[j] then • begin a[k]:=b[i]; i:=i+1 end else begin a[k]:=b[j]; j:=j-1 end;

• end;

• end;

Page 43: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Mergesort

Property 1: Mergesort requires about N lg N comparisons to sort any file of N elements.

Property 2: Mergesort is stable:

Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list.

Property 3: Mergesort is insensible to the initial order of its input.

Page 44: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

External sort

• A classic problem in computer science!

• Advantages of requesting data in sorted order:

• gathers duplicates

• allows for efficient searches

• Sorting is first step in bulk loading database index (as B+ tree index).

• Sort-merge join algorithm involves sorting.

• Problem: sort 20 Gb of data with 1 Gb of RAM.

• why not let the OS handle it with virtual memory?

Page 45: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Streaming Data through RAM• An important method for sorting & other DB operations

• Simple case:

• Compute f(x) for each record, write out the result

• Read a page from INPUT to Input Buffer

•Write f(x) for each item to Output Buffer

•When Input Buffer is consumed, read another page

•When Output Buffer fills, write it to OUTPUT

f(x)RAM

InputBuffer

OutputBuffer

OUTPUTINPUT

Page 46: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Streaming Data through RAM• Reads and Writes are not coordinated• E.g., if f ( ) is Compress ( ), you read many pages per write.• E.g., if f ( ) is DeCompress ( ), you write many pages per read.

f(x)RAM

InputBuffer

OutputBuffer

OUTPUTINPUT

Page 47: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

2-Way Sort: Requires 3 Buffers• Pass 0: Read a page, sort it, write it.

• only one buffer page is used

• Pass 1, 2, 3, …, etc.:

• requires 3 buffer pages

•merge pairs of runs into runs twice as long

• three buffer pages used.

Main memory buffers

INPUT 1

INPUT 2

OUTPUT

DiskDisk

Page 48: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Two-Way External Merge Sort• Each pass we read + write

each page in file.• N pages in the file => the

number of passes

• So total cost is:

• Idea: Divide and conquer: • sort subfiles and merge

! "= +log2 1N

! "( )2 12N Nlog +

Input file

1-page runs

2-page runs

4-page runs

8-page runs

PASS 0

PASS 1

PASS 2

PASS 3

9

3,4 6,2 9,4 8,7 5,6 3,1 2

3,4 5,62,6 4,9 7,8 1,3 2

2,34,6

4,78,9

1,35,6 2

2,34,46,78,9

1,23,56

1,22,33,44,56,67,8

Page 49: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

General External Merge Sort

• Insight #1: We can merge more than 2 input buffers at a time... affects fanout à base of log!

• Insight #2: The output buffer is generated incrementally, so only one buffer page is needed for any size of run!

• To sort a file with N pages using B buffer pages:

• Pass 0: use B buffer pages. Produce sorted runs of B pages each.

• Pass 2, ..., etc.: merge B - 1 runs, leaving one page for output.

More than 3 buffer pages. How can we utilize them?

! "N B/

Page 50: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

General External Merge Sort

• To sort a file with N pages using B buffer pages:

• Pass 0: use B buffer pages. Produce sorted runs of Bpages each.

• Pass 1, 2, …, etc.: merge B-1 runs.

! "N B/

B Main memory buffers

INPUT 1

INPUT B-1

OUTPUT

DiskDisk

INPUT 2

. . . . . .. . .

More than 3 buffer pages. How can we utilize them?

Page 51: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Cost of External Merge Sort• Number of passes:• Cost = 2 N * (# of passes)• E.g., with 5 buffer pages, to sort 108 page file:

• Pass 0: = 22 sorted runs of 5 pages each (last run is only 3 pages)

• Pass 1: = 6 sorted runs of 20 pages each (last run is only 8 pages)

• Pass 2: 2 sorted runs, 80 pages and 28 pages• Pass 3: Sorted file of 108 pages

! "! "1 1+ −log /B N B

! "108 5/

! "22 4/

Formula check: ┌log4 22┐= 3 … + 1 à 4 passes √

Page 52: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Number of Passes of External Sort

N B=3 B=5 B=9 B=17 B=129 B=257 100 7 4 3 2 1 1 1,000 10 5 4 3 2 2 10,000 13 7 5 4 2 2 100,000 17 9 6 5 3 3 1,000,000 20 10 7 5 3 3 10,000,000 23 12 8 6 4 3 100,000,000 26 14 9 7 4 4 1,000,000,000 30 15 10 8 5 4

( I/O cost is 2 N times number of passes)

Page 53: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

I/O for External Merge Sort• Do I/O a page at a time

• Not one I/O per record

• In fact, read a block (chunk) of pages sequentially!

• Suggests we should make each buffer (input/output) be a block of pages.

• But this will reduce fan-in during merge passes!

• In practice, most files still sorted in 2-3 passes.

Page 54: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Double Buffering• To reduce wait time for I/O request to complete, can prefetch

into `shadow blockʼ.

• Potentially, more passes; in practice, most files still sorted in 2-3 passes.

OUTPUTOUTPUT'

Disk Disk

INPUT 1

INPUT k

INPUT 2

INPUT 1'

INPUT 2'

INPUT k'

block sizeb

B main memory buffers, k-way merge

Page 55: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway merging• On each merge step, alternate the I/O units so that on one

step, a unit is an input file and on the next step it is an output file.

• Read one run from each of the input files, merge the runs, and store the resulting run on an output file.

• Characteristics of External Sorting

• During the sort, some of the data must be stored externally. Typically the data will be stored on tape or disk.

• The cost of accessing data is significantly greater than either bookkeeping or comparison costs.

• There may be severe restrictions on access. For example, if tape is used, items must be accessed sequentially.

Page 56: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway mergingCriteria for Developing an External Sorting Algorithm

•Minimize number of times an item is accessed.

• Access items in sequential order

Balanced Multiway Merging

• Strategy

• Select an equal number of I/O units (e.g., tapes, disks)

• Sort Step

• Select half of the I/O units to be output files

• Using an in-memory sorting algorithm, create the initial runs and divide them evenly among the output files

Page 57: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway mergingMerge Step• (a) On each merge step, alternate the I/O units so that on one

step, a unit is an input file and on the next step it is an output file• (b) Read one run from each of the input files, merge the runs,

and store the resulting run on an output file. Alternate output files so that the runs are evenly distributed on the output files.• (c) Repeat step b until all the runs on the input files have

been merged.• Repeat steps a - c until the file has been sorted

Page 58: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway mergingExample: Sort the keys

A S O R T I N G A N D M E R G I N G E X A M P L Eassuming that initial run sizes are 3 and that 6 I/O units used.Suppose that we have these records with the keys on an input tape; • these are to be sorted and put onto an output tape.

Using a "tape" simply means that we're restricted to reading the records sequentially: • the second record can't be read until the first is read, and

so on. Assume further that we have only enough room for three records in our computer memory but that we have plenty of tapes available.

Page 59: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway merging• The first step is to read in the file three records at a time, sort

them to make three-record blocks, and output the sorted blocks. • Thus, first we read in A S 0 and output the block A 0 S, next

we read in R T I and output the block I R T, and so forth. • Now, in order for these blocks to be merged together, they

must be on different tapes. • If we want to do a three-way merge, then we would use three

tapes.

Balanced Multiway Merging To begin, we'll trace through the various steps of the simplest sort-merge procedure for a small example. Suppose that we have records with the keys

A S 0 R T I N G A N D M E R G I N G E X A M P L E on an input tape; these are to be sorted and put onto an output tape. Using a "tape" simply means that we're restricted to reading the records sequentially: the second record can't be read until the first is read, and so on. Assume further that we have only enough room for three records in our computer memory but that we have plenty of tapes available.

The first step is to read in the file three records at a time, sort them to make three-record blocks, and output the sorted blocks. Thus, first we read in A S 0 and output the block A 0 S, next we read in R T I and output the block I R T, and so forth. Now, in order for these blocks to be merged together, they must be on different tapes. If we want to do a three-way merge, then we would use three tapes, ending up after the sorting pass with the configuration shown in figure:

Now we're ready to merge the sorted blocks of size three. We read the first record off each input tape (there's just enough room in the memory) and output the one with the smallest key. Then the next record from the same tape as the record just output is read in and, again, the record in memory with the smallest key is output. When the end of a three-word block in the input is encountered, that tape is ignored until the blocks from the other two tapes have been processed and nine records have been output. Then the process is repeated to merge the second three-word block on each tape into a nine-word block (which is output on a different tape, to get ready for the next merge). By continuing in this way, we get three long blocks configured as shown in figure next:

Now one more three-way merge completes the sort. If we had a much longer file with many blocks of size 9 on each tape, then we would finish the second pass with blocks of size 27 on tapes 1, 2, and 3, then a third pass would produce blocks of size 81 on tapes 4, 5, and 6, and so forth. We need six tapes to sort an arbitrarily large file: three for the input and three for the output of each three-way merge. (Actually, we could get by with just four tapes: the output could be put on just one tape, and then the blocks from that tape distributed to the three input tapes in between merging passes.)

Page 60: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway merging•We read the first record off each input tape (there's just

enough room in the memory) and output the one with the smallest key.

• Then the next record from the same tape as the record just output is read in and, again, the record in memory with the smallest key is output.

•When the end of a three-word block in the input is encountered, that tape is ignored until the blocks from the other two tapes have been processed and nine records have been output.

Page 61: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway merging• Then the process is repeated to merge the second three-word

block on each tape into a nine-word block (which is output on a different tape, to get ready for the next merge)

Balanced Multiway Merging To begin, we'll trace through the various steps of the simplest sort-merge procedure for a small example. Suppose that we have records with the keys

A S 0 R T I N G A N D M E R G I N G E X A M P L E on an input tape; these are to be sorted and put onto an output tape. Using a "tape" simply means that we're restricted to reading the records sequentially: the second record can't be read until the first is read, and so on. Assume further that we have only enough room for three records in our computer memory but that we have plenty of tapes available.

The first step is to read in the file three records at a time, sort them to make three-record blocks, and output the sorted blocks. Thus, first we read in A S 0 and output the block A 0 S, next we read in R T I and output the block I R T, and so forth. Now, in order for these blocks to be merged together, they must be on different tapes. If we want to do a three-way merge, then we would use three tapes, ending up after the sorting pass with the configuration shown in figure:

Now we're ready to merge the sorted blocks of size three. We read the first record off each input tape (there's just enough room in the memory) and output the one with the smallest key. Then the next record from the same tape as the record just output is read in and, again, the record in memory with the smallest key is output. When the end of a three-word block in the input is encountered, that tape is ignored until the blocks from the other two tapes have been processed and nine records have been output. Then the process is repeated to merge the second three-word block on each tape into a nine-word block (which is output on a different tape, to get ready for the next merge). By continuing in this way, we get three long blocks configured as shown in figure next:

Now one more three-way merge completes the sort. If we had a much longer file with many blocks of size 9 on each tape, then we would finish the second pass with blocks of size 27 on tapes 1, 2, and 3, then a third pass would produce blocks of size 81 on tapes 4, 5, and 6, and so forth. We need six tapes to sort an arbitrarily large file: three for the input and three for the output of each three-way merge. (Actually, we could get by with just four tapes: the output could be put on just one tape, and then the blocks from that tape distributed to the three input tapes in between merging passes.)

Page 62: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway merging• Now one more three-way merge completes the sort.

• If we had a much longer file with many blocks of size 9 on each tape, then we would finish:

• the second pass with blocks of size 27 on tapes 1, 2, and 3,

• then a third pass would produce blocks of size 81 on tapes 4, 5, and 6,

• and so forth.

•We need six tapes to sort an arbitrarily large file:

• three for the input and three for the output of each three-way merge.

Page 63: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Balanced multiway merging• Actually, we could get by with just four tapes:

• the output could be put on just one tape, and then the blocks from that tape distributed to the three input tapes in between merging passes.

• This method is called the balanced multiway merge:

• it is a reasonable algorithm for external sorting and a good starting point for the implementation of an external sort.

Page 64: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Replacement selection• If the size of memory available for the array is M records,

then the input file can be broken into initial runs of length M.

• A better approach is to use an algorithm called replacement selection that, on average, creates runs of 2M records in length.

• Replacement selection is actually a slight variation on the Heapsort algorithm.

• The fact that Heapsort is slower than Quicksort is irrelevant in this context because I/O time will dominate the total running time of any reasonable external sorting algorithm.

• Building longer initial runs will reduce the total I/O time required.

Page 65: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Replacement selection• Replacement selection views RAM as consisting of an array of

size M in addition to an input buffer and an output buffer.

• Additional I/O buffers might be desirable if the operating system supports double buffering, because replacement selection does sequential processing on both its input and its output.

• Imagine that the input and output files are streams of records.

• Replacement selection takes the next record in sequential order from the input stream when needed, and outputs runs one record at a time to the output stream.

Page 66: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Replacement selection• Buffering is used so that disk I/O is performed one block at a

time.

• A block of records is initially read and held in the input buffer.

• Replacement selection removes records from the input buffer one at a time until the buffer is empty.

• It turns out that the details of the implementation can be developed in an elegant and efficient way using priority queues.

• First, we'll see that priority queues provide a natural way to implement a multiway merge.

Page 67: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Replacement selection•More important, it turns out that we can use priority queues

for the initial sorting pass in such a way that they can produce sorted blocks much longer than could fit into internal memory.

• The basic operation needed to do P-way merging is repeatedly to output the smallest of the smallest elements not yet output from each of the P blocks to be merged.

• That smallest element should be replaced with the next element from the block from which it came.

• The replace operation on a priority queue of size P is exactly what is needed.

Page 68: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Replacement selection• The process of merging A 0 S with I R T and A G N (the first

merge from our example above), using a heap of size three in the merging process is shown:

This method is called the balanced multiway erge: it is a reasonable algorithm for external sorting and a good starting point for the implementation of an external sort. The more sophisticated algorithms below can make the sort run a little faster, but not much. (However, when execution times are measured in hours, as is not uncommon in external sorting, even a small percentage decrease in running time can be quite significant.) Suppose that we have N words to be manipulated by the sort and an internal memory of size M. Then the "sort" pass produces about N/M sorted blocks. (This estimate assumes one-word records: for larger records, the number of sorted blocks is computed by multiplying further by the record size.) If we do P-way merges on each subsequent pass, then the number of subsequent passes is about logp (N/M), since each pass reduces the number of sorted blocks by a factor of P. For example, the formula above says that using a four-way merge to sort a 200 million-word file on a computer with a million words of memory should take a total of about five passes. A very rough estimate of the running time can be found by multiplying by five the running time for the reverse file copy implementation suggested above.

Replacement Selection

It turns out that the details of the implementation can be developed in an elegant and efficient way using priority queues. First, we'll see that priority queues provide a natural way to implement a multiway merge. More important, it turns out that we can use priority queues for the initial sorting pass in such a way that they can produce sorted blocks much longer than could fit into internal memory. The basic operation needed to do P-way merging is repeatedly to output the smallest of the smallest elements not yet output from each of the P blocks to be merged. That smallest element should be replaced with the next element from the block from which it came. The replace operation on a priority queue of size P is exactly what is needed. Specifically, to do a P-way merge we begin by filling up a priority queue of size P with the smallest element from each of the P inputs. Then we output the smallest element and replace it in the priority queue with the next element from its block. The process of merging A 0 S with I R T and A G N (the first merge from our example above), using a heap of size three in the merging process is shown in figure:

The "keys" in these heaps are the smallest (first) key in each node. For clarity, we show entire blocks in the nodes of the heap; of course, an actual implementation would be an indirect heap of pointers into the blocks. First, the A is output so that the O (the next key in its block) becomes the "key" of the root. This violates the heap condition, so that node is exchanged with the node containing A, G, and N. Then that A is output and replaced with the next key in its block, the G. This does not violate the heap condition, so no further change is necessary.

Page 69: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Replacement selection• In summary, the replacement selection technique can be

used for both the "sort" and the "merge" steps of a balanced multiway merge.

• Property: A file of N records can be sorted using an internal memory capable of holding M records and (P + 1) tapes in about 1 + logP (N/2M) passes.

•We first use replacement selection with a priority queue of size M to produce initial runs of size about 2M (in a random situation) or more (if the file is partially ordered), then use replacement selection with a priority queue of size P for about logP (N/2M) (or fewer) merge passes.

Page 70: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Polyphase merging• One problem with balanced multiway merging for tape sorting is

that it requires either an excessive number of tape units or excessive copying.

• For P-way merging either we must use 2P tapes (P for input and P for output) or we must copy almost all of the file from a single output tape to P input tapes between merging passes, which effectively doubles the number of passes to be about 2 logP(N/2M).

• Several clever tape-sorting algorithms have been invented which eliminate virtually all of this copying by changing the way in which the small sorted blocks are merged together.

Page 71: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Polyphase merging• The most prominent of these methods is called polyphase

merging.

• The basic idea behind polyphase merging is

• to distribute the sorted blocks produced by replacement selection somewhat unevenly among the available tape units (leaving one empty) and

• then to apply a "merge-until-empty" strategy, at which point one of the output tapes and the input tape switch roles.

Page 72: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Polyphase merging• For example, suppose that we have just three tapes, and we

start out with the initial configuration of sorted blocks on the tapes shown at the top of figure:

(This comes from applying replacement selection to our example file with an internal memory that can only hold two records.) Tape 3 is initially empty, the output tape for the first merges. Now, after three two-way merges from tapes I and 2 to tape 3, the second tape becomes empty. Then, after two two-way merges from tapes I and 3 to tape 2, the first tape becomes empty. The sort is completed in two more steps. First, a two-way merge from tapes 2 and 3 to tape I leaves one file on tape 2, one file on tape 1. Then a two-way merge from tapes I and 2 to tape 3 leaves the entire sorted file on tape 3. This merge-until-empty strategy can be extended to work for an arbitrary number of tapes. Table below shows how six tapes might be used to sort 497 initial runs:

Tape 1 61 0 31 15 7 3 1 0 1

Tape 2 0 61 30 14 6 2 0 1 0

Tape 3 120 59 28 12 4 0 2 1 0

Tape 4 116 55 24 8 0 4 2 1 0

Tape 5 108 47 16 0 8 4 2 1 0

Tape 6 92 31 0 16 8 4 2 1 0 If we start out as indicated in the first column of table, with Tape 2 being the output tape, Tape I having 61 initial runs, Tape 3 having 120 initial runs, etc. as indicated in the first column of table, then after running a five-way "merge until empty," we have Tape I empty, Tape 2 with 61 (long) runs, Tape 3 with 59 runs, etc., as shown in the second column of table. At this point, we can rewind Tape 2 and make it an input tape, and rewind Tape I and make it the output tape. Continuing in this way, we eventually get the whole sorted file onto Tape 1. The merge is broken up into many phases which don't involve all the data, but no direct copying is involved.

The main difficulty in implementing a polyphase merge is to determine how to distribute the initial runs. It is not difficult to see how to build the table above by working backwards:

take the largest number in each column, make it zero, and add it to each of the other numbers to get the previous column.

This corresponds to defining the highest-order merge for the previous column which could give the present column. This technique works for any number of tapes (at least three):

Page 73: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Polyphase merging• This merge-until-empty strategy can be extended to work for

an arbitrary number of tapes. • Table below shows how six tapes might be used to sort 497

initial runs:

(This comes from applying replacement selection to our example file with an internal memory that can only hold two records.) Tape 3 is initially empty, the output tape for the first merges. Now, after three two-way merges from tapes I and 2 to tape 3, the second tape becomes empty. Then, after two two-way merges from tapes I and 3 to tape 2, the first tape becomes empty. The sort is completed in two more steps. First, a two-way merge from tapes 2 and 3 to tape I leaves one file on tape 2, one file on tape 1. Then a two-way merge from tapes I and 2 to tape 3 leaves the entire sorted file on tape 3. This merge-until-empty strategy can be extended to work for an arbitrary number of tapes. Table below shows how six tapes might be used to sort 497 initial runs:

Tape 1 61 0 31 15 7 3 1 0 1

Tape 2 0 61 30 14 6 2 0 1 0

Tape 3 120 59 28 12 4 0 2 1 0

Tape 4 116 55 24 8 0 4 2 1 0

Tape 5 108 47 16 0 8 4 2 1 0

Tape 6 92 31 0 16 8 4 2 1 0 If we start out as indicated in the first column of table, with Tape 2 being the output tape, Tape I having 61 initial runs, Tape 3 having 120 initial runs, etc. as indicated in the first column of table, then after running a five-way "merge until empty," we have Tape I empty, Tape 2 with 61 (long) runs, Tape 3 with 59 runs, etc., as shown in the second column of table. At this point, we can rewind Tape 2 and make it an input tape, and rewind Tape I and make it the output tape. Continuing in this way, we eventually get the whole sorted file onto Tape 1. The merge is broken up into many phases which don't involve all the data, but no direct copying is involved.

The main difficulty in implementing a polyphase merge is to determine how to distribute the initial runs. It is not difficult to see how to build the table above by working backwards:

take the largest number in each column, make it zero, and add it to each of the other numbers to get the previous column.

This corresponds to defining the highest-order merge for the previous column which could give the present column. This technique works for any number of tapes (at least three):

Page 74: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Polyphase merging• The main difficulty in implementing a polyphase merge is to

determine how to distribute the initial runs. • It is not difficult to see how to build the table above by

working backwards: • take the largest number in each column, make it zero, and

add it to each of the other numbers to get the previous column.

• This corresponds to defining the highest-order merge for the previous column which could give the present column. • This technique works for any number of tapes (at least three):

• the numbers which arise are "generalized Fibonacci numbers" which have many interesting properties.

Page 75: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Summary

• External sorting is important

• External merge sort minimizes disk I/O cost:

• Pass 0: Produces sorted runs of size B (# buffer pages).

• Later passes: merge runs.

• # of runs merged at a time depends on B, and block size.

• Larger block size means less I/O cost per page.

• Larger block size means smaller # runs merged.

• In practice, # of runs rarely more than 2 or 3.

Page 76: Data Structures and Algorithmsalgis/dsax/Data-structures-1.pdf · 2020-02-04 · Selection Sort -O(n2) algorithms •This process continues moving unsorted array boundary by one element

Summary

• Choice of internal sort algorithm may matter:

• Quicksort: Quick!

• Heap/tournament sort: slower (2x), longer runs

• The best sorts are wildly fast:

• Despite 40+ years of research, still improving!

• Clustered B+ tree is good for sorting;

• unclustered tree is usually very bad.