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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

MATH 224 – Discrete Mathematics
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
CHAPTER 11 Sorting.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Algorithm Efficiency and Sorting
Elementary Data Structures and Algorithms
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
The Program Design Phases
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Recursion, Complexity, and Sorting By Andrew Zeng.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Algorithms and their Applications CS2004 ( ) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Searching and Sorting Searching algorithms with simple arrays
16 Searching and Sorting.
Sorting Mr. Jacobs.
Introduction to Search Algorithms
COMP 53 – Week Seven Big O Sorting.
Lesson Objectives Aims Understand the following “standard algorithms”:
3.1 Algorithms (and real programming examples).
Teach A level Computing: Algorithms and Data Structures
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Basics of Recursion Programming with Recursion
Module 8 – Searching & Sorting Algorithms
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

Algorithms in Computer Science Topics in CS

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

Searching & Sorting Algorithms Applied Computer Science

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

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

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

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)

Sorting Algorithms A general overview

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

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?

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

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

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

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

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

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

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