PseudocodePseudocode
Set i = 1Set i = 1While i < 10While i < 10
1. print [i “bottles of beer on a stone”]1. print [i “bottles of beer on a stone”]2. print [i “bottles of beer”]2. print [i “bottles of beer”]3. print [“if one of those bottles should 3. print [“if one of those bottles should
happen to clone”]happen to clone”]4. print [i+1 “bottles of beer on a 4. print [i+1 “bottles of beer on a stone”]stone”]5. INCREMENT i (i = i+1 or “i++”)5. INCREMENT i (i = i+1 or “i++”)
PseudocodePseudocode
Set num = 0, set sum = 0Set num = 0, set sum = 0While there are more numbers to be readWhile there are more numbers to be read
1. read(x) (let x = next number read)1. read(x) (let x = next number read)2. sum = sum + x (increment sum by x)2. sum = sum + x (increment sum by x)3. num++ (increment num by 1)3. num++ (increment num by 1)
Output: sum/numOutput: sum/num
walk through code with data = 3, 5, 2, 8, 7
PseudocodePseudocode
GCD (a, b)GCD (a, b)
While remainder r is not 0While remainder r is not 0
1. find q, and r < b, so that a=bq+r1. find q, and r < b, so that a=bq+r
2. set a = b2. set a = b
set b = rset b = r
Output aOutput a
stepping through codestepping through codeGCD (a, b)GCD (a, b)While remainder r is not 0While remainder r is not 0
1. find q and r < b 1. find q and r < b so that a=bq+rso that a=bq+r
2. set a = b2. set a = b set b = rset b = r
Output aOutput a
a b q r
42 15 ? ?Initial values
42 15 2 12after step 1
15 12 2 12after step 2
15 12 1 3step 1 (2nd time)
12 3 1 3step 2 (2nd time)
12 3 4 0step 1 (3rd time)
3 0 4 0step 2 (3rd time)
• for understanding• for debugging
Linear SearchLinear SearchAssume data M(0) … M(n)Assume data M(0) … M(n)Input x; Find memory location i such that M(i)=xInput x; Find memory location i such that M(i)=x
(or report that x is not in list)(or report that x is not in list)
How many steps does it take when Linear_search is run?
Linear_search (x)Linear_search (x)i = 0i = 0WHILE i < n+1 WHILE i < n+1 IF M(i) = x then halt and output iIF M(i) = x then halt and output i
ELSE i = i+1ELSE i = i+1Output “not in the list”.Output “not in the list”.
Binary SearchBinary Search
Assume Assume sortedsorted data M(0) … M(n) data M(0) … M(n)
Binsearch (x)Binsearch (x)put left, right fingers on 0 and nput left, right fingers on 0 and nlet let midpointmidpoint be the point halfway between. be the point halfway between.WHILE x not equal M(midpoint)WHILE x not equal M(midpoint) IF M(midpoint) < x IF M(midpoint) < x
THEN move left finger to midpointTHEN move left finger to midpoint ELSE move right finger to midpointELSE move right finger to midpoint
set midpoint halfway between fingersset midpoint halfway between fingers END WHILEEND WHILE
Binary SearchBinary Search
Assume sorted data M(0) … M(n)Assume sorted data M(0) … M(n)
Binsearch (x)Binsearch (x)left = 0left = 0right = nright = n
midpoint = (left + right) / 2midpoint = (left + right) / 2WHILE x notequal M(midpoint)WHILE x notequal M(midpoint)
IF M(midpoint) < x THEN left = midpointIF M(midpoint) < x THEN left = midpointELSE right = midpointELSE right = midpoint
midpoint = (left + right) / 2midpoint = (left + right) / 2 END WHILEEND WHILE
This actually has several bugs. Can you figure out what they are?
stepping through binary searchstepping through binary search
0 16 ?Initial values
8 16 8move left
8 16 12compute midpt
8 12 12move right
8 12 10compute midpt
8 10 10move right
left right midpoint
0 16 8compute midpt
ARRAY M( )
M(0) = 3M(1) = 4M(2) = 6M(3) = 21M(4) = 24M(5) = 27M(6) = 28M(7) = 30M(8) = 31M(9) = 33M(10)= 39M(11)= 44M(12)= 45M(13)= 48M(14)= 50M(15)= 55M(16)= 57
searching for 33
8 10 9compute midpt
Running TimesRunning Times
Binary SearchBinary Search
At each iteration, range is cut in halfAt each iteration, range is cut in half Total number of iterations to find an item among Total number of iterations to find an item among
N elements is y such thatN elements is y such that “ “cut N in half y times to get down to 1”cut N in half y times to get down to 1”
Yes, this is just (base 2) log NYes, this is just (base 2) log N Searching among 2Searching among 2300300 elements takes only 300 elements takes only 300
stepssteps (this exceeds the number of particles in the universe!!)(this exceeds the number of particles in the universe!!)
Sorting AlgorithmsSorting Algorithms
Different methods to put things in Different methods to put things in orderorder
Which are fastest methods?Which are fastest methods?
Bubble SortBubble Sort
Put left, right fingers on M(0), M(1)Put left, right fingers on M(0), M(1)
IF M(left) > M(right) THEN swap themIF M(left) > M(right) THEN swap them
Move left, right fingers to rightMove left, right fingers to right
IF right finger = nIF right finger = n
THEN done with this pass, start againTHEN done with this pass, start again
BUT next time only go up to n-1BUT next time only go up to n-1
Etc., for passes ending at n-2, n-3, Etc., for passes ending at n-2, n-3, etc.etc.
Aside: FOR loopsAside: FOR loops
new kind of iteration command (similar to new kind of iteration command (similar to WHILE)WHILE)
ExampleExampleFOR i = 1 to 10FOR i = 1 to 10
print 3*iprint 3*i ExampleExample
FOR count = 100 downto 1FOR count = 100 downto 1 print countprint count
Outputs 3,6,9,…,30
Outputs 100,99,98,…,1
Bubble SortBubble Sort
FOR max = n-1 downto 1FOR max = n-1 downto 1
FOR left = 1 upto maxFOR left = 1 upto max
right = left + 1right = left + 1
IF M(left) > M(right) THEN swap IF M(left) > M(right) THEN swap themthem
LET’S UNROLL THIS CODE TO MAKE SURE WE UNDERSTAND
make a pass up to position max
Bubble SortBubble SortFOR max = n-1 downto 1FOR max = n-1 downto 1
FOR left = 1 upto maxFOR left = 1 upto max
right = left + 1right = left + 1
IF M(left) > M(right) THEN swap IF M(left) > M(right) THEN swap themthem
Let max = n-1Let max = n-1
blah blah blahblah blah blah
Let max = n-2Let max = n-2
blah blah blahblah blah blah
… … etcetc
Let max = 2Let max = 2
blah blah blahblah blah blah
Let max = 1Let max = 1
blah blah blahblah blah blah
There are n-1 phasesDuring a phase, what happens?
A nested FOR loop is executed
The nested loop moves from 1 to maxand does a swap if necessary
Let’s look at an example with n = 4
make a pass up to position max
Bubble SortBubble SortFOR max = n-1 downto 1FOR max = n-1 downto 1
FOR left = 1 upto maxFOR left = 1 upto max
right = left + 1right = left + 1
IF M(left) > M(right) THEN swap IF M(left) > M(right) THEN swap themthem
max = 3max = 3
IF M(1) > M(2) swap themIF M(1) > M(2) swap them
IF M(2) > M(3) swap themIF M(2) > M(3) swap them
IF M(3) > M(4) swap themIF M(3) > M(4) swap them
max = 2max = 2
IF M(1) > M(2) swap themIF M(1) > M(2) swap them
IF M(2) > M(3) swap themIF M(2) > M(3) swap them
max = 1max = 1
IF M(1) > M(2) swap themIF M(1) > M(2) swap them
Now we can see the effect of the inner loop for each “version” of the outer loop…
…and the overall effect of the nested loops
make a pass up to position max
CorrectnessCorrectness
Why does it work?Why does it work? After first pass, the largest is in position nAfter first pass, the largest is in position n 22ndnd pass, the 2 pass, the 2ndnd largest is in position n-1 largest is in position n-1 33rdrd pass, the 3 pass, the 3rdrd largest is in position n-2 largest is in position n-2 etc.etc. nth pass, the nth largest is in position 1nth pass, the nth largest is in position 1
Running timeRunning time
Count comparisonsCount comparisons Don’t get bogged down in detailDon’t get bogged down in detail Iteration is the biggest contributorIteration is the biggest contributor Bubble Sort: (n-1) + (n-2) + … + 2 + Bubble Sort: (n-1) + (n-2) + … + 2 +
11= n(n-1)/2= n(n-1)/2= (n= (n22-n)/2-n)/2= n= n22/2 - n/2/2 - n/2≤ ≤ nn22/2 /2
Insertion SortInsertion Sort
Insert firstInsert firstInsert next into proper placeInsert next into proper placeInsert next into proper placeInsert next into proper placeetc.etc.
(typical method for sorting playing cards in hand)(typical method for sorting playing cards in hand)
Insertion SortInsertion SortFOR i = 1 to n /* Insert M(i) into correct place in new list T()FOR i = 1 to n /* Insert M(i) into correct place in new list T() { j = 1 /* Start looking at position T(1){ j = 1 /* Start looking at position T(1) WHILE M(i) < T(j) WHILE M(i) < T(j)
{ j = j+1{ j = j+1 }}
/* AHA! M(i) belongs right after T(j) /* AHA! M(i) belongs right after T(j) slideslide T(j+1), T(j+2), … over to T(j+2), T(j+3)… T(j+1), T(j+2), … over to T(j+2), T(j+3)… T(j+1) = M(i) T(j+1) = M(i) }}
where where slideslide is a separate “procedure” that we have written. is a separate “procedure” that we have written.
Running TimesRunning Times
Insertion Sort: 1 + 2 + 3 + 4 … + n-1Insertion Sort: 1 + 2 + 3 + 4 … + n-1 ≤ ≤ nn22/2 /2 (see previous (see previous
analysis)analysis)
Quick SortQuick Sort
A very fast method in practiceA very fast method in practice Used often, especially with large data Used often, especially with large data
setssets
Quick SortQuick Sort
12
34 5
67 8
9 Pick a “pivot’ at random
12
3
4 5
67 8
9 Compare each element to the pivot, placing it to left or right
Pivot is in correct place.Now sort the left, right using Quick Sort method…..
Quick SortQuick Sort12
3
4 5
67 8
9
Compare each element to the pivot, placing it to left or right
Pivot is in correct place.Now sort the left, right using Quick Sort method…..
Pick a pivot at random
56 8
9123
47
Quick SortQuick Sort
Quick Sort (in-class demo): Quick Sort (in-class demo): depends on how lucky you are with “pivot”depends on how lucky you are with “pivot” complex analysis shows on average, you are complex analysis shows on average, you are
luckylucky running time on average can be shown to be running time on average can be shown to be
about n log n about n log n
Quick calculationsQuick calculations Sort n=1,000,000,000 items with Sort n=1,000,000,000 items with
algorithms taking times nalgorithms taking times n22, and n log n, on , and n log n, on computers capable of doing 1,000,000,000 computers capable of doing 1,000,000,000 (a billion) comparisons per second?(a billion) comparisons per second?
How much time is taken by each How much time is taken by each algorithm?algorithm?
Demo: sorting applets:Demo: sorting applets: http://www.sorting-algorithms.com
Demo: asSORTed dances:Demo: asSORTed dances: http://algo-rythmics.ms.sapientia.ro/dance /
SummarySummary
pseudocodepseudocode stepping through codestepping through code basic algorithms: linear search, basic algorithms: linear search,
binary search, different sorting binary search, different sorting algorithmsalgorithms
notion of running time and notion of running time and computing approximate bounds to computing approximate bounds to determine efficiencydetermine efficiency