+ All Categories
Home > Documents > 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the...

1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the...

Date post: 18-Dec-2015
Category:
Upload: hollie-bathsheba-conley
View: 214 times
Download: 0 times
Share this document with a friend
98
1 Sorting Gordon College
Transcript
Page 1: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

1

Sorting

Gordon College

Page 2: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

2

Sorting• Consider a list

x1, 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: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

3

Categories of Sorting Algorithms

1. Selection sort– Make passes through a list– On each pass reposition correctly

some element

Look for smallest in list and replace 1st element, now start the process over with the remainder of the list

Page 4: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

4

Selection

Recursive Algorithm

If the list has only 1 element ANCHOR

stop – list is sorted

Else do the following:

a. Find the smallest element and place in front

b. Sort the rest of the list

Page 5: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

5

Categories of Sorting Algorithms

2. Exchange sort– Systematically interchange pairs of elements

which are out of order– Bubble sort does this

Out of order, exchange In order, do not exchange

Page 6: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

6

Bubble Sort Algorithm

1. Initialize numCompares to n - 12. While numCompares!= 0, do following

a. Set last = 1 // location of last element in a swap

b. For i = 1 to numPairsif xi > xi + 1

Swap xi and xi + 1 and set last = i

c. Set numCompares = last – 1End while

Page 7: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

7

Bubble Sort Algorithm

1. Initialize numCompares to n - 1

2. While numCompares!= 0, do following

a. Set last = 1 // location of last element in a swap

b. For i = 1 to numPairsif xi > xi + 1

Swap xi and xi + 1 and set last = i

c. Set numCompares = last – 1

End while

45 67 12 34 25 3945 12 67 34 25 3945 12 34 67 25 3945 12 34 25 67 3945 12 34 25 39 67

Allows it to quit ifIn orderTry: 23 12 34 45 67

Also allows us toLabel the highest assorted

45 12 34 25 39 6712 45 34 25 39 6712 34 45 25 39 6712 34 25 45 39 6712 34 25 39 45 67…

Page 8: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

8

Categories of Sorting Algorithms

3. Insertion sort– Repeatedly insert a new element into an already

sorted list

– Note this works well with a linked list implementation

Page 9: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

9

Example of Insertion Sort

• Given list to be sorted 67, 33, 21, 84, 49, 50, 75

– Note sequence of steps carried out

Page 10: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

10

Improved Schemes• These 3 sorts - have computing time O(n2)• We seek improved computing times for sorts of large data

sets

• There are sorting schemes which can be proven to have average computing time

O( n log2n )

• No universally good sorting scheme– Results may depend on the order of the list

Page 11: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

11

Comparisons of Sorts

• Sort of a randomly generated list of 500 items– Note: times are on 1970s hardware

Algorithm Type of Sort Time (sec)

•Simple selection•Heapsort•Bubble sort•2 way bubble sort•Quicksort•Linear insertion•Binary insertion•Shell sort

Selection

Selection

Exchange

Exchange

Exchange

Insertion

Insertion

Insertion

69

18

165

141

6

66

37

11

Page 12: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

12

Indirect Sorts

• What happens if items being sorted are large structures (like objects)?– Data transfer/swapping time unacceptable

• Alternative is indirect sort– Uses index table to store positions of the objects

– Manipulate the index table for ordering

Page 13: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

13

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

A

ED

CB

GF

JIH

Complete Tree (Depth 3)

Page 14: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

14

Heaps

Which of the following are heaps?

A B C

Page 15: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

15

Maximum and Minimum Heaps Example

10

40

3015

40

10

3015

5

25

55522011

5010

22

55

2220

5111025

5250

(A) Maximum Heap (9 nodes) (B) Maximum Heap (4 nodes)

(C) Minimum Heap (9 nodes) (D) Minimum Heap (4 nodes)

Page 16: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

16

Implementing a Heap

• Use an array or vector

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

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

Page 17: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

17

Implementing a Heap

• Note the placement of the nodes in the array

41

Page 18: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

18

Implementing a Heap

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

myArray[2*i+1]• Parent of the ith node is at

mayArray[i/2]

Page 19: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

