Download Data Structures and Algorithm Analysis

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Structures and Algorithm
Analysis
(Or how to design efficient code that
works)
Why Learn Data Structures?
• Write faster code.
• I’ll prove to you that a faster computer cannot fix a bad/slow
algorithm.
• Write code that saves memory.
• Efficiency – save computer resources.
reduce
in essence…
Always Efficient Code?
•
Efficiency versus clarity
–
–
–
–
–
•
Simplest code is easier to write.
Simplest code is easier to understand.
Simplest code is easier to debug.
Simplest code may not be fast/efficient.
But simplest code may be sufficient!
Choose simpler code…
– When 100% sure that speed is not critical
•
Choose efficiency…
– when necessary
– when in doubt
Example: Slow Code is Ok
• Example: Direct deposit of paycheck.
– time: 1 hour (from time check is entered)
– result: customer just thinks check hasn’t
arrived or cleared yet
– solution: slower code just fine
Direct
deposit sure
is nice.
Example: Need Fast Code
• Example: Customer transfers money from
online account.
– time: takes 15 minutes
– result: customer panics
– solution: faster code
My money
is gone!
What Are Data?
• Definition: Data are any collection of objects.
• Examples:
–
–
–
–
–
Satellite measurements of sea-level rise.
Colors and sizes of Imelda Marcos’ shoes.
Numbers of bad hair days for each president.
Sets of Java classes.
Complete works of Shakespeare.
What Are structures?
• Definition: A structure is a particular
organization (or representation) of data.
data: your classes
• Examples:
–
–
–
–
Arrays
Lists
Hashes
Trees (branches with a data
“leaf” at the end of each branch)
structure: Nearby classrooms are close by on tree
What Are Operations?
• Definition: An operation is any function
that manipulates or uses the data.
•
•
•
•
•
•
alphabetize
count
insert
delete
sum
etc.
What’s a Data Structure?
• Definition: Any data representation (i.e.,
organization) and it’s associated
operations.
• Example: An array of integers, with
operations that (a) sort in order, (b) find the
nth largest, (c) insert and delete new
numbers.
Data Structure Example
operation
•
data
structure
Sort N numbers in an array.
–
Solution (Bubblesort):
1. Read into array.
2. Scan pairs from left to right, and swap if out of order.
8, 6, 2, 3
6, 8, 2, 3
6, 2, 8, 3
6, 2, 3, 8
3. Repeat scans until no more changes.
6, 2, 3, 8
–
2, 6, 3, 8
2, 3, 6, 8
2, 3, 6, 8
Time:
•
•
Seconds with 10 numbers.
Days with 1 million numbers.
(Just fine.)
(No good!)
Data Structure Comparison
• Heapsort and Bubblesort are both operations on data.
• But the heap uses a different structure – a tree instead of an
array.
Bubblesort
100 seconds
1,000,000 seconds
300 years
If it takes this long for bubblesort…
Heapsort (fancy, learn later)
1 second
3000 seconds
6 days
Then heapsort takes this long.
(So a heap data structure is far superior!)