Professional Documents
Culture Documents
AST20105 Data Structures & Algorithms: Chapter 1 - Introductory
AST20105 Data Structures & Algorithms: Chapter 1 - Introductory
2
Kinds of data structures
3
An algorithm is...
● a set of instructions telling computers what to do in order to perform
certain tasks;
● involving manipulation of data such as searching, modifying, and sorting
(if necessary) of data.
4
Why DS and Algo are important?
● Computer Program = Data Structures + Algorithms
● Well use of these two elements improve efficiency of computer
programmes in terms of
○ Reducing time spent on data operation;
○ Reducing space used in data storage;
○ Programme become more robust to errors.
5
Importance of choosing an appropriate DS
● Data representation can be messy of improper choice of DS is picked.
● The whole point of data structures is to organize data so that when
you look for data, you can find and process it efficiently, much like
how a library organizes its books to make sure people can find
them quickly.
● Study the scenarios regarding how books should be placed.
6
Scenario 1
Books are arranged in a linear
structure based on their dates
Introduction to
Backpacking to
Calculus 101
Engneering
Aerospace
Books acquired later will be put
Finance
Europe
at the end of book shelves.
...
A bookshelf contains various
kinds of books regardless their
topic and category, etc.
7
Pros and Cons of Scenario 1
Pros: Cons:
8
Scenario 2
Each new book will be given a call
number and will be placed into its
corresponding bookshelf according to
its call number.
10
So, which book arrangement we should adopt?
How books (or data) should be arranged depends on:
11
Algorithms….
● Suppose you are asked to search for a particular number from 100
integers ranged from 1 to 200.
● These 100 integers are arranged in random order at the beginning.
● How would you find the particular number, say 47?
● Will you change your mind if you are asked to search for a number from 1
trillion integers?
12
Code Study
Refer to CH1-SampleCode1.cpp:
● In this .cpp file, it contains 2 ways, linear and binary, to look for a
particular number, k, from n numbers.
● Numbers, ranged from 1 to 2n, are stored randomly in a vector. k may or
may not exist in the vector.
● Try small n to observe the elapsed time of both methods.
● Then try 2n, 3n, ..., 10n, and other positive multiples of n to observe the
relationship between size of n and elapsed time for each method.
13
Algorithms...your choice?
So, what is your choice when searching a number k?
14
Now...what about...
1. What if the array is dynamic instead of static? New elements could be
added or existing element could be removed?
2. What if data are not numbers and sometimes even could not be
ordered?
3. What if some manipulations of data are a lot more frequent than the
other?
4. Can algorithms be improved?
5. We have kind of discussed time. How about space (memory
consumption)?
15
Linear Search VS Binary Search
Linear Search: Binary Search:
17