1 / 13

Lecture 2: Introduction to Algorithms

Lecture 2: Introduction to Algorithms. Review Linear Search Binary Search Selection Sort Reading. Review. Algorithm: Textbook Definition : A well-ordered collection of unambiguous and effectively computable operations that when executed produces a result and halts in a finite

burt
Download Presentation

Lecture 2: Introduction to Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2: Introduction to Algorithms • Review • Linear Search • Binary Search • Selection Sort • Reading

  2. Review Algorithm: Textbook Definition: A well-ordered collection of unambiguous and effectively computable operations that when executed produces a result and halts in a finite amount of time.

  3. Review • Computer Science • Textbook Definition: The study of algorithms including • Their formal and mathematical properties • Their hardware realizations • Their linguistic realizations • Their applications

  4. Pseudocode • A programming language is a notation for specifying instructions that a computer can execute • Every (programming) language has a syntax and a semantics • Syntax specifies how something is said (grammar) • Semantics specifies what it means • Pseudocode is an informal programming language with English-like constructs modeled to look like statements in a Java-like language • Anyone able to program in any computer language should understand how to read pseudocode instructions. • When in doubt just write it out in English.

  5. Your first Algorithm Linear Search: (also called sequential search) Algorithm to determine whether a given name x appears on a list. Input: A list of n ≥1 names A[1], A[2], ... , A[n], and a given name x. Output. The message "Sorry, x is not on the list" if x is not on the list. Otherwise, the message: "x occurs at position i on the list."

  6. Linear Search Here are the instructions for linear search written in pseudocode: found = "no"; i=1; while (found == "no" and i <= n) { if (A[i] == x) { found = "yes"; location = i; } i++; } if (found == "no") { print ("Sorry, " + x + " is not on the list"); } else { print (x + " occurs at position " + location + " on the list"); }

  7. Introduction to Algorithms • Definition of an algorithm: A well-ordered collection of unambiguous and effectively computable operations that, when executed, produces a result and halts in a finite amount of time. • There are three components to an algorithm: • A set of inputs, where each input is a finite sequence of items. • A set of outputs, where each output is a finite sequence of items. • A method consisting of a finite sequence of instructions, each one of which can be mechanically executed in a fixed length of time with a fixed amount of resources. The method must produce an output for each input in the set of possible inputs in a finite amount of time.

  8. Introduction to Algorithms • Input size: associated with each input is a measure of its size. How we measure the size can vary from problem to problem. Examples: • Number of digits as in the number of digits in a number (Often we use binary digits). • Number of characters in a string. • Number of items as in the number of items to be searched or sorted.

  9. Binary Search • Searching a sorted list • How can we exploit the structure that a sorted list provides? • Start in the middle and determine in which half of the list our target lives • Repeat this procedure eliminating half of the remaining list elements on each iteration • What is the maximum number of comparisons required here?

  10. Binary Search Input: A list of n ≥1 numbers A[1], A[2], ... , A[n], and a target number x. Output. The message "Sorry, x is not on the list" if x is not on the list. Otherwise, the message: "x occurs at position i on the list."

  11. Pseudocode for Binary Search found = "no"; begin = 1; end = n; while (found == "no" and begin  end) { m = begin + floor((end-begin)/2) if(A[m] == x) { found = "yes"; location = m; } if(A[m]>x) end = m-1; if(A[m]<x) begin = m+1; } if (found == "no") { print ("Sorry, " + x + " is not on the list"); } else { print (x + " occurs at position " + location + " on the list"); }

  12. Selection Sort Algorithm to sort the items on a list into nondecreasing order Input. A list of n ≥1 elements A[1], A[2], ... , A[n]. Output. The list sorted in nondecreasing order. for (i = 1; i < length(A); i++) { locmin=i; for (j=i+1;j <= length(A); j++) { if (A[locmin] > A[j]) locmin=j; } exchange(A[locmin], A[i]); }

  13. Reading • Chapters 1-3 in Schneider and Gersting

More Related