Lecture Note 1

Download Report

Transcript Lecture Note 1

ITEC 2620A
Introduction to Data Structures
Instructor: Prof. Z. Yang
Course Website:
http://people.math.yorku.ca/~zyang/itec
2620a.htm
Office: Tel 3049
Course Objective
• Course is about concepts
– what you can program, not how
• Traditionally, second course in computer science
– still separates top third of programmers
– not taught in any known college diploma program
• Learn and use efficient programming patterns
– “an efficient programmer can often produce programs
that run five times faster than an inefficient
programmer”
2
Textbooks
•“Data Structures and Algorithm Analysis in Java”
third edition by Clifford A. Shaffer.
•Lecture notes and announcements will be made
available at:
http://people.math.yorku.ca/~zyang/itec
2620a.htm
3
Marking Scheme
• Assignment 1: 10%
• Midterm: 30%
– about 1/3 code, 2/3 concepts
• Final: 60%
– about 1/3 code, 2/3 concepts
• Late Policy
– Late assignments will NOT be accepted. If you miss it for
an approved excuse, the weight will be added to the
midterm. NO makeup assignment will be provided.
– If you miss the midterm for an approved excuse, the
weight will be added to the weight of the final exam. A
medical note is required. NO makeup midterm will be
provided.
4
Course Organization
• Key concepts first
– searching, sorting, complexity analysis,
linked structures
• Concrete to concept
– searching  sorting  complexity analysis
– binary tree  recursion
5
Introduction
Need for Data Structure
• Computer
– Store and retrieve information
– Perform calculation
• Costs and benefits
– A data structure requires a certain amount
of space for each data item it stores, a
certain amount of time to perform a single
basic operation, and a certain amount of
programming effort.
7
Problem, Algorithms and
Programs
• Problem: a task to be performed
• Algorithm: a method or a process
followed to solve a problem
• Program: an instance, or concrete
representation, of an algorithm in some
programming language.
8
Algorithm Efficiency
• Design goals:
– Design an algorithm that is easy to
understand, code, and debug.
– Design an algorithm that makes efficient
use of the computer’s resources.
• Algorithm analysis
9
Three keys to programming
• Algorithm Design
– Sequence, branching, looping – flowcharts and
pseudocode
– Efficiency and time-space tradeoffs (linkages
between algorithms and data structures)
• Data Organization
– objects and classes
– data structures
• Modularization
– methods (modularization of algorithms)
– classes and inheritance (modularization of
data/objects)
10
Searching
Key Points
• Searching arrays
– Linear search
– Binary search
– Best, average, and worst cases
12
Searching in General
• “A query for the existence and location of an element
in a set of distinct elements”
• Elements have searchable keys associated with a
stored record.
– search on keys – e.g. name
– to find an element – e.g. phone number
• Two goals of searching
– existence  does the person exist?
– location  where are they in the phone book?
• Isolate searching for “keys” – all keys have the same
data type
13
Definition
• Suppose k1, k2, …, kn are distinct keys, and that
we have a collection T of n records of the form
(k1, I1), (k2, I2), …, (kn, In), where Ij is information
associated with key kj. Given a particular key
value K, the search problem is to locate the
record (kj, Ij) in T such that kj = K.
• Exact-match query
• Range query
14
Review of Array Algorithms
• Write a static method that determines the
range (the difference between the largest and
smallest element) for a fully populated array
of ints.
• How is the above example similar to
searching?
– find the largest/smallest value in a set of
elements
• Searching  find a given value in a set of
elements
15
Searching Arrays
• Write a static method that determines
the location (the array index) for a given
value in a fully populated array of ints,
return “–1” if the value is not in the
array.
• Example
16
Binary Search
• Make your first guess in the middle of the
range.
• If not the target element, determine which
sub-range you have to search.
• Make your next guess in the middle of this
sub-range.
• Repeat
• Example
17
Best, Worst and Average
• What’s the best case for search?
– Linear  first element  1 compare
– Binary  middle element  1 compare
• What’s the worst case for search?
– Linear  not there  n compares
– Binary  not there  logn compares
• What’s the average case for search?
– Linear  middle element  n/2 compares
– Binary  50/50…  logn - 1 compares
18
What’s the difference?
• Toronto phone book  2 million names
– Avg. Linear  1 million compares
– Avg. Binary  20 compares
• For 2 million records, binary search is 50,000
times faster on average.
• What’s the cost
– To do binary search, we need a sorted array
– Sorting…next lecture!
19