Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements...

32
Sorting Chapter 11

Transcript of Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements...

Page 1: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

Sorting

Chapter 11

Page 2: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

2

Sorting

• Consider listx1, x2, x3, … xn

• We seek to arrange the elements of the list in order– Ascending or descending

• Some O(n2) schemes– easy to understand and implement– inefficient for large data sets

Page 3: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

3

Categories of Sorting Algorithms

• Selection sort– Make passes through a list– On each pass reposition correctly some element

• Exchange sort– Systematically interchange pairs of elements which

are out of order– Bubble sort does this

• Insertion sort– Repeatedly insert a new element into an already

sorted list– Note this works well with a linked list implementation

All these have computing time O(n2)

All these have computing time O(n2)

Page 4: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

4

Improved Schemes

• We seek improved computing times for sorts of large data sets

• Chapter presents schemes which can be proven to have worst case computing time

O( n log2n )

• Heapsort

• Quicksort

Page 5: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

5

Heaps

A heap is a binary tree with properties:

1. It is complete• Each level of tree completely filled• Except possibly bottom level (nodes in left

most positions)

2. It satisfies heap-order property• Data in each node >= data in children

Page 6: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

6

Heaps

• Example

Not a heap – Why? Not a heap – Why? HeapHeap

22

12

14

24

28

22

12 14

28

24

Page 7: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

7

Implementing a Heap

• Use an array or vector

• Number the nodes from top to bottom– Number nodes on each row from left to right

• Store data in ith node in ith location of array (vector)

Page 8: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

8

Implementing a Heap

• If heap is the name of the array or vector used, the items in previous heap is stored as follows:heap[0]=78; heap[1]=56; heap[2]=32;heap;[3]=45; heap;[4]=8; heap[5]=23;heap[6]=19;

Page 9: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

9

Implementing a Heap

• In an array implementation children of ith node are at heap[2*i+1] and

heap[2*(i+1)]• Parent of the ith node is at

heap[(i-1)/2]

Page 10: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

10

Converting a Complete Binary Tree to a Heap

• Percolate down the largest value

For c = 2 * r to n do: // c is location of left child // Find the largest child a. If c < n and heap[c] < heap[c + 1]

Increment c by 1. /* Swap node & largest child if needed, move down to the next subtree */

b. If heap[r] < heap[c]: i. Swap heap[r] and heap[c]. ii. Set r = c. iii. Set c = 2 * c.

Else Terminate repetition.

Page 11: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

11

Convert Complete Binary Tree to a Heaptemplate <typename ElementType> void PercolateDown(ElementType x[], int n, int r) { int c = 2*r; bool done = false; while (c < n && !done) { if (c < n && x[c] < x[c+1] ) c++; if (x[r] < x[c]) { ElementType temp = x[r]; x[r] = x[c]; x[c] = temp; r = c; c = 2*c; } else done = true; } return; }

Page 12: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

12

Convert Complete Binary Tree to a Heap

template <typename ElementType> void Heapify(ElementType x[], int n) { for (int r = n/2; r > 0; r--) PercolateDown(x, n, r); return; } After application of Heapify(), x is a heap.

Page 13: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

13

Heapsort

Consider array x as a complete binary tree and use the Heapify algorithm to convert this tree

to a heap.1. For i = n down to 2:Interchange x[1] and x[i], thus putting the largest element in the sublist

x[1],...,x[i] at end of sublist. 2. Apply the PercolateDown algorithm to convert

the binary tree corresponding to the sublist stored in positions 1 through i - 1 of x.

Page 14: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

14

Heapsort

• In PercolateDown, the number of items in the subtree considered at each stage is one-half the number of items in the subtree at the preceding stage. Thus, the worst-case computing time is O(log 2 n).

• Heapify algorithm executes PercolateDown n/2 times: worst-case computing time is O(nlog2 n).

• Heapsort executes Heapify one time and PercolateDown n - 1 times; consequently, its worst-case computing time is O(n log2 n).

Page 15: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

15

Heapsort

Page 16: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

16

Heapsort

• Note the way thelarge values arepercolated down

Page 17: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

17

Quicksort

• A more efficient exchange sorting scheme than bubble sort – A typical exchange involves elements that are far

apart – Fewer interchanges are required to correctly position

an element.• Quicksort uses a divide-and-conquer strategy

– A recursive approach – The original problem partitioned into simpler sub-

problems,– Each sub problem considered independently.

• Subdivision continues until sub problems obtained are simple enough to be solved directly

Page 18: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

