# Algorithms, Lists and Pseudocode Dr. Andrew Wallace PhD BEng(hons) EurIng

## Presentation on theme: "Algorithms, Lists and Pseudocode Dr. Andrew Wallace PhD BEng(hons) EurIng"— Presentation transcript:

Algorithms, Lists and Pseudocode Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Overview Pseudocode Algorithm Lists

Pseudocode SpecificationTop level design Detail level design Implementation 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

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 Abstract data structure Data arranged in linier order To do list: 1.Cancel papers 2.Let out the cat 3.Invade Poland 23-34 -739 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 Operations on lists Insert Delete Search Min / max Successor Predecessor

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)  (n)(specific key) x prev next l1 prev next l3 prev next l.head

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 2334 3739 921973022 3492302

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

Even faster skip list Add more express lanes log n 2334 3739 921973022 3492302 37 Tree?

Lists

Operations Insert Delete Linked list operations Problem? Chain lengths? Promoting to the next level Counting Random 50% probability

Questions?

Download ppt "Algorithms, Lists and Pseudocode Dr. Andrew Wallace PhD BEng(hons) EurIng"

Similar presentations