62
ch18 Searching and Sorting Algorithms 1

18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

ch18Searching and Sorting Algorithms

1

Page 2: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Algorithm Efficiency deals with

• Methods of solution rather than programs

• Efficient use of time and memory

• Mathematical methods independent of coding, computers or data

2

Page 3: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Don't be misled by

• How are the algorithms coded?

• What computer should you use?

• What data should programs use?

3

Page 4: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Execution Time• Depends on number of operations

• Consider two List implementations:

• Array-based: Each item requires one step to access

• Pointer-based: Last item requires n steps to access

• Conclusion:

• Array-based: Size of array does not affect time

• Pointer-based: The larger the list the more time to access (time is proportional to n)

4

Page 5: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Traversing a linked list

5

Page 6: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

6

// List consists of N nodesNode *cur = head;while (cur != NULL) { cout << cur->item << endl; cur = cur->next;}

1 assignment n+1 comparison n writes n assignments

total: (n+1)*(a + c) + n * (w) ∝ N

Page 7: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Towers of Hanoi

• N disks require 2n – 1 moves

• Each move requires m time units

• 2n – 1 moves require (2n -1) * m time units

• Exponential times grow very quickly. 🙁

7

Page 8: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

nesting is the bottleneck

8

Page 9: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

9

for (i = 1 through n) // n-times for (j = 1 through i) // i-times for (k = 1 through 5) // 5-times task T

T: (5) + (5 + 5) + (5 + 5 + 5) + ... : 1 + 2 + 3 + ... + N : N * (N + 1) / 2 ∝ N2

Page 10: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

BigO formalities

10

Page 11: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Time v N

• Establish a relationship between time and n

• Time proportional to some function of n, f(n)

• This function is the growth rate, indicated as O(f(n))

11

Page 12: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

12

Actual number of operations for each nk * f(n)

From this point on, the number of operations is less than k * f(n)

Input size n

Run time

n0

O(f(n))

Page 13: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

13

Actual number of operations for each n

f(n) = n2

Beyond this point, the operations for the algorithm are bounded above by n2 and by n3

Input size n

Run time

f(n) = n3

n0

∝ O(n3) ∝ O(n2)

Page 14: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

14

N

Function 10 100 1,000 10,000 100,000 1,000,0001 1 1 1 1 1 1log2n 3 6 9 13 16 19n 10 102 103 104 105 106

n * log2n 30 664 9,965 105 106 107

n2 102 104 106 108 1010 1012

n3 103 106 109 1012 1015 1018

2n 103 1030 10301 103,010 1030,103 10301,030

Page 15: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

don't be quick to judge

15

Page 16: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

16

Page 17: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

! 1 < logn < n < nlogn < n2 < n3 < 2n

17

Page 18: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

What you are going for

• Largest power of n in polynomial

• Ignore constants

• Worst order wins (eg n over logn)

18

Page 19: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

What is measured

19

Page 20: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

20

Worst Case v Avg Case

Page 21: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

21

total = 0;

for (int row = 0; row < n; row++) { RowTotal[row] = 0;

for (int col = 0; col < n; col++) { RowTotal[row] += mat[row][col]; total += mat[row][col]; } }

n

n

n * n = O(n2)

Nesting is usually the issue

Page 22: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

22

for (k = 1; k <= n / 2; k++) { ...

for (j = 1; j <= n * n; j++) { ... } }

n

n2

n * n2 = O(n3)

Nesting is usually the issue

Page 23: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

23

for (k = 1; k <= n / 2; k++) { ... }

for (j = 1; j <= n * n; j++) { ... }

n

n2

n + n2 = O(n2)

Sequential is usually better

Page 24: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

24

k = n;

while (k >= 1) { ... k = k / 2; }

logN

O(logN)

Page 25: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

25

for (k = 1; k <= n; k++) { j = n;

while (j > 0) { ... j = j / 2; } }

n

logN

n * logn = O(nlogN)

Page 26: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

26

k = 1; do { j = 1; do { ... j = 2 * j; } while (j <= n); k++; } while (k <= n);

n

logN

n * logn = O(nlogN)

Page 27: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Sequential search

• Look at each item in the data collection in turn

• Stop when the desired item is found, or the end of the data is reached

27

Page 28: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

28

Worst CaseO(n)

Best CaseO(1)

Avg CaseO(n)

Page 29: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Binary search

• To search a sorted array for a particular item

• Repeatedly divide the array in half

• Determine which half the item must be in, if it is indeed present, and discard the other half

29

Page 30: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

30

Worst CaseO(logN)

Best CaseO(1)

Avg CaseO(logN)

Page 31: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Sorting

• Organize values (ascending or descending) by key

• Key is unique for each record

31

Page 32: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

32

Internalreside in RAM

Externalreside on HD

Page 33: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Selection sort

• Place the largest item in its correct place

• Place the next largest item in its correct place, and so on

• Left aligned (ordering is from left to right)

33

Page 34: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

34

Worst CaseO(n2)

Best CaseO(n2)

Avg CaseO(n2)

Page 35: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

35

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

29 10 14 13 37

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

10 29 14 13 37

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

10 29 14 13 37

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

10 13 14 29 37

pass 2

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

10 13 14 29 37

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

10 13 14 29 37

pass 3

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

10 13 14 29 37

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

10 13 14 29 37

pass 4

Page 36: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Bubble sort

• Compare adjacent elements and exchange them if they are out of order

• Will move the largest (or smallest) elements to the end of the array

• Repeating this process will eventually sort the array into ascending (or descending) order

36

Page 37: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

