Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM212 Data Structures Intro to Abstract Data Structures 4 Weeks ~7 Homework Problems Implementation of Abstract Data Structures 7 Weeks ~7 Programming.

Similar presentations


Presentation on theme: "COM212 Data Structures Intro to Abstract Data Structures 4 Weeks ~7 Homework Problems Implementation of Abstract Data Structures 7 Weeks ~7 Programming."— Presentation transcript:

1 COM212 Data Structures Intro to Abstract Data Structures 4 Weeks ~7 Homework Problems Implementation of Abstract Data Structures 7 Weeks ~7 Programming Assignments Test Advanced Java Programming 3 Weeks Project

2 Today Complexity Abstract Data Structures Lists Recursion

3 Complexity How long functions take to run –Can be counted in number of instructions executed Growth rate of functions –How much longer they take to run as size of the input increases

4 Example Function One example(n) print x print y x = y 2 What is the run time?

5 Example Function One example(n) print x print y x = y 2 The run time is 3.

6 Example Function Two example(n) loop n times y = x + 1 x = y 2 print x What is the run time?

7 Example Function Two example(n) loop n times y = x + 1 x = y 2 print x The run time is 4n

8 Example Function Three example(n) print x print y loop n times y = x + 1 x = y 2 print x What is the run time?

9 Example Function Three example(n) print x print y loop n times y = x + 1 x = y 2 print x The run time is 4n + 2

10 Complexity Notation We need some notation to help us categorize functions by how long they take to run. Since the second two example functions will take about the same time to run, they should be in the same category. Even functions with run times of 2n and 3n take similar time to run. –They are much more dependent on n than a function that takes constant time to run.

11 Example A large n affects ex1() but not ex2() ex1(n) loop n times print x ex2(n) print x

12 Big O f(n) ≤ cg(n) for all n ≥ n 0

13 Example f(n) = 4n + 2 using g(n) = n and c = 5 and n 0 = 2 f(n) ≤ cg(n) for all n ≥ n 0 is 4n + 2 ≤ 5n when n ≥ 2 So g(n) is n. The complexity of f(n) is O(n)

14 Other Examples O(n) O(1) 4n+8 16 ¼(n-8)296 200n+568011000302 O(n 2 ) 4n 2 +18

15 Others 56n + 7 0.34n – 455 0.003n 2 + 3 14 5(lg n) + 5 98n 4 + 2

16 Data Structures Data Types A way of representing or organizing data Examples integer 5 float6.543 character a array

17 Array 91034536 Contiguous section of memory Fixed size Containing all one data type

18 Abstract Data Structures Independent of implementation User does not care about the implementation Set of operations to access / manipulate data Examples: FIFO queue Return the largest from a set List

19 Abstract Data Structure The user has a need for a specific DS, but the actual implementation is not of their concern. The actual implementation is hidden from the user. The programmer needs to ensure that the means of implementation allows the user to do everything that is required.

20 List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1 <= the key of x i for all i where 0 < i < n. The sort symbol = or any other function that determines ordering in the keys. An unordered list does not have this restriction. Functions: access(L, i) returns x i length(L) returns n concat(L 1, L 2 ) returns a new list with L 2 concatenated on to L 1 createEmptyList() returns a newly created empty list isEmptyList(L) returns true if L is empty and false if it is not searchFor(L, key) returns i where key of x i = key remove(L, i) returns a list with x i removed; the old x i+1 is now x i, etc. inserti(L, i, x) returns a list with x inserted as x i ; the old x i is now x i+1, etc. insert(L, x) returns a list with x added to L sort(L) returns the list in sorted order Lists

21 A Node NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95

22 Problem We need a list that has a maximum of 100 nodes. How can you implement this kind of list?

23 Homework 0 Describe how to use an array to implement an unordered list (assume a max size of 100 elements). Determine how to do the following functions: access, length, concat, createEmptyList, isEmptyList, searchFor, remove, and insert. How would any of these functions change if the list was to be ordered?

24 Recursion


Download ppt "COM212 Data Structures Intro to Abstract Data Structures 4 Weeks ~7 Homework Problems Implementation of Abstract Data Structures 7 Weeks ~7 Programming."

Similar presentations


Ads by Google