Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Overview Pseudocode Algorithm Lists

3 Pseudocode SpecificationTop level design Detail level design Implementation Pseudocode

4 Program design language Design algorithms Structured English Code like syntax Design -> comments

5 Pseudocode What do you do in program? Declarations / assignments Loops For loops While loops If statements Else Switch Function calls Logic operations Blocks

6 Pseudocode Local Variables Variables as objects Attributes attr[x] References

7 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 … …

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

9 Pseudocode Function calls Parameters passed as pointers Change attributes

10 Pseudocode Logic operations And Or Not Short circuiting not x and y or z

11 Pseudocode Indentation defines blocks Unlike C, C++ or java! …

12 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]

13 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

14 Pseudocode

15 Algorithm What is an algorithm? Problem solving instructions Structured method Detailed, step by step plan Calculable

16 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

17 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

18 Lists Types of lists Arrays Ordered / unordered Linked lists Single Double Circular Skip lists

19 Lists Pointers A variable that contains an address to another variable Int*pPtr; Int i; pPtr&i; *pPtr = 3;

20 Lists Linked list Head Date Double linked list Tail Data Head Data Head Data Head Tail

21 Lists Operations on lists Insert Delete Search Min / max Successor Predecessor

22 Lists Insert(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

23 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

24 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)

25 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

26 Lists Implementation issues Pointers Memory leaks

27 Lists Can we speed up the search operation? Skip lists Local line Express lines 2334 3739 921973022 3492302

28 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

29 Lists Worse case performance Best case: evenly space nodes How many nodes?

30 Lists

31

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

33 Lists

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

35 Questions?


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

Similar presentations


Ads by Google