Vous êtes sur la page 1sur 35

Algorithms, Lists and

Pseudocode
Dr. Andrew Wallace PhD BEng(hons) EurIng
andrew.wallace@cs.umu.se
Overview
• Pseudocode
• Algorithm
• Lists
Pseudocode

Specification Top level design

Detail level Implementation


Pseudocode
design
Pseudocode
• Program design language
• Design algorithms
• Structured English
• Code like syntax
• Design -> comments
Pseudocode
• What do you do in program?
• Declarations / assignments
• Loops
• For loops
• While loops
• If statements
• Else
• Switch
• Function calls
• Logic operations
• Blocks
Pseudocode
• Local Variables
• Variables as objects
• Attributes
• attr[x]
𝑥 𝑦
• References
Pseudocode
• Loops
• Same as in C, C++ or java. Loop variable still defined after loop
• For
for i 0 to max


• While
while i < 0 to max


Pseudocode
• If statements
• Same as in C, C++ and java
• If
if a <= b then


Pseudocode
• Function calls
• Parameters passed as pointers
• Change attributes
Pseudocode
• Logic operations
• And
• Or
• Not
• Short circuiting

not x and y or z
Pseudocode
• Indentation defines blocks
• Unlike C, C++ or java!







Pseudocode
• Example
Selection sort (a)
n length[a]
for j 1 to n-1
do smallest j
for i j + 1 to n
do if a[i] < a[smallest]
then smallest i
exchange a[i] a[smallest]
Pseudocode
• Example
Bucket sort(a, n)
for i 1 to n
do insert a[i] into list b[na[i]]
for i 0 to n – 1
do sort list b[i] with insert sort
concatenate lists b[0], b[1], … b[n-1] together in order
return the concatenated lists
Pseudocode
• Example
Transitive closure(e, n)
for i 1 to n
do for j 1 to n
do if i = j or (i, j) ∈ e[g]
(𝟎)
then 𝒕𝒊,𝒋 1
(𝟎)
else 𝒕𝒊,𝒋 0
for k 1 to n
do for i 1 to n
do for j 1 to n
do t(k)
Return T(n)
Algorithm
• What is an algorithm?
• Problem solving instructions
• Structured method
• Detailed, step by step plan
• Calculable
Algorithm
• Finiteness
• The algorithm must stop
• Assertiveness
• Each step must be unique
• Input
• Must have zero or more input
• Output
• Must have one or more output
• Efficiency / Feasibility
• Each step of the algorithm must be done in a finite time
Lists
To do list:
• Abstract data structure 1. Cancel papers
• Data arranged in linier order 2. Let out the cat

3. Invade Poland

23 -34 -7 39 92
Lists
• Types of lists
• Arrays
• Ordered / unordered
• Linked lists
• Single
• Double
• Circular
• Skip lists
Lists
• Pointers
• A variable that contains an address to another variable
Int* pPtr;
Int i;

pPtr &i;

*pPtr = 3;
Lists
• Linked list
• Head
• Date

• Double linked list


• Tail

Head Head Head

Data Data Data

Tail Tail Tail


Lists
• Operations on lists
• Insert
• Delete
• Search
• Min / max
• Successor
• Predecessor
Lists
l.head prev

Insert(l, x)
x
x.next = l.head
next
if l.head ≠ NULL
l.head.prev = x
l.head = x
x.head = NULL prev prev

l.head l1 l2
O(1)
next next
Lists
Delete(l, x)
if x.prev ≠ NULL
x.prev.next = x.next
else l.head = x.head
if x.next ≠ NULL
x.next.prev = x.prev
l.head prev prev prev
O(1)
l1 x l3
Q(n) (specific key)
next next next
Lists
Search(l, k)
x = l.head
while x.next ≠ NULL and x.key ≠ k
x = x.next
if x.key ≠ k
x = NULL
return x

O(n)
Lists
• Sentinel
• Null dummy object (same attributes)
• Removed need to check boundary conditions

Delete(l, x)
x.prev.next = x.next
x.next.prev = x.prev
Lists
• Implementation issues
• Pointers
• Memory leaks
Lists
• Can we speed up the search operation?
• Skip lists
• Local line
• Express lines

34 92 302

2 23 34 37 39 92 197 302
Lists
• What data items to store in the express lane?
• Too many and we don’t save much on time!
• Too few and we don’t save much on time!
• Most used
• Dynamic skip list
• Uniformly
• Equal spacing between each express lane item
Lists
• Worse case performance
• Best case: evenly space nodes
• How many nodes?
Lists
• 𝑳𝟏 = top (express list) and 𝑳𝟐 = lower list (full list)
|𝑳𝟐 |
• Search cost : approximately = |𝑳𝟏 | + =0
|𝑳𝟏| |
• |𝑳𝟏 |𝟐 = |𝑳𝟐 | = n
• |𝑳𝟏 | = 𝒏
Lists
• Search time :
• |𝑳𝟐 | = n
• |𝑳𝟏 | = 𝒏
|𝑳𝟐 |
• |𝑳𝟏 | +
|𝑳𝟏| |
𝒏
• 𝒏+ =2 𝒏
𝒏
Lists
Tree?
• Even faster skip list
• Add more express lanes
• log n
37

34 92 302

2 23 34 37 39 92 197 302
Lists
• 2 sorted lists = 2 𝒏
𝟑
• 3 sorted lists = 3 𝒏
• k sorted lists = 𝒌 𝒌 𝒏
𝒍𝒐𝒈𝟐 𝒏
• 𝒍𝒐𝒈𝟐 𝒏sorted lists = 𝒍𝒐𝒈𝟐 𝒏 𝒏 = 2𝒍𝒐𝒈𝟐 𝒏
Lists
• Operations
• Insert
• Delete
• Linked list operations
• Problem?
• Chain lengths?
• Promoting to the next level
• Counting
• Random 50% probability
Questions?

Vous aimerez peut-être aussi