problem solving with algorithms and data structure - lists

15
PROBLEM SOLVING WITH ALGORITHMS AND DATA STRUCTURES — LISTS Bruce Tsai http://interactivepython.org/runestone/static/pythonds/BasicDS/ Lists.html

Upload: yi-lung-tsai

Post on 12-Jul-2015

423 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Problem Solving with Algorithms and Data Structure - Lists

PROBLEM SOLVING WITH ALGORITHMS AND DATA STRUCTURES — LISTSBruce Tsai

http://interactivepython.org/runestone/static/pythonds/BasicDS/Lists.html

Page 2: Problem Solving with Algorithms and Data Structure - Lists

LIST

Unordered list

Collection of items where each item holds a relative position with respect to the others

[54, 26, 93, 17, 77, 31]

2

Page 3: Problem Solving with Algorithms and Data Structure - Lists

UNORDERED LIST COMPLEXITY

add(item) — O(1)

remove(item) — O(n)

search(item) — O(n)

isEmpty() — O(1)

size() — O(n)

append(item) — O(n)

index(item) — O(n)

insert(pos,item) — O(pos)

pop() — O(n)

pop(pos) — O(pos)

3

Page 4: Problem Solving with Algorithms and Data Structure - Lists

ORDERED LIST

Collection of items where each item holds a relative position that is based upon some underlying characteristic of the item

Ascending or descending

[54, 26, 93, 17, 77, 31] => [17, 26, 31, 54, 77, 93]

4

Page 5: Problem Solving with Algorithms and Data Structure - Lists

ORDERED LIST COMPLEXITY

add(item) — O(n)

remove(item) — O(n)

search(item) — O(n)

isEmpty() — O(1)

size() — O(n)

index(item) — O(n)

pop() — O(n)

pop(pos) — O(pos)

5

Page 6: Problem Solving with Algorithms and Data Structure - Lists

SELF CHECK

1. Implement the append method for UnorderedList. What is the time complexity of the method you created?

2. In the previous problem, you most likely created an append method that was O(n). If you add an instance variable to the UnorderedList class you can create an append method that is O(1). Modify your append method to be O(1).

6

Page 7: Problem Solving with Algorithms and Data Structure - Lists

PART 1

Append method adds a new item to the end of the list making it the last item in the collection.

First traverse UnorderedList to original last item and then set new item as next node of original last item

The time complexity is O(n)

7

Page 8: Problem Solving with Algorithms and Data Structure - Lists

PART 2

Add instance variable tail to represent the last item

https://gist.github.com/wagamama/18a75738f7dd3dcdf817#file-unorderedlist-py

8

Page 9: Problem Solving with Algorithms and Data Structure - Lists

DISCUSSION QUESTIONS

7. What is the result of carrying out both steps of the linked list add method in reverse order? What kind of reference results? What types of problems may result?

8. Explain how the linked list remove methods works when the item to be removed is in the last node.

9. Explain how the remove method works when the item is in the only node in the linked list.

9

Page 10: Problem Solving with Algorithms and Data Structure - Lists

NO. 7

Page 11: Problem Solving with Algorithms and Data Structure - Lists

NO. 8

previous current

Page 12: Problem Solving with Algorithms and Data Structure - Lists

NO. 9

previous == None

self.head = current.getNext()

self.head = None

12

Page 13: Problem Solving with Algorithms and Data Structure - Lists

ARRAY (SEQUENCE)

Allocate size first

Item with index

assign — O(1)

insert — O(n)

delete — O(n)

Page 14: Problem Solving with Algorithms and Data Structure - Lists

PYTHON LIST COMPLEXITY

append — O(1)

insert — O(n)

delete — O(n)

search — O(n)

size — O(1)

get — O(1)

set — O(1)

14

Page 15: Problem Solving with Algorithms and Data Structure - Lists

REFERENCE

http://interactivepython.org/runestone/static/pythonds/BasicDS/Lists.html

https://gist.github.com/wagamama/18a75738f7dd3dcdf817

http://www.csie.ntnu.edu.tw/~u91029/Sequence.html

https://wiki.python.org/moin/TimeComplexity