15-112 Fundamentals of Programming Week 3 - Lecture 2: Intro to - - PowerPoint PPT Presentation

15 112 fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

15-112 Fundamentals of Programming Week 3 - Lecture 2: Intro to - - PowerPoint PPT Presentation

15-112 Fundamentals of Programming Week 3 - Lecture 2: Intro to efficiency + Searching and sorting + Big O June 1, 2016 Principles of good programming Correctness Your program does what it is supposed to. Handles all cases (e.g. invalid user


slide-1
SLIDE 1

June 1, 2016

15-112 Fundamentals of Programming

Week 3 - Lecture 2: Intro to efficiency + Searching and sorting + Big O

slide-2
SLIDE 2

Principles of good programming

Correctness Maintainability Efficiency In terms of running time and memory space used. Your program does what it is supposed to. Handles all cases (e.g. invalid user input). Readability, clarity of the code. Reusability for yourself and others (proper use of functions/methods and objects) programs that are easy to handle and debug.

slide-3
SLIDE 3

Why care about efficiency?

multiplying two integers protein structure prediction factoring integers sorting a list computing Nash Equilibria of games simulating quantum systems building AI proving theorems

slide-4
SLIDE 4

The Plan > Searching a given list > How to properly measure running time > Sorting a given list

  • Linear search
  • Binary search
  • Selection sort
  • Bubble sort

> Big-Oh notation

slide-5
SLIDE 5

Motivating example: searching a list

Given a list of integers, and an integer, determine if the integer is in the list. 3 1 9 4 8 7 6 2 5 6 How many steps in the algorithm?

slide-6
SLIDE 6

Motivating example: searching a list

3 1 9 4 8 7 6 2 5 6 How many steps in the algorithm? Given a list of integers, and an integer, determine if the integer is in the list.

slide-7
SLIDE 7

Motivating example: searching a list

1 6 6 How many steps in the algorithm? Given a list of integers, and an integer, determine if the integer is in the list.

slide-8
SLIDE 8

Motivating example: searching a list

1 6 6 How many steps in the algorithm? running time of an algorithm depends on:

  • size of input (e.g., size of the list)
  • the values in the input

Given a list of integers, and an integer, determine if the integer is in the list.

slide-9
SLIDE 9

Measuring running time

running time of an algorithm depends on:

  • size of input (e.g., size of the list)
  • the values in the input
slide-10
SLIDE 10

Measuring running time

running time of an algorithm depends on:

  • size of input (e.g., size of the list)
  • the values in the input

size of the list: Want to know running time with respect to any list size. N = list size Measure running time as a function of N.

slide-11
SLIDE 11

Measuring running time

running time of an algorithm depends on:

  • size of the list (size of input)
  • the values in the input

the values in the input: Measure running time with respect to worst input. worst input = input that leads to most number of steps

slide-12
SLIDE 12

Measuring running time

  • for strings: = number of characters

N

  • for lists: = number of elements

N

  • for ints: = number of digits

N > Running time is a function of . N > Look at worst-case scenario/input of length . N > Count algorithmic steps. > Ignore constant factors. (e.g. ) N 2 ≈ 3N 2 How to properly measure running time (use Big-Oh notation) > Input length/size denoted by (and sometimes by ) N n

slide-13
SLIDE 13

The Plan > Searching a given list > How to properly measure running time > Sorting a given list

  • Linear search
  • Binary search
  • Selection sort
  • Bubble sort

> Big-Oh notation

slide-14
SLIDE 14

Searching for an element in a list

N steps Can’t do better (in the worst case) How many steps does this take? This algorithm is called Linear Search. Given a list of integers, and an integer, determine if the integer is in the list.

slide-15
SLIDE 15

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 running time: N steps Can we do better? 99 How would you search for a name in a phonebook? 60 50 Given a sorted list of integers, and an integer, determine if the integer is in the list.

slide-16
SLIDE 16

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 Binary Search Start in the middle 99 60 50

slide-17
SLIDE 17

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 Binary Search Start in the middle 99 If (element > middle), can ignore left half of the list. If (element < middle), can ignore right half of the list. Repeat process on the remaining half. 60 50

slide-18
SLIDE 18

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 Binary Search 99 60 50 Start in the middle If (element > middle), can ignore left half of the list. If (element < middle), can ignore right half of the list. Repeat process on the remaining half.

slide-19
SLIDE 19

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 Binary Search 60 50 99 Start in the middle If (element > middle), can ignore left half of the list. If (element < middle), can ignore right half of the list. Repeat process on the remaining half.

slide-20
SLIDE 20

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 Binary Search 60 50 99 Start in the middle If (element > middle), can ignore left half of the list. If (element < middle), can ignore right half of the list. Repeat process on the remaining half.

slide-21
SLIDE 21

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 Binary Search 60 50 99 Start in the middle If (element > middle), can ignore left half of the list. If (element < middle), can ignore right half of the list. Repeat process on the remaining half.

slide-22
SLIDE 22

Searching for an element in a sorted list

8 1 2 4 5 5 6 9 9 50 Binary Search How many steps does this take (in the worst case)? 60 50 99 When is this 1? ∼ log2 N At each step we halve the list.

N → N 2 → N 4 → N 8 → · · · → 1

After k steps: elements left.

N 2k

slide-23
SLIDE 23

N vs log N

How much better is log N compared to N ? N log N 2 1 8 3 128 7 1024 10 1,048,576 20 1,073,741,824 30 1,152,921,504,606,846,976 60 ~ 1 quintillion

slide-24
SLIDE 24

n vs log n

slide-25
SLIDE 25

Linear search vs Binary search

Linear Search Takes steps. Works for both sorted and unsorted lists. ∼ N Binary Search Takes steps. Works for only sorted lists. ∼ log2 N

slide-26
SLIDE 26

Linear search code

def linearSearch(L, target): for index in range(len(L)): if(L[index] == target): return True return False

How many steps in the worst case?

slide-27
SLIDE 27

Binary search code

def binarySearch(L, target): start = 0 end = len(L) - 1 while(start <= end): middle = (start + end)//2 if(L[middle] == target): return True elif(L[middle] > target): end = middle-1 else: start = middle+1 return False

How many steps in the worst case?

slide-28
SLIDE 28

The Plan > Searching a given list > How to properly measure running time > Sorting a given list

  • Linear search
  • Binary search
  • Selection sort
  • Bubble sort

> Big-Oh notation

slide-29
SLIDE 29

O(·)

The CS way to compare functions:

means , ignoring constant factors and small values of n f(n) ≤ g(n) ≡ f(n) = O(g(n)) f(n) is O(g(n))

slide-30
SLIDE 30

O(·)

The CS way to compare functions:

means , ignoring constant factors and small values of n 10n + 25 = O(n) 10n + 25 is O(n) ≡ 10n + 25 ≤ n

slide-31
SLIDE 31

Big Oh Notation

A notation to ignore constant factors and small n.

2n is O(n) 3n is O(n) 1000n is O(n) 0.0000001n is O(n) 2 log2 n is O(log n) 3 log2 n is O(log n) 1000 log2 n is O(log n) 0.0000001 log2 n is O(log n) log9 n is O(log n)

Running time of linear search is O(N) Running time of binary search is O(log N)

0.0000001n2 is not O(n) n is O(n2) n log7 n + 100 is not O(n)

slide-32
SLIDE 32

Big Oh Notation

Why ignore constant factors and small n?

  • Technology independent. Language independent.
  • We want to capture the essence of an

algorithm/problem.

  • Difference in Big Oh

a really fundamental difference.

slide-33
SLIDE 33

Big Oh Notation

Ignoring constant factors means ignoring lower order additive terms.

n2 + 100n + 500 is O(n2) 601n2 = n2 + 100n2 + 500n2 > n2 + 100n + 500 n2 + 100n + 500 n2 = 1 + 100n n2 + 500 n2

Lower order terms don’t matter! − → 1 Also:

slide-34
SLIDE 34

Big Oh Notation

slide-35
SLIDE 35

Big Oh Notation

slide-36
SLIDE 36

Important Big Oh Classes

Again, not much interested in the difference between and .

n n/2

We are very interested in the differences between log n <<< √n << n << n2 << n3 <<< 2n

slide-37
SLIDE 37

Important Big Oh Classes

Common function families: Constant:

O(1)

Logarithmic:

O(log n)

Square-root:

O(√n) = O(n0.5)

Linear:

O(n)

Loglinear:

O(n log n)

Quadratic:

O(n2)

Exponential:

O(kn)

slide-38
SLIDE 38

Important Big Oh Classes

slide-39
SLIDE 39

Exponential running time

If your algorithm has exponential running time e.g. ∼ 2n No hope of being practical.

slide-40
SLIDE 40

n vs 2n

2n n 2 1 8 3 128 7 1024 10 1,048,576 20 1,073,741,824 30 1,152,921,504,606,846,976 60

slide-41
SLIDE 41

Exponential running time example

Given a list of integers, determine if there is a subset

  • f the integers that sum to 0.
  • 3
  • 2

7 99 5 1 4

slide-42
SLIDE 42

Exponential running time example

Given a list of integers, determine if there is a subset

  • f the integers that sum to 0.

Exhaustive Search Try every possible subset and see if it sums to 0. Number of subsets is 2N So running time is at least 2N

  • 3
  • 2

7 99 5 1 4

slide-43
SLIDE 43

The Plan > Searching a given list > How to properly measure running time > Sorting a given list

  • Linear search
  • Binary search
  • Selection sort
  • Bubble sort

> Big-Oh notation

  • 1. Algorithm
  • 2. Running time
  • 3. Code
slide-44
SLIDE 44

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Find the minimum element. Put it on the left. Repeat process on the remaining n-1 elements. Selection Sort

slide-45
SLIDE 45

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Selection Sort

slide-46
SLIDE 46

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 4 Selection Sort

slide-47
SLIDE 47

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 4 Selection Sort

slide-48
SLIDE 48

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 4 Selection Sort

slide-49
SLIDE 49

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-50
SLIDE 50

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-51
SLIDE 51

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-52
SLIDE 52

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-53
SLIDE 53

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-54
SLIDE 54

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 0 Selection Sort

slide-55
SLIDE 55

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Swap current min with first element of the array Selection Sort

slide-56
SLIDE 56

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Swap current min with first element of the array Selection Sort

slide-57
SLIDE 57

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Selection Sort

slide-58
SLIDE 58

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 8 Selection Sort

slide-59
SLIDE 59

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 8 Selection Sort

slide-60
SLIDE 60

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-61
SLIDE 61

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-62
SLIDE 62

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-63
SLIDE 63

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-64
SLIDE 64

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Current min: 2 Selection Sort

slide-65
SLIDE 65

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Swap current min with first element of unsorted part Selection Sort

slide-66
SLIDE 66

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Swap current min with first element of unsorted part Selection Sort

slide-67
SLIDE 67

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Selection Sort

slide-68
SLIDE 68

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 8 Selection Sort

slide-69
SLIDE 69

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 8 Selection Sort

slide-70
SLIDE 70

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 7 Selection Sort

slide-71
SLIDE 71

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 7 Selection Sort

slide-72
SLIDE 72

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 7 Selection Sort

slide-73
SLIDE 73

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 5 Selection Sort

slide-74
SLIDE 74

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 5 Selection Sort

slide-75
SLIDE 75

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Current min: 4 Selection Sort

slide-76
SLIDE 76

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Swap current min with first element of unsorted part Selection Sort

slide-77
SLIDE 77

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Swap current min with first element of unsorted part Selection Sort

slide-78
SLIDE 78

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Selection Sort

slide-79
SLIDE 79

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Current min: 7 Selection Sort

slide-80
SLIDE 80

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Current min: 7 Selection Sort

slide-81
SLIDE 81

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Current min: 7 Selection Sort

slide-82
SLIDE 82

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Current min: 5 Selection Sort

slide-83
SLIDE 83

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Current min: 5 Selection Sort

slide-84
SLIDE 84

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 7 99 5 8 Swap current min with first element of unsorted part Selection Sort

slide-85
SLIDE 85

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 99 7 8 Swap current min with first element of unsorted part Selection Sort

slide-86
SLIDE 86

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 99 7 8 Selection Sort

slide-87
SLIDE 87

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 99 7 8 Current min: 99 Selection Sort

slide-88
SLIDE 88

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 99 7 8 Current min: 99 Selection Sort

slide-89
SLIDE 89

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 99 7 8 Current min: 7 Selection Sort

slide-90
SLIDE 90

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 99 7 8 Current min: 7 Selection Sort

slide-91
SLIDE 91

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 99 7 8 Swap current min with first element of unsorted part Selection Sort

slide-92
SLIDE 92

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 99 8 Swap current min with first element of unsorted part Selection Sort

slide-93
SLIDE 93

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 99 8 Selection Sort

slide-94
SLIDE 94

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 99 8 Current min: 99 Selection Sort

slide-95
SLIDE 95

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 99 8 Current min: 99 Selection Sort

slide-96
SLIDE 96

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 99 8 Current min: 8 Selection Sort

slide-97
SLIDE 97

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 99 8 Swap current min with first element of unsorted part Selection Sort

slide-98
SLIDE 98

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 8 99 Swap current min with first element of unsorted part Selection Sort

slide-99
SLIDE 99

Selection Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 8 99 Done! Selection Sort

slide-100
SLIDE 100

Selection Sort: Running Time

Sort a given list of integers (from small to large). 2 4 5 7 8 99 Selection Sort How many steps does this take (in the worst case)? (As N increases, small terms lose significance.) ∼ N + (N − 1) + (N − 2) + · · · + 1 = N 2 2 + N 2 Running time is . O(N 2)

slide-101
SLIDE 101

Selection Sort: Code

2 8 7 99 4 5

start len(a) - 1

Find the min position from start to len(a) - 1

min position

Swap elements in min position and start Increment start Repeat Selection sort snapshot:

slide-102
SLIDE 102

Selection Sort: Code

2 8 7 99 4 5

start len(a) - 1

Find the min position from start to len(a) - 1

min position

Swap elements in min position and start for start = 0 to len(a)-1: Selection sort snapshot:

slide-103
SLIDE 103

Selection Sort: Code

def selectionSort(a): Find the min position from start to len(a) - 1 Swap elements in min position and start

2 8 7 99 4 5

start len(a) - 1 min position for start = 0 to len(a)-1: for start in range(len(a)): currentMinIndex = start for i in range(start, len(a)): if(a[i] < a[currentMinIndex]): currentMinIndex = i (a[currentMinIndex], a[start]) = (a[start], a[currentMinIndex])

slide-104
SLIDE 104

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-105
SLIDE 105

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 8 2 7 99 5 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-106
SLIDE 106

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-107
SLIDE 107

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 8 7 99 5 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-108
SLIDE 108

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 99 5 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-109
SLIDE 109

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 99 5 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-110
SLIDE 110

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 99 5 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-111
SLIDE 111

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 5 99 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-112
SLIDE 112

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 5 99 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-113
SLIDE 113

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 5 99 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-114
SLIDE 114

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 5 99 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-115
SLIDE 115

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 7 8 5 99 4 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-116
SLIDE 116

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 8 5 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-117
SLIDE 117

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 8 5 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-118
SLIDE 118

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 8 5 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-119
SLIDE 119

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 5 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-120
SLIDE 120

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 5 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-121
SLIDE 121

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 5 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-122
SLIDE 122

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 5 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-123
SLIDE 123

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 5 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-124
SLIDE 124

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 5 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-125
SLIDE 125

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 7 5 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-126
SLIDE 126

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-127
SLIDE 127

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-128
SLIDE 128

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-129
SLIDE 129

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-130
SLIDE 130

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-131
SLIDE 131

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-132
SLIDE 132

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-133
SLIDE 133

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-134
SLIDE 134

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-135
SLIDE 135

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-136
SLIDE 136

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-137
SLIDE 137

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-138
SLIDE 138

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-139
SLIDE 139

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 4 5 7 8 99 2 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-140
SLIDE 140

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 8 99 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort

slide-141
SLIDE 141

Bubble Sort: Algorithm

Sort a given list of integers (from small to large). 2 4 5 7 8 99 Compare each pair of adjacent items (left to right). Swap them if they are in the wrong order. Repeat until no more swaps are needed. Bubble Sort Large elements “bubble up”

slide-142
SLIDE 142

Bubble Sort: Running Time

Sort a given list of integers (from small to large). 2 4 5 7 8 99 How many steps does this take (in the worst case)? Bubble Sort O(N 2)

slide-143
SLIDE 143

Bubble Sort: Code

repeat until no more swaps: for i = 0 to end: if a[i] > a[i+1], swap a[i] and a[i+1] decrement end Bubble sort snapshot 4 7 5 8 99 2

end a[i] a[i+1]

slide-144
SLIDE 144

Bubble Sort: Code

4 7 5 8 99 2

end a[i] a[i+1] repeat until no more swaps: for i = 0 to end: if a[i] > a[i+1], swap a[i] and a[i+1] decrement end def bubbleSort(a): swapped = True end = len(a)-1 while(swapped): swapped = False for i in range(end): if(a[i] > a[i+1]): (a[i], a[i+1]) = (a[i+1], a[i]) swapped = True end -= 1

slide-145
SLIDE 145

Comparison: Selection Sort vs Bubble Sort

How about best case? Bubble sort: If your list is close to being sorted, bubble sort can be better. Selection sort: Is there a better way? Worst case both take steps. O(N 2) O(N 2) O(N)

slide-146
SLIDE 146

Exercise Write the code yourself: linear search binary search selection sort bubble sort