37

Worst CaseO(n2)

Best CaseO(n)

Avg CaseO(n2)

Page 38: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

38

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

29 10 13 14 37

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

29 10 13 14 37

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

29 10 14 13 37

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

29 10 14 13 37

pass 1

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

29 10 14 13 37

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

29 10 13 14 37

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

29 10 13 14 37

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

10 29 13 14 37

Page 39: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Insertion sort

• Partition the array into two regions: sorted and unsorted

• Take each item from the unsorted region and insert it into its correct order in the sorted region

39

Page 40: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

40

Worst CaseO(n2)

Best CaseO(n)

Avg CaseO(n2)

Page 41: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

41

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

29 10 14 13 37

10pass 1

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

10 29 14 13 37

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

10 29 14 13 37

14

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

10 14 29 13 37

pass 2

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

10 14 29 13 37

pass 313

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

10 14 29 29 37

13

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

10 14 29 29 37

13

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

10 13 14 29 37

Page 42: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Merge sort

• Divide an array into halves

• Sort each half

• Merge the sorted halves into one sorted array

• Requires additional array

• Use with external data

42

Page 43: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

43

Worst CaseO(nlogN)

Best CaseO(nlogN)

Avg CaseO(nlogN)

Page 44: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

44

MergeSort(inout array:ItemArray, in first:integer, int last:integer) // sort the array by // 1. sorting the first half // 2. sorting the second half // 3. merging the two halves if (first < last) { mid = (first + last) / 2 MergeSort(array, first, mid) MergeSort(array, mid + 1, last) // merge sorted halves, array[first..mid] // and array[mid + 1..last] Merge(array, first, mid, last) }

Page 45: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

45

10 3 5 22 40 2 9 n

10 3 5 22 40 2 9 n/2

10 3 5 22 40 2 9 n/4

10 3 5 22 40 2 9 n/8

Page 46: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

46

2 3 5 9 10 22 40 n

3 5 10 22 2 9 40 n

3 10 5 22 2 40 9 n

10 3 5 22 40 2 9 n

Page 47: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Quick sort

• Partition an array into items that are less than the pivot and those that are greater than or equal to the pivot

• Sort the left section

• Sort the right section

47

Page 48: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

48

Worst CaseO(n2)

Best CaseO(nlogN)

Avg CaseO(nlogN)

Page 49: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

49

QuickSort(inout array:ItemArray, in first:integer, in last:integer) { if (first < last) { Choose array[0] as pivot Partition array[first...last] about p

// the partition is // array[first..pivotIndex...last]

// sort S1 QuickSort(array, first, pivotIndex - 1) // sort S2 QuickSort(array, pivotIndex + 1, last) }

Page 50: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

50

Partition(inout array:ItemArray, in first:integer, in last:integer, out pivotIndex:integer) p = array[first] lastS1 = first firstUnknown = first + 1 while (firstUnknown <= last) { if (array[firstUnknown < p) Move array[firstUnknown] into S1 else Move array[firstUnknown] into S2 } swap array[first] with array[lastS1] pivotIndex = lastS1

Page 51: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Initial state

51

lastS1 = first firstUnknown = first + 1

p ?

last

Unknown

first, lastS1

firstUnknown

Page 52: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

S1: swap

52

p

lastfirst firstUnknown

S1 (<p) S2 (>=p) <p

lastS1

>=p

p

lastfirst firstUnknown

S1 (<p) S2 (>=p)<p

lastS1

>=p

Page 53: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

S2: increment

53

p

lastfirst firstUnknown

S1 (<p) S2 (>=p) >=p

lastS1

p

lastfirst firstUnknown

S1 (<p) S2 (>=p) >=p

lastS1

Page 54: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Swap pivot

54

p

lastfirst

S1 (<p) S2 (>=p)<p

lastS1

p

lastfirst

S1 (<p) S2 (>=p)p

lastS1

Page 55: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

55

9 3 10 11 2 30

9 3 10 11 2 30

9 3 10 11 2 30

9 3 10 11 2 30

9 3 2 11 10 30

9 3 2 11 10 30

2 3 9 11 10 30

Page 56: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Radix sort

• Each number has the same number of digits

• Distribute in bins by digit (right to left)

• Collect the bins

• Repeat with next digit

• Requires more space (n-numbers * 10 bins)

56

Page 57: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

57

Worst CaseO(n)

Best CaseO(n)

Avg CaseO(n)

Page 58: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

58

RadixSort(inout array:ItemArray, in n:integer, in d:integer) for (j = d down to 1) { Initialize 10 groups to empty Initialize a counter for each group to 0 for (i = 0 through n-1) { k = jth digit of array[i] Place array[i] at the end of group k Increase kth counter by 1 } Replace the items in array with all items in group 0, followed by all the items in group 1 and so on. }

Page 59: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

59

123 004 034 240 023 100 009

0 240 100123 123 0234 004 03456789 009

Page 60: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

60

240 100 123 023 004 034 009

0 100 004 00912 123 0233 0344 24056789

Page 61: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

61

100 004 009 123 023 034 240

0 004 009 023 0341 100 1232 2403456789

Page 62: 18.Searching and Sorting Algorithms - SIUEstornar/courses/notes/cs240/18.Searching...Searching and Sorting Algorithms 1 Algorithm Efficiency deals with • Methods of solution rather

Summary

62

Sort Worst Avg

Selection n2 n2

Bubble n2 n2

Insertion n2 n2

Merge nlogN nlogNQuick n2 nlogNRadix n nTree nlogN nlogNHeap nlogN nlogN