Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms in Computer Science Topics in CS. Terms & Concepts in this Unit  algorithms  Big O Notation  flow-charts  pseudo-code  Search Algorithms.

Similar presentations


Presentation on theme: "Algorithms in Computer Science Topics in CS. Terms & Concepts in this Unit  algorithms  Big O Notation  flow-charts  pseudo-code  Search Algorithms."— Presentation transcript:

1 Algorithms in Computer Science Topics in CS

2 Terms & Concepts in this Unit  algorithms  Big O Notation  flow-charts  pseudo-code  Search Algorithms  Sorting Algorithms

3 What's an Algorithm?  Definition : a formula or set of steps for solving a particular problem from Webopedia.comfrom Webopedia.com  Note the following:  computers are not smart  computers are efficient  Therefore, when it comes to algorithms…  the steps must be unambiguous (clear)  there must be a clear stopping point

4 Big O Notation  Definition : It's a way to measure the performance or complexity of an algorithm  from A Beginner's Guide to Big O Notation by Rob BellA Beginner's Guide to Big O Notation  Big O can either measure  the execution time or  the space in memory used  Big O always measures the worst-case scenario

5 An Example of a Worst-case  Let's say your sorting algorithm sorts an array that has the smallest item on the left and largest on the right by… 1. starting at the left-hand side of an array 2. swapping larger items on the left 3. with smaller items on the right 4. progressing 1 at a time through the list  For example:  In {3, 2, 5, 1}, the algorithm starts with the left and compares with each item to its right  What if the unsorted list is the following?  {99, 58, 50, 49, 45, 44, 23, 22, 20, 10, 9, 7, 4, 2, 1}  this becomes our worst case

6 Big O Formulas  Some examples are  O(1)  O(n)  O(n2)  The O stands for Order: that's the rate of growth of a function  The n stands for the number of items in the given data set For a more comprehensive list of examples, see The Big O Cheat Sheetsee The Big O Cheat Sheet

7 Why Re-invent the Wheel?  I could put a bunch of examples in this PowerPoint…  but I would rather point you towards a website that covers the topic better than I could  I present to you A Beginner's Guide to Big O Notation by Rob BellA Beginner's Guide to Big O Notation

