You are on page 1of 17

AST20105

Data Structures & Algorithms


Chapter 1 - Introductory
Data structure is...
● a particular way of organizing data in a computer so that it can be used
effectively;
● for example, we could arrange a list of data into an array (linear approach);

● data can be organized in linear or non-linear approach depending on the NATURE


of data.

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

C++ Cook Book+


of acquisition.

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.

Call numbers are generated


systematically (e.g. Library of Congress
(LC) classification system).

A bookshelf contains similar kinds of


book as books with common nature
will be put together.
9
Pros and Cons of Scenario 2
Pros: Cons:

10
So, which book arrangement we should adopt?
How books (or data) should be arranged depends on:

1. What is the nature of data? Is order important?


2. Are data related?
3. Are there any commonalities among data? If so, are such commonalities
important?
4. How data will be manipulated and its frequency?
5. How much are the costs of data manipulation (modifying, searching, copying,
etc.)?
6. Are special arrangements of data difficult to implement? Etc...

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:

+ Easy to implement + Time grows logarithmically (good!)


+ Work for both sorted and unsorted to the growth of the size of array.
array - Only works on sorted array. Time
- Will reach to the worst scenario taken in sorting should also be
(taking longest time) when the key is considered.
at the last element or the key does - Relatively more difficult to
not exist. implement.
- Time grows (theoretically) linearly to
the growth of the size of array.
16
Q&A

17

You might also like