19

Basic Heap Operations

• Constructor– Set mySize to 0, allocate array (if dynamic array)

• Empty– Check value of mySize

• Retrieve max item– Return root of the binary tree, myArray[1]

Page 20: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

20

Basic Heap Operations

• Delete max item (popHeap)– Max item is the root, replace with last node in

tree

– Then interchange root with larger of two children– Continue this with the resulting sub-tree(s) –

result is a new heap.

Result called a semiheap

Result called a semiheap

Page 21: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

21

Exchanging elements when performing a popHeap()

63

1835

3882510

4030

v[0]

v[1] v[2]

v[9]

v[4]

v[8]v[7]

v[5]v[3] v[6]

Before a deletion After exchanging the rootand last element in the heap

63

18

35

3882510

4030

v[0]

v[1] v[2]

v[9]

v[4]

v[8]v[7]

v[5]v[3] v[6]

Page 22: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

22

Adjusting the heap for popHeap()

Step 1: Exchange 18 and 40

18

388

40

v[0]

v[2]

v[5] v[6]

. . .

Step 2: Exchange 18 and 38

18

38

8

40

v[0]

v[2]

v[5] v[6]

. . .

Page 23: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

23

Percolate Down Algorithmconverts semiheap to heap

1. Set c = 2 * r //location of left child

2. While r <= n do following // must be child(s) for root

a. If c < n and myArray[c] < myArray[c + 1]Increment c by 1 //find larger child

b. If myArray[r] < myArray[c]i. Swap myArray[r] and myArray[c]ii. set r = ciii. set c = 2 * r

else Terminate repetitionEnd while

r = current root node

n = number of nodes

Page 24: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

24

Basic Heap Operations

• Insert an item (pushHeap)– Amounts to a percolate up routine– Place new item at end of array

– Interchange with parent so long as it is greater than its parent

Page 25: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

25

Example of Heap Before and After Insertion of 50

63

1835

3882510

4030

v[0]

v[1] v[2]

v[9]

v[4]

v[8]v[7]

v[5]v[3] v[6]

63

1835

3882510

4030

v[0]

v[1] v[2]

v[9]

v[4]

v[8]v[7]

v[5]v[3] v[6]

(a) (b)

50

v[10]

Page 26: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

26

Reordering the tree for the insertion

63

18 25

30

v[0]

v[1]

v[9]

v[4]

50

v[10]

. . .

. . .

Step 1 Compare 50 and 25(Exchange v[10] and v[4])

63

18 25

30

v[0]

v[1]

v[9]

v[4]

50

v[10]

. . .

. . .63

18 25

30

v[0]

v[1]

v[9]

v[4]

50

v[10]

. . .

. . .

Step 2 Compare 50 and 30(Exchange v[4] and v[1])

Step 3 Compare 50 and 63(50 in correct location)

Page 27: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

27

Heapsort

• Given a list of numbers in an array– Stored in a complete binary tree

• Convert to a heap (heapify)– Begin at last node not a leaf– Apply “percolated down” to this subtree– Continue

Page 28: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

28

Example of Heapifying a Vector

9

19465

60205030

1712

Initial Vector

Page 29: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

29

Example of Heapifying a Vector

4

9

1965

60205030

1712

adjustHeap() at 4 causes no changes(A)

Page 30: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

30

Example of Heapifying a Vector

4

9

1930

60205065

1712

adjustHeap() at 3 moves 30 down(B)

Page 31: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

31

Example of Heapifying a Vector

50

4

9

1930

172065

6012

adjustHeap() at 2 moves 17 down(C)

Page 32: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

32

Example of Heapifying a Vector

50

4

9

1912

172030

6065

adjustHeap() at 1 moves 12 down two levels(D)

50

4

9

1930

172065

6012

adjustHeap() at 2 moves 17 down(C)

Page 33: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

33

Example of Heapifying a Vector

19

4

65

912

172030

6050

adjustHeap() at 0 moves 9 down three levels(E)

50

4

9

1912

172030

6065

adjustHeap() at 1 moves 12 down two levels(D)

Page 34: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

34

Heapsort