18

Quicksort

• Choose some element called a pivot • Perform a sequence of exchanges so that

– All elements that are less than this pivot are to its left and

– All elements that are greater than the pivot are to its right.

• Divides the (sub)list into two smaller sub lists, • Each of which may then be sorted independently

in the same way.

Page 19: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

19

Quicksort

If the list has 0 or 1 elements, return. // the list is sorted

Else do:Pick an element in the list to use as the pivot.

  Split the remaining elements into two disjoint groups:SmallerThanPivot = {all elements < pivot}LargerThanPivot = {all elements > pivot}

 

 Return the list rearranged as: Quicksort(SmallerThanPivot),

pivot, Quicksort(LargerThanPivot).

Page 20: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

20

Quicksort Example

• Given to sort:75, 70, 65, , 98, 78, 100, 93, 55, 61, 81,

• Select, arbitrarily, the first element, 75, as pivot.• Search from right for elements <= 75, stop at

first element <75• Search from left for elements > 75, stop at first

element >=75• Swap these two elements, and then repeat this

process

84 68

Page 21: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

21

Quicksort Example

75, 70, 65, 68, 61, 55, 100, 93, 78, 98, 81, 84

• When done, swap with pivot• This SPLIT operation placed pivot 75 so that all

elements to the left were <= 75 and all elements to the right were >75.– See code page 602

• 75 is now placed appropriately • Need to sort sublists on either side of 75

Page 22: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

22

Quicksort Example

• Need to sort (independently):

55, 70, 65, 68, 61 and

100, 93, 78, 98, 81, 84

• Let pivot be 55, look from each end for values larger/smaller than 55, swap

• Same for 2nd list, pivot is 100

• Sort the resulting sublists in the same manner until sublist is trivial (size 0 or 1)

Page 23: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

23

Quicksort

• Note thepartitionsand pivotpoints

• Note codepgs 602-603 of text

Page 24: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

24

Quicksort Performance

• is the average case computing time– If the pivot results in sublists of approximately

the same size.

• O(n2) worst-case – List already ordered, elements in reverse – When Split() repetitively results, for example,

in one empty sublist

2( log )O n n

Page 25: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

25

Improvements to Quicksort

• Quicksort is a recursive function– stack of activation records must be

maintained by system to manage recursion.– The deeper the recursion is, the larger this

stack will become.

• The depth of the recursion and the corresponding overhead can be reduced – sort the smaller sublist at each stage first

Page 26: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

26

Improvements to Quicksort

• Another improvement aimed at reducing the overhead of recursion is to use an iterative version of Quicksort()

• To do so, use a stack to store the first and last positions of the sublists sorted "recursively".

Page 27: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

27

Improvements to Quicksort

• An arbitrary pivot gives a poor partition for nearly sorted lists (or lists in reverse)

• Virtually all the elements go into either SmallerThanPivot or LargerThanPivot– all through the recursive calls.

• Quicksort takes quadratic time to do essentially nothing at all.

Page 28: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

28

Improvements to Quicksort

• Better method for selecting the pivot is the median-of-three rule, – Select the median of the first, middle, and last

elements in each sublist as the pivot.

• Often the list to be sorted is already partially ordered

• Median-of-three rule will select a pivot closer to the middle of the sublist than will the “first-element” rule.

Page 29: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

29

Improvements to Quicksort

• For small files (n <= 20), quicksort is worse than insertion sort; – small files occur often because of recursion.

• Use an efficient sort (e.g., insertion sort) for small files.

• Better yet, use Quicksort() until sublists are of a small size and then apply an efficient sort like insertion sort.

Page 30: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

30

Mergesort

• Sorting schemes are either … – internal -- designed for data items stored in main

memory – external -- designed for data items stored in

secondary memory.

• Previous sorting schemes were all internal sorting algorithms:– required direct access to list elements

• not possible for sequential files

– made many passes through the list • not practical for files

Page 31: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

31

Mergesort

• Mergesort can be used both as an internal and an external sort.

• Basic operation in mergesort is merging, – combining two lists that have previously been

sorted – resulting list is also sorted.

• Example was the file merge program done as last assignment in CS2

Page 32: Sorting Chapter 11. 2 Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.

32

Merge Flow Chart

Open files, read 1st records

Trans key > OM key

Write OM record to NM file, Trans key yet to be matched

Trans key = = OM key

Compare keys

Trans Key < OM key

Type of Trans Type of

Trans

Add OK Other, Error

Other, Error

Modify, Make changes

Del, go on