8 Ways to Write Algorithms  Instructions/Directions – you choose how to represent the instructions  Recipes – Ingredients & Instructions  Pseudocode – Language Independent Code (doesn't work though)  Flowcharts – a visual reference for the way code gets executed

9 Pseudocode  Language neutral "code" to describe the instructions for an algorithm  Should be able to be followed and implemented in any programming language  Generally follows a loose syntax with keywords

10 Pseudocode Keywords  GET / READ (input)  SET / PUT / DISPLAY (output)  CALCULATE / COMPUTE (math)  INCREMENT (used for i++)  DECREMENT (used for i--)  IF condition THEN (conditional execution)  ELSE  ENDIF  FOR (iteration)  WHILE (repetition)  ENDWHILE  CALL (calling a method/function/subroutine)

11 Sample Pseudocode Algorithms SET moveCount to 1 FOR each row on the board FOR each column on the board IF gameBoard position (row, column) is occupied THEN CALL findAdjacentTiles with row, column INCREMENT moveCount END IF END FOR END FOR From PSEUDOCODE STANDARD

12 by Hundredvisionsguy Flowcharts  Flowcharts help illustrate control flow  Control flow (or flow of control) is the order in which code statements are executed in a program Welcome User Get name Get age Get Weight Pluto_age = age/258 dog_age = age*7 Print pluto_age Print dog_age End

13 by Hundredvisionsguy Flowchart Symbols  Input & Output – use a parallelogram  Write get for input  Write put or print for output  Processing of Data – Use a rectangle  You typically write it as a formula with operators, etc.  Decision Statements - use a diamond  Diamonds are used for…  While loops  Conditional execution  Write decisions as a T/F question  Two arrows come out  One for True (label ‘T’)  One for False (label ‘F’)  Terminal Statements  Oval  Write “end” Get username Print name count += 1 Is count > 0? TT F End

14 A More Comprehensive List of Flowchart Shapes Taken from Standard Flowchart Symbols and Their Usage by EdrawStandard Flowchart Symbols and Their Usage

15 by Chris Winikka Branching Statements  If structure  Notice how with false it simply skips the block of code and moves on.  If-else structure  The statement on the left represents the block under the if: statement  The statement on the right represents the block under the else: statement  If-elif-else structure  Each rhombus after the top one represents an ELSE IF statement  Notice the last rhombus

16 by Chris Winikka Flowcharts with While Loops  Sentinel-Controlled While Loop  Notice how we initialize the sentry variable  The “ask Why” block before the diamond was not necessary  Notice how we have to ask why Inside of the loop  Counter-controlled loop  This is one that counts down  What will the while loop look like? while (count > 0): { } TF

17 Do While Loop  with a do while loop, you will always perform at least 1 iteration  That's because the condition is after the loop body  Code sample from do (C# Reference)do (C# Reference)  Flowchart taken from Drawing a Structured FlowchartDrawing a Structured Flowchart

18 Traditional For Loops  Taken from Repetition and Loops by John EvenRepetition and Loops

19 For Each Loops Flowchart Example For Each Explanation  Python's for loops are for each loops  Other languages have them as well  Java  C#  PHP  Basically, a for each loop runs through an array/list/sequence  It performs an operation on each element  It's done when there are no more elements Are there any characters left? Letter = 1 st character print character Read next character y n continue w/program

20 Searching & Sorting Algorithms Applied Computer Science

21 Common Search Algorithms  Sequential/Linear Search  Binary Search  Hash table Search

22 Sequential Search Notes  O(n)  Doesn't need to be sorted to perform  Inefficient  But at least it can be easily calculated  Best Case :  found on first index position  Worst Case :  not found at all Algorithm 1. Iterate through a list 2. If there's a match 1. return index position 3. Else 1. return -1

23 Binary Search Notes  O(log n)  Array must be sorted first  Often used on binary search trees  I use this algorithm for the Guess my number game Algorithm  GET array  SET low to 0  SET high to length of array - 1  WHILE low <= high  mid = low + (high – low) / 2  IF A[mid] == target  RETURN mid  IF A[mid] < target  low = mid + 1  ELSE  high = mid – 1  RETURN null

24 Hash Table Search I don't have time to go through this now, for a good explanation try the following links: Hash Tables (on Spark Notes) Searching Algorithms (Battleship)

25 Sorting Algorithms A general overview

26 A Definition  You sort collections (arrays, vectors, lists, etc.)  To sort means to put in order from the smallest to the largest  smallest on the left  largest on the right  Only items that can be less than, equal to, or greater than can be sorted  numerically  alphabetically

27 Why sort in the first place?  How would you like to have your textbooks randomly place topics throughout your book?  How would you like it if phone books inserted names randomly?  Humans like to insert order  If you've ever played a card game, what's the first thing you do once you've been dealt cards?

28 Some of the types of sorts include…  bubble sort  insertion sort  selection sort  merge sort  quick sort  radix sort  etc…

29 Sorts Typically Fall into 2 Categories Linear Sorts  sort through entire arrays (at once)  sequentially  Examples:  Bubble Sort  Insertion Sort  Selection Sort Divide & Conquer Sorts  break up arrays into smaller sets  each set is sorted independently  Examples:  Merge Sort  Quick Sort  Radix Sort

30 How much does a sort cost?  Depends on the following factors:  how many comparisons get made  how many times values are switched  Each factor costs  time : due to…  comparisons  swaps  memory allocation : due to…  number of sub-arrays created  swaps (each placeholder takes up memory)  recursive calls

31 Bubble Sort Explanation  Uses multiple passes  works from left to right  in each pass, the largest number bubbles its way to the right  current value is compared to the next value  11, 3, 5, 32  if the item on the right is smaller, we swap  3, 11, 5, 32  We keep swapping until no more swaps are performed  After 1 pass, the largest value is at the end of the collection  See it visualized here See it visualized here Algorithm For I = 0 to N - 1 For J = 0 to N - 1 If (A[J] > A[J + 1] Temp = A[J] A[J] = A[J + ] A[J + 1] = Temp End-If End-For

32 Selection Sort Explanation  Also a linear sort  Works the opposite of bubble sort  We're looking for the smallest item in an array and swapping it with the first element in the unsorted array  See it visualized here See it visualized here Algorithm FOR I = 0 to N-1 do: Smallsub = I FOR J = I + 1 to N-1 do: IF A(J) < A(Smallsub) Smallsub = J END-IF END-FOR Temp = A(I) A(I) = A(Smallsub) A(Smallsub) = Temp End-For

33 Quicksort Algorithm  It's a divide and conquer algorithm  At each pass,  It selects a pivot element (usually from the end)  It compares all other items to the pivot and creates 3 sub arrays  LEFT: all elements less than the pivot move in front of it (to the left)  END: all elements greater than the pivot move after it (to the right of the pivot)  EQUAL: all elements equal to the pivot get placed next to it  After each pass, the LEFT and END sub arrays are sorted using the same method  It's recursive  The base case happens when there are no more sub arrays to pass it  When all items are in the EQUAL spot  See it visualized here See it visualized here

34 Quicksort Partition Method  Let's see an example of what happens to the array during 1 pass  Here is the array:  The pivot is the last item: 33  From QuicksortQuicksort [13, 66, 66, 11, 39, 33, 96, 33] [][33][][13, 66, 66, 11, 39, 33, 96] [13][33][] [66, 66, 11, 39, 33, 96][13][33][66] [66, 11, 39, 33, 96][13][33][66, 66] [11, 39, 33, 96][13, 11][33][66, 66] [39, 33, 96][13, 11][33][66, 66, 39] [33, 96][13, 11][33, 33][66, 66, 39] [96][13, 11][33, 33][66, 66, 39, 96]  Now let's look at a Python program


Download ppt "Algorithms in Computer Science Topics in CS. Terms & Concepts in this Unit  algorithms  Big O Notation  flow-charts  pseudo-code  Search Algorithms."

Similar presentations


Ads by Google