• Algorithm for converting a complete binary tree to a heap – called "heapify"For r = n/2 down to 1:

Apply percolate_down to the subtreein myArray[r] , … myArray[n]

End for

• Puts largest element at root

n = index for last node in treetherefore n/2 is parent

Page 35: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

35

Heapsort• Now swap element 1 (root of tree) with last

element

– This puts largest element in correct location

• Use percolate down on remaining sublist– Converts from semi-heap to heap

Page 36: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

36

Heapsort

• Again swap root with rightmost leaf

• Continue this process with shrinking sublist

60

60

Page 37: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

37

Heapsort Algorithm

1. Consider x as a complete binary tree, use heapify to convert this tree to a heap

2. for i = n down to 2:a. Interchange x[1] and x[i] (puts largest element at end)b. Apply percolate_down to convert binary tree corresponding to sublist in x[1] .. x[i-1]

Page 38: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

38

Example of Implementing heap sort

75

2520

5035

Heapified Tree

int arr[] = {50, 20, 75, 35, 25};vector<int> v(arr, 5);

Page 39: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

39

Example of Implementing heap sort

50

7520

2535

35

7550

2520

Calling popHeap() with last = 5deletes 75 and stores it in h[4]

Calling popHeap() with last = 4deletes 50 and stores it in h[3]

Page 40: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

40

Example of Implementing heap sort

25

7550

3520

20

7550

3525

Calling popHeap() with last = 3deletes 35 and stores it in h[2]

Calling popHeap() with last = 2deletes 25 and stores it in h[1]

Page 41: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

41

Heap Algorithms in STL

• Found in the <algorithm> library–make_heap() heapify

–push_heap() insert

–pop_heap() delete

–sort_heap() heapsort

Page 42: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

42

Priority Queue

• A collection of data elements– Items stored in order by priority– Higher priority items removed ahead of lower

Implementation ?

Page 43: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

43

Implementation possibilities

list (array, vector, linked list)

insert – O(1)

remove max - O(n)

ordered list

insert - linear insertion sort O(n)

remove max - O(1)– Heap (Best)

Basic operations have O(log2n) time

Page 44: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

44

Priority Queue

Basic Operations– Constructor– Insert– Find, remove smallest/largest (priority) element– Replace – Change priority– Delete an item– Join two priority queues into a larger one

Page 45: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

45

Priority Queue• STL priority queue adapter uses heap

priority_queue<BigNumber, vector<BigNumber> > v;cout << "BIG NUMBER DEMONSTRATION" << endl;for(int k=0;k<6;k++){

cout << "Enter BigNumber: "; cin >> a;v.push(a);

}cout<<"POP IN ORDER"<<endl;while(!v.empty()){

cout<<v.top()<<endl;v.pop();

}

Page 46: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

46

Quicksort• More efficient exchange sorting scheme

– (bubble sort is an exchange sort)

• Typical exchange: involves elements that are far apart

Fewer interchanges are required to correctly position an element.

• Quicksort uses a divide-and-conquer strategyA 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 47: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

47

Quicksort

Basic Algorithm• Choose an element - pivot • Perform sequence of exchanges so that

<elements less than P> <P> <elements greater than P>– 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 48: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

48

Quicksortrecursive

If the list has 0 or 1 elements, ANCHORreturn. // 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 49: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

49

Quicksort Example

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

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

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

match• Swap these two elements, and then repeat this

process. When can you stop?

84 68

Page 50: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

50

Quicksort Example

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

• When done, swap with pivot• This SPLIT operation places pivot 75 so

that all elements to the left are <= 75 and all elements to the right are >75.

• 75 is in place. • Need to sort sublists on either side of 75

Page 51: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

51

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 52: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

52

QuickSort Recursive Function

template <typename ElementType> void quicksort (ElementType x[], int first int last) {

int pos; // pivot's final position if (first < last) // list size is > 1 { split(x, first, last, pos); // Split into 2 sublists

quicksort(x, first, pos - 1); // Sort left sublist quicksort(x,pos + 1, last); // Sort right sublist

}

} 23 45 12 67 32 56 90 2 15

Page 53: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

53

