You are on page 1of 36

Data Structure & Algorithm

Lecture 2 – Basic Data Structure

JJCAO

Steal some from Prof. Yoram Moses


The Sorting Problem
• Example:

• Input: A sequence of n numbers < 𝑎1 , 𝑎2 , … , 𝑎n >

• Output: A permutation (reordering) <


𝑏1 , 𝑏2 , … , 𝑏n > of the input sequence such that
𝑏1 ≤ 𝑏2 … ≤ 𝑏n
2
Recitation
Selection Sort 𝑂(𝑛2 ) Insertion Sort 𝑂(𝑛2 )
k=i key = A[j]

for i = 1:n,
k = i
//invariant: a[k] smallest of a[i..n]
for j = i+1:n
if a[j] < a[k] then k = j
//invariant: a[1..i] in final
position
swap a[i,k]
end

3
How is Data Represented?
• Sort a sequences: Array? List? Heap?

– There are many options


– Efficiency of the algorithm also depends on
the data structure used

4
Elementary Data Structures
• Arrays
• Lists
• Stacks
• Queues
• Trees

“Mankind’s progress is measured by the number of


things we can do without thinking.”

In some languages or libs, some of them are the “off-the-shelf”


components. 5
Components of Data Structure
There are two aspects to any data structure:
1. The abstract operations which it supports. (Abstract
Data Types, ADT)
2. The implementation of these operations. (Data
Structure)

6
Data Type vs. Data Structure
• Data Structure is the way an ADT is
implemented.
• Must be distinguished from the ADT
itself!

7
C++ Implementation of Stack

8
Data Structure vs. Algorithm
• Data Structures
– Represent objects of the ADT

• Algorithms
– Manipulate the data structures to
implement a mission using the operations of
the ADT

9
Contiguous vs. Linked Data
Structures
Data structures can be neatly classified as either
contiguous or linked depending upon whether they
are based on arrays or pointers:

• Contiguously-allocated structures are composed


of single slabs of memory, and include arrays,
matrices, heaps, and hash tables.
• Linked data structures are composed of
multiple distinct chunks of memory bound
together by pointers, and include lists, trees,
and graph adjacency lists.

10
Array
Array of integer

Array of images

An array is a number of data items of the same type arranged


contiguously in memory.
Fixed-size
0 1 2 m 1

m _ pData a0 a1 a2 am  2 am 1
𝑚_𝑛𝑆𝑖𝑧𝑒
数据区
Advantages of contiguously-
allocated arrays
1. Constant-time access given the index.

2. Space efficiency - Arrays consist purely


of data, so no space is wasted with links or
other formatting information.

3. Memory locality - Physical continuity


(memory locality) between successive data
accesses helps exploit the high-speed
cache memory on modern computer
architectures.
12
Dynamic Array
• Unfortunately we cannot adjust the size of
simple arrays in the middle of a program’s
execution.
0 1 2 m 1 n -1

m _ pData a0 a1 a2 am  2 am 1
m _ nMax  n
m _ nSize  m 数据区 备用区

• Start with an array of size 1, and double its


size from m to 2m each time we run out of
space. How many times will we double for n
elements?
• Only log(𝑛)
How much total work
• The apparent waste in this procedure involves the
recopying of the old contents on each expansion.
• If half the elements move once, a quarter of the
elements twice, and so on, the total number of
movements M is given by

• Thus each of the n elements move an average of only


twice, and the total work of managing the dynamic
array is the same O(n) as a simple array.

14
Remove an Element

15
Notes
• Practical implementations usually include
more operations
• Initialization/Destruction
• “Luxury” operations:
– size()
– print()
– operators

16
Bubble Sort
Bubble sort: beginning of
first pass:

1. Compare two players.


2. If the one on the left is
taller, swap them.
3. Move one position right.

End of first pass 17


Bubble Sort
for i = 1:n,
swapped = false
for j = n:i+1,
if a[j] < a[j-1],
swap a[j,j-1]
swapped = true
break if not swapped
Is there any problem?
end

18
Several Sort Algorithms

http://www.sorting-algorithms.com 19
The Linked List Structures
typedef struct list {
item type item;
struct list *next;
} list;

Pointers represent the address of a location in memory.


A cell-phone number can be thought of as a pointer to its owner
as they move about the planet.
20
Searching a List
Searching in a linked list can be done iteratively
or recursively.

list *search_list(list *l, item type x)


{
if (l == NULL) return(NULL);
if (l->item == x)
return(l);
else
return( search_list(l->next, x) );
}
21
Insertion into a List
Since we have no need to maintain the list in any particular
order, we might as well insert each new item at the head.

void insert_list(list **l, item_type x)


{
list *p = new list;
p->item = x;
p->next = *l;
*l = p;
}

Note the **l, since the head element of the list changes.

22
Deleting from a List

23
Deleting from a List

24
Advantages of Linked Lists
1. Overflow on linked structures can never
occur unless the memory is actually full.

2. Insertions and deletions are simpler than


for contiguous (array) lists.

3. With large records, moving pointers is


easier and faster than moving the items
themselves.

25
Various List

26
Stack
• last-in, first-out (LIFO)

27
Stack ADT & std::vector

Stack ADT std::vector comments


create() constructor of vector creates empty stack
bool isEmpty() empty() tells whether the stack s is empty
push(Item e) push_back(Item e) put e on top of the stack s
Item peek() Item back() returns topmost element in stack s
pop() pop_back() removes topmost element from the
stack s
destroy() Deconstructor of vector destroys stack s
28
Stack Implementation Using a
Linked List

29
Stack Example 1: Reversing a
Word
• part
• trap

30
Stack Example 2: Delimiter
Matching
• When compilers compile your code
• When you want to write a program to
parse a math formula

31
Stack Example 2: Delimiter
Matching
• A successful example: b(c[d]e)
Character Read Stack Contents
b
( (
c (
[ [(
d [(
] (
e (
)
32
Queue
• first-in, first-out

33
Implementation of Queue

34
A Circular Queue

35
Homework 1
• Basic Dynamic Array & Selection Sort
• Deadline: 22:00, Sep. 10, 2011

36

You might also like