General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
 1 Sorting. For computer, sorting is the process of ordering data. [ ]  [ ] [ “Tom”, “Michael”, “Betty” ]  [ “Betty”, “Michael”,
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
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.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
General Computer Science for Engineers CISC 106 Lecture 08
Quicksort
General Computer Science for Engineers CISC 106 Lecture 09 James Atlas Computer and Information Sciences 9/25/2009.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/30/2009.
Searching Arrays Linear search Binary search small arrays
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/26/2009.
General Computer Science for Engineers CISC 106 Lecture 08 James Atlas Computer and Information Sciences 9/21/2009.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/21/2009.
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Chapter 16: Searching, Sorting, and the vector Type.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
General Computer Science for Engineers CISC 106 Lecture 11 James Atlas Computer and Information Sciences 07/27/2009.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
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 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
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.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009.
General Computer Science for Engineers CISC 106 Lecture 05 James Atlas Computer and Information Sciences 6/22/2009.
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 16: Searching, Sorting, and the vector Type.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
General Computer Science for Engineers CISC 106 Lecture 10 James Atlas Computer and Information Sciences 07/15/2009.
Searching and Sorting Algorithms
Sorting Mr. Jacobs.
Teach A level Computing: Algorithms and Data Structures
Algorithm design and Analysis
Topic 24 sorting and searching arrays
Presentation transcript:

General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009

Important Notes on Exam Write code Study labs + project Study Midterm review

Switch construct color = ‘yellow’; switch (color) case ‘red’ disp(‘Stop now!’); case ‘green’ disp(‘Proceed through intersection.’); case ‘yellow’ disp(‘Prepare to stop.’); otherwise disp(‘Illegal color encountered.’); end

Logical Operators &, && - AND |, || - OR ~ - NOT

a = [ ] b = [1; 2; 3; 4] c = [1 2; 3] (error) d = [1 2; 3 4] f = d(1,2) g(4,5) = 7 a(3:end) a(1:2:end) d’ Array commands

For Loops Used when you know how many times code is to be executed. Syntax for = : : Variable is initially the start value At end of iteration variable changes by increment If value is not greater than end the loop runs again.

Example Problem total = 0; for i = 1:1:1000 loop starts at 1 total = total + i;loop increments by 1 end loop ends at 1000 disp(total);

A Loop Analogy (for) The runner executes a loop. If they know the distance they want to run For loop for lapCount = start : 1 : end runLap() end

A Loop Analogy (while) The runner executes a loop. If they don’t know the distance they want to run (run until tired) While loop tired = false; while(~tired) tired = runLap() end

Linear Search Given [ ] find which position 6 occupies Alternatively, does the array contain the number 6? foundIndex = -1; for index = 1:length(values) if (values(index) == 6) foundIndex = index; end

Binary Search Now, what if the array is sorted, can we search it faster?

Binary Search Find N in list Pick a number X halfway between the start and end of the list If X == N we are done else if X < N search top half of list else search bottom half of list

Algorithm for sorting (Selection Sort) 1. Find the minimum data in the set 2. Swap it with the first element of the set 3. Repeat Steps 1-2 for the remaining elements

Running Time (Selection Sort) loop progress

Algorithm for sorting (Quick Sort) 1. Choose a random pivot point 2. Split your data into two sets: 1.Low - the data < pivot 2.High - the data > pivot 3. Repeat Steps 1-2 for both data sets 4. The sorted data is [low pivot high]

Running Time (Quick Sort) recursion progress If we divide the size of the data, n, in half at each step, how many steps does this take? log n area of table = width * height = n * log n

Structures in MATLAB

A Database Application Given: Name: Chris Credits: 27 Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/2012

Given: We can implement it with arrays like this: Name Credits Grad Name: Chris Credits: 27 Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/ Chris12/15/ Sola05/17/ Roger06/10/ Tom05/22/2012 A Database Application

Given: OR we can do it like this an array with structs:.d Name: Chris Credits: 27 Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/2012 Students (1). Name: Chris Students (1).Credits: 27 Students (1). Graduation: 12/15/2011 Students (2).Name: Sola Students (2).Credits: 18 Students (2).Graduation: 05/17/2011 Students (3). Name: Roger Students (3). Credits: 55 Students (3). Graduation: 06/10/2009 Students (4). Name: Tom Students (4). Credits: 15 Students (4). Graduation: 05/22/2012 A Database Application

MATLAB Array Initialization y = []; for i = 1:10 y(i) = i; end; This is an example of “growing” an array

MATLAB Array Initialization y = zeros(1,10); for i = 1:10 y(i) = i; end; Initializes the array first

Vectorization Additional examples ◦ x = [ ]; ◦ x < 3 ◦ x(x < 3) ◦ x(x 3) x < 3 produces a mask

Masking Masking selects only certain elements of an array to perform an operation Masking uses an array of only 0’s and 1’s that is the same size as the argument ◦ y = x < 3 ◦ whos y ◦ y is a mask of x that selects only the elements that are less than 3

Masking x = [ ]; y = x < 3 x(y) = x(y).* 2;

MATLAB functions - find find ◦ locates index of all nonzero elements of array z = [ ]; find(z) ◦ [1 2 6]

MATLAB functions - any/all x = [ ]; any(x < 3) any(x < 0) all(x > 1) all(x > 0)

MATLAB functions - randi rand() randi(100)