template <typename ElementType>void split (ElementType x[], int first, int last, int & pos){ ElementType pivot = x[first]; // pivot element int left = first, // index for left search right = last; // index for right search while (left < right) { while (pivot < x[right]) // Search from right for right--; // element <= pivot // Search from left for while (left < right && // element > pivot x[left] <= pivot) left++;

if (left < right) // If searches haven't met swap (x[left], x[right]); // interchange elements } // End of searches; place pivot in correct position pos = right; x[first] = x[pos]; x[pos] = pivot;} 

23 45 12 67 32 56 90 2 15

Page 54: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

54

Quicksort

• Visual example ofa quicksort on an array

etc. …

Page 55: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

55

QuickSort Example

v = {800, 150, 300, 650, 550, 500, 400, 350, 450, 900}

150 300 650 550 800 400 350 450

scanUp scanDown

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

pivot

500 900

Pivot selected at random

Page 56: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

56

QuickSort ExampleBefore the exchange

After the exchange and updates to scanUp and scanDown

150 300 650 550 800 400 350 450

scanUp scanDown

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

pivot

500 900

150 300 450 550 800 400 350 650

scanUp scanDown

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

pivot

500 900

Page 57: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

57

QuickSort Example

Before the exchange

After the exchange and updates to scanUp and scanDown

150 300 450 550 800 400 350 650

scanUp scanDown

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

pivot

500 900

150 300 450 350 800 400 550 650

scanUp scanDown

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

pivot

500 900

Page 58: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

58

QuickSort ExampleBefore the exchange

After the exchange and updates to scanUp and scanDown

150 300 450 350 800 400 550 650

scanUp scanDown

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

pivot

500 900

150 300 450 350 400 800 550 650

scanUpscanDown

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

pivot

500 900

Page 59: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

59

QuickSort Example

400 150 300 450 350 500 800 550 650

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

900

Pivot in its final position

400 150 300 450 350 500 800 550 650

v[0] v[9]v[8]v[7]v[6]v[5]v[4]v[3]v[2]v[1]

v[0] - v[4] v[6] - v[9]

900

quicksort(x, 0, 4); quicksort(x, 6, 9);

Page 60: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

60

QuickSort Example

150 300 400 450 350

v[0] v[4]v[3]v[2]v[1]

pivot

300 150 400 450 350

scanUp

v[0] v[4]v[3]v[2]v[1]

Initial Values

scanDown

pivot

300 150 400 450 350

v[0] v[4]v[3]v[2]v[1]

scanUp

After Scans Stop

scanDown

quicksort(x, 0, 0); quicksort(x, 2, 4);

Page 61: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

61

QuickSort Example

550 650 800 900

v[6] v[9]v[8]v[7]

pivot

650 550 800 900

scanUp

v[6] v[9]v[8]v[7]

Initial Values

scanDown

pivot

650 550 800 900

v[6] v[9]v[8]v[7]

scanUp

After Stops

scanDown

quicksort(x, 6, 6); quicksort(x, 8, 9);

Page 62: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

62

QuickSort Example

400 450 350

v[4]v[3]v[2]

Before Partitioning

350 400 450

v[4]v[3]v[2]

After Partitioning

150 300 350 400 450 500

v[0] v[4]v[3]v[2]v[1]

550 650 800 900

v[6] v[9]v[8]v[7]v[5]

v[0] v[4]v[3]v[2]v[1] v[6] v[9]v[8]v[7]v[5]

150 900800650550500350450400300

Page 63: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

63

Quicksort Performance

• O(n log2n) is the average case computing time– If the pivot results in sublists of approximately

the same size.

• O(n2) worst-case – List already ordered or elements in reverse. – When Split() repeatedly creates a sublist

with one element. (when pivot is always smallest or largest value)

99 45 12 67 32 56 90 2 15 What 2 pivots would result in empty sublist?

12 34 45 56 78 88 90 100

Page 64: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

64

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.

12 34 45 56 78 88 90 100

Page 65: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

65

Improvements to Quicksort

• Better method for selecting the pivot is the median-of-three rule,

– Select the median (middle value) of the first, middle, and last elements in each sublist as the pivot.

(4 10 6) - median is 6

• 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 66: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

66

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. (major overhead)

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

Page 67: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

67

Improvements to Quicksort

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

Implementation: use a stack to store the first and last positions of the sublists sorted "recursively". In other words – create your own low-overhead execution stack.

Page 68: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

68

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 69: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

69

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 70: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

70

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.

Page 71: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

71

Merge Algorithm

1. Open File1 and File2 for input, File3 for output2. Read first element x from File1 and

first element y from File23. While neither eof File1 or eof File2

If x < y thena. Write x to File3b. Read a new x value from File1

Otherwisea. Write y to File3b. Read a new y from File2

End while4. If eof File1 encountered copy rest of of File2 into File3.

If eof File2 encountered, copy rest of File1 into File3

Page 72: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

72

Binary Merge Sort

• Given a single file

• Split into two files (alternatively into each file)

Page 73: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

73

Binary Merge Sort

• Merge first one-element "subfile" of F1 with first one-element subfile of F2– Gives a sorted two-element subfile of F

• Continue with rest of one-element subfiles

Page 74: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

74

Binary Merge Sort

• Split again

• Merge again as before

• Each time, the size of the sorted subgroups doubles

Page 75: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

75

Binary Merge Sort

• Last splitting gives two files each in order

• Last merging yields a single file, entirely in order

Note we always are limited to subfiles of

some power of 2

Note we always are limited to subfiles of

some power of 2

Page 76: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

76

Natural Merge Sort

• Allows sorted subfiles of other sizes– Number of phases can be reduced when file

contains longer "runs" of ordered elements

• Consider file to be sorted, note in order groups

Page 77: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

77

Natural Merge Sort

• Copy alternate groupings into two files– Use the sub-groupings, not a power of 2

• Look for possible larger groupings

Page 78: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

78

Natural Merge Sort

• Merge the corresponding sub files

EOF for F2, Copy remaining groups from F1

EOF for F2, Copy remaining groups from F1

Page 79: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

79

Natural Merge Sort

• Split again, alternating groups

• Merge again, now two subgroups

• One more split, one more merge gives sort

Page 80: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

80

Natural Merge Sort

Split algorithm for natural merge sort

1. Open F for input and F1 and F2 for output

2. While the end of F has not been reached:a. Copy a sorted subfile of F into F1 as follows: repeatedly

read an element of F and write it to F1 until the next element in F is smaller than this copied item or the end of F is reached.

b. If the end of F has not been reached, copy the next sorted subfile of F into F2 using the method above.

End while.

Page 81: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

81

Natural Merge SortMerge algorithm for natural merge sort1. Open F1 and F2 for input, F for output.2. Initialize numSubfiles to 03. While not eof F1 or not eof F2

a. While no end of subfile in F1 or F2 has been reached: If the next element in F1 is less than the next element in F2

Copy the next element from F1 into F.Else

Copy the next element from F2 into F.End While

b. If the eof F1 has been reached Copy the rest of subfile F2 to F.Else Copy the rest of subfile F1 to F.

c. Increment numSubfiles by 1. End While

4. Copy any remaining subfiles to F, incrementing numSubfiles by 1 for each.

Page 82: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

82

Natural Merge Sort

Mergesort algorithm

Repeat the following until numSubfiles is equal to 1:

1. Call the Split algorithm to split F into files F1 and F2.

2. Call the Merge algorithm to merge corresponding subfiles in F1 and F2 back into F.

Worst case for natural merge sort O(n log2n)

Page 83: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

83

Natural MergeSort Example

7 10 19 25 12 17 21 30 48

sublist Bsublist A

first lastmid

Page 84: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

84

Natural MergeSort Review

7 10 19 25 12 17 21 30 48

7

sublist Bsublist A

indexA indexB

tempVector

7 10

tempVector

7 10 19 25 12 17 21 30 48

sublist Bsublist A

indexA indexrB

Page 85: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

85

Natural MergeSort Review

7 10 19 25 12 17 21 30 48

7 10 12

sublist Bsublist A

indexA indexB

tempVector

7 10 19 25 12 17 21 30 48

7 10 12 17 19 21 25

sublist Bsublist A

indexA indexB

tempVectorSo forth and so on…

Page 86: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

86

Natural MergeSort Review

7 10 19 25 12 17 21 30 48

7 10 12 17 19 21 25 30 48

sublist Bsublist A

indexA indexB

tempVector

last

7 10 12 17 19 21 25 30 48

tempVector

7 10 12 17 19 21 25 30 48

first last

Page 87: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

87

Recursive Natural MergeSort (25 10 7 19 3 48 12 17 56 30 21)

(25 10 7 19 3 )

(7 19 3 )(25 10 )

[3 7 10 19 25]

(48 12 17 56 30 21 )

(56 30 21 )(48 12 17 )

[12 17 21 30 48 56]

[3 7 10 12 17 19 21 25 30 48 56]

(10 )

[10 25]

(25 ) (19 3 )(7 )

[3 7 19]

(56 )

[21 30 56]

(12 17 )(48 )

[12 17 48]

(21 )(3 )

[3 19]

(19 )

(30 21 )

[21 30]

(30 )(17 )

[12 17]

(12 )

Page 88: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

88

Recursive Natural MergeSort Call msort() - recusive call1 to msort() - recusive call2 to msort() - call1 merge()

msort() : n/2 msort(): n/2

msort(): n/4

msort(): n/8 msort(): n/8

msort(): n/4

msort(): n/8 msort(): n/8

msort(): n/4

msort(): n/8 msort(): n/8

msort(): n/4

msort(): n/8 msort(): n/8

Level 0:

Level 3:

Level 2:

Level 1:

Level i:

. . .

Page 89: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

89

Sorting Fact

• any algorithm which performs sorting using comparisons cannot have a worst-case performance better than O(n log n)– a sorting algorithm based on comparisons

cannot be O(n) - even for its average runtime.

Page 90: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

90

Radix Sort• Based on examining digits in some base-b

numeric representation of items

• Least significant digit radix sort– Processes digits from right to left

• Create groupings of items with same value in specified digit– Collect in order and create grouping with next significant

digit

Page 91: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

91

Radix SortOrder ten 2 digit numbers in 10 bins from smallest number to largest number. Requires 2 calls to the sort Algorithm.

Initial Sequence: 91 6 85 15 92 35 30 22 39Pass 0: Distribute the cards into bins according

to the 1's digit (100).

9130

0

39

987

6

6

351585

543

2292

21

Page 92: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

92

Radix SortFinal Sequence: 91 6 85 15 92 35 30 22 39Pass 1: Take the new sequence and distribute

the cards into bins determined by the 10's digit (101).

6

0

9291

9

85

87654

393530

3

22

2

15

1

Page 93: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

93

Sort Algorithm Analysis

• Selection Sort (uses a swap)– Worst and average case O(n^2)– can be used with linked-list (doesn’t require

random-access data)– Can be done in place– not at all fast for nearly sorted data

Page 94: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

94

Sort Algorithm Analysis

• Bubble Sort (uses an exchange)– Worst and average case O(n^2)– Since it is using localized exchanges - can be

used with linked-list– Can be done in place– O(n^2) - even if only one item is out of place

Page 95: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

95

Sort Algorithm Analysissorts actually used

• Insertion Sort (uses an insert)– Worst and average case O(n^2)– Does not require random-access data– Can be done in place– It is fast (linear time) for nearly sorted data– It is fast for small lists

Most good sorting methods call Insertion Sort for small lists

Page 96: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

96

Sort Algorithm Analysissorts actually used

• Merge Sort– Worst and average case O(n log n)– Does not require random-access data– For linked-list - can be done in place– For an array - need to use a buffer– It is not significantly faster on nearly sorted data

(but it is still log-linear time)

Page 97: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

97

Sort Algorithm Analysissorts actually used

• QuickSort– Worst O(n^2)– Average case O(n log n) [good time]– can be done in place– Additional space for recursion O(log n)– Can be slow O(n^2) for nearly sorted or reverse

data.– Sort used for STL sort()

Page 98: 1 Sorting Gordon College. 2 Sorting Consider a list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending.

98

End of sorting


Recommended