Pseudocode Dr Andrew Wallace PhD BEng hons EurIng andrewwallacecsumuse Overview Pseudocode Algorithm Lists Pseudocode Specification Top level design Detail level design Implementation ID: 532514
Download Presentation The PPT/PDF document "Algorithms, Lists and" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Algorithms, Lists and Pseudocode
Dr. Andrew Wallace PhD
BEng
(hons)
EurIng
andrew.wallace@cs.umu.seSlide2
Overview
Pseudocode
Algorithm
ListsSlide3
Pseudocode
Specification
Top level design
Detail level design
Implementation
PseudocodeSlide4
PseudocodeProgram design language
Design algorithms
Structured English
Code like syntax
Design -> comments Slide5
Pseudocode
What do you do in program?
Declarations /
assignments
Loops
For loops
While loopsIf statements
ElseSwitchFunction callsLogic operations
BlocksSlide6
PseudocodeLocal Variables
Variables as objects
Attributes
a
ttr
[
x]
References
Slide7
PseudocodeLoops
Same as in C, C++ or java. Loop variable still defined after loop
For
f
or
i
0 to max
… …
Whilewhile i
< 0 to max
… …Slide8
PseudocodeIf statements
Same as in C, C++ and java
If
if a <= b then
…
…Slide9
PseudocodeFunction calls
Parameters passed as pointers
Change attributesSlide10
PseudocodeLogic operations
And
Or
Not
Short circuiting
not
x
and y or zSlide11
PseudocodeIndentation defines blocks
Unlike C, C++ or java!
…
…
…
…
……Slide12
PseudocodeExample
Selection sort (a)
n
length[a]
f
or 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]Slide13
PseudocodeExample
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 orderr
eturn the concatenated listsSlide14
Pseudocode
Example
Transitive closure(e, n)
f
or
i
1 to n
do for j 1 to n do if i
= j or (i, j)
e[g]
then
1
else
0
f
or k 1 to n
do for
i
1 to n
do for j 1 to n
do t
(k)
Return T
(n)
Slide15
Algorithm
What is an algorithm?
Problem solving instructions
Structured method
Detailed, step by step plan
CalculableSlide16
AlgorithmFiniteness
The
algorithm must
stop
Assertiveness
Each
step must be unique
InputMust have zero or more input
OutputMust have one or more output
Efficiency /
FeasibilityEach
step of the algorithm must be done in a finite timeSlide17
Lists
Abstract data structure
Data arranged in linier order
To do list:
Cancel papers
Let out the cat
Invade Poland
23
-34
-7
39
92Slide18
ListsTypes of lists
Arrays
Ordered / unordered
Linked lists
Single
Double
Circular
Skip listsSlide19
ListsPointers
A variable that contains an address to another
variable
Int
*
pPtr
;
Int
i;pPtr
&i
;
*pPtr
= 3;Slide20
ListsLinked list
Head
Date
Double linked list
Tail
Data
Head
Data
Head
Data
Head
Tail
Tail
TailSlide21
ListsOperations on lists
Insert
Delete
Search
Min / max
Successor
PredecessorSlide22
ListsInsert(l, x)
x.next
=
l.head
if
l.head ≠ NULL
l.head.prev = x l.head
= x
x.head = NULL
O(1)
x
prev
next
l1
prev
next
l2
prev
next
l.head
l.headSlide23
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
O(1)
Q
(n)
(specific key)
x
prev
next
l1
prev
next
l3
prev
next
l.headSlide24
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)Slide25
ListsSentinel
Null dummy object (same attributes)
Removed need to check boundary conditions
Delete(l
, x)
x.prev.next
= x.next
x.next.prev = x.prevSlide26
ListsImplementation issues
Pointers
Memory leaksSlide27
ListsCan we speed up the search operation?
Skip lists
Local line
Express lines
23
34
3
7
39
92
197
302
2
34
92
302Slide28
ListsWhat 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 itemSlide29
ListsWorse case performance
Best case: evenly space nodes
How many nodes?Slide30
Lists
= top (express list) and
= lower list (full list)
Search cost : approximately =
= 0
=
=
n
=
Slide31
Lists
Search time :
=
n
=
+
= 2
Slide32
Lists
Even faster skip list
Add more express lanes
l
og
n
23
34
37
39
92
197
302
2
34
92
302
3
7
Tree?Slide33
Lists
2 sorted lists =
2
3 sorted lists = 3
k
sorted lists =
sorted lists =
= 2
Slide34
ListsOperations
Insert
Delete
Linked list operations
Problem?
Chain lengths?
Promoting to the next level
CountingRandom 50% probabilitySlide35
Questions?