Download Pseudocode, Algorithms - Fundamental Programming Concepts - Lecture Slides and more Slides Computer Programming in PDF only on Docsity! Today Pseudocode Algorithms Prime Sieve Number Guessing Sorting Numbers Switch/Case Lecture 06 Pseudocode, Algorithms docsity.com Definitions Algorithm is a step-by-step description of a calculation, like a recipe. Examples: Prime Sieve, Binary Search, Bubble Sort. Pseudocode is a high-level description of a program or algorithm. It can be converted to a program easily. Lecture 06 Pseudocode, Algorithms docsity.com Measuring Performance tic/toc tic starts the timer, toc returns the elapsed time. Comparing primes functions N = input('Enter N: '); tic % Start timer p0 = primes(N); % Call built−in primes t0 = toc; % Stop timer and % store elapsed time % Let's also measure our functions tic; p1 = primes1(N); t1 = toc; tic; p2 = primes2(N); t2 = toc; Lecture 06 Pseudocode, Algorithms docsity.com Why is it slow? We check isprime for 3,5,7,9,11,13,15,.. Why do we check 9? 15? 21? 25? .. Current Version function p = primes2 (N) if N>1, p = [2]; else p = []; end % there are unnecessary checks for j = 3:2:N if isprime(j) p = [p, j]; end end end Lecture 06 Pseudocode, Algorithms docsity.com Sieve of Eratosthenes Prime Sieve Idea: Eliminate the multiples of a number ahead of time, so that we don’t need to check it. Algorithm % Create an array X of all 1's of length N % Set X(1) to 0 % Find position k of next 1 in the X array % If k is less than or equal to sqrt(N) % Set X(2*k), X(3*k), X(4*k) ... to zero % Go back to finding k % Else % Find the indices of all 1's in X array % These indices are prime numbers Lecture 06 Pseudocode, Algorithms docsity.com Switch/Case switch branches based on several cases Usage switch switch expr case case expr1 do something1 case case expr2 do something2 case {case expr3, case expr4} do something3 otherwise do some other thing end Lecture 06 Pseudocode, Algorithms docsity.com Switch/Case - Examples n = input('enter a digit: '); switch n case 1 disp('one'); case 2 disp('two'); case 3 disp('three'); case 4 disp('four'); % and many more otherwise disp('something else'); end Lecture 06 Pseudocode, Algorithms docsity.com Switch/Case - Examples hall = 'Morrill'; switch hall case {'Morrill','Goldwin Smith'} disp('Arts Quad'); case {'Warren','Roberts'} disp('Ag Quad'); case {'Upson','Duffield','Kimball'} disp('Engineering Quad'); otherwise disp('Unknown Hall'); end Lecture 06 Pseudocode, Algorithms docsity.com Number Guessing - 3 numbergame3.m disp('Pick a number between 1−100!'); pause high = 100; low = 1; trial = 0; while 1 guess = floor((high+low)/2); trial = trial + 1; fprintf('(Trial %d) Is it %d ? ', trial, guess); response = input('(y −> Yes, d −> Go down, u −> Go up) ','s'); switch response case {'y','Y'} disp('Yay!'); break; case {'u','U'} low = guess; case {'d','D'} high = guess; otherwise disp('Please enter ''y'', ''d'' or ''u'''); response = input('(y −> Yes, d −> Go down, u −> Go up) ','s'); end end Lecture 06 Pseudocode, Algorithms docsity.com Bubble Sort function x = bubblesort(x) disp('==================== INPUT ARRAY =============================') fprintf('(Array x) '); disp(x) disp('==============================================================') n = length(x); step = 0; for i=1:n for j=1:n−1 if x(j) > x(j+1) temp = x(j+1); x(j+1) = x(j); x(j) = temp; end step = step + 1; fprintf('(Step %2d) ',step); disp(x); pause end fprintf('(Pass %2d) ',i); disp(x); pause end Lecture 06 Pseudocode, Algorithms docsity.com