Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017

Similar presentations


Presentation on theme: "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017"— Presentation transcript:

1 EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Lecture 9: Abstract data types

2 Data Structures: Lecture 9
Lecture outline Announcements/reminders HW 1 due Friday, 2/10 Problem set dealing with algorithmic complexity Program 2 to be posted; due Wednesday, 2/15 Exam 1: Friday, 2/17 Will be allowed one double-sided 8.5” x 11” note sheet No electronic devices Today’s lecture Review Worst case analysis: linear search, binary search Worst case analysis: selection sort ADT intro 6/17/2019 Data Structures: Lecture 9

3 Review: Worst case analysis: linear search
Search entire array of n values for item; found = true and loc = item position if successful; otherwise, found = false Set found = false Set loc = 0 While loc < n and not found, do following: If item == a[loc] then Set found = true Else Increment loc by 1 6/17/2019 Data Structures: Lecture 9

4 Review: linear search (2)
Worst case: item is not in list Algorithm will go through all elements in array In all cases Lines 1 & 2 execute once In worst case Line 3 executes n+1 times Lines 4 & 6 execute n times Therefore, T(n) = 3n + 3 = O(n) 6/17/2019 Data Structures: Lecture 9

5 Review: Worst case analysis: binary search
Searching ordered array much more efficient Search array of n ascending values for item; found = true and loc = item position if successful; otherwise, found = false Set found = false Set first = 0 Set last = n - 1 While first ≤ last and not found, do following: Calculate loc = (first + last) / 2 If item < a[loc] then Set last = loc – 1 // Search first half Else if item > a[loc] then Set first = loc + 1 // Search second half Else Set found = true // Item found 6/17/2019 Data Structures: Lecture 9

6 Review: binary search (2)
Algorithm splits list into smaller sublist to be searched Loop control statement again limiting factor Each time, sublist size ≤ ½ previous sublist size Total number of loop iterations: 1 + (# iterations to produce sublist of size 1) If k = # iterations to produce sublist of size 1, n / 2k < 2  n < 2k * 2  n < 2k+1  log2n < k + 1 Therefore, in worst case (item is larger than everything in list) line 4 executed 2 + log2n times T(n) = O(log2n) 6/17/2019 Data Structures: Lecture 9

7 Worst case analysis: selection sort
Algorithm to sort array of n elements into ascending order On ith pass, first find smallest element in sublist x[i] … x[n-1], then place that value in position i For i = 0 to n – 2 do the following Set smallPos = i Set smallest = x[smallPos] For j = i+1 to n-1 do the following If x[j] < smallest then Set smallPos = j Set x[smallPos] = x[i] Set x[i] = smallest 6/17/2019 Data Structures: Lecture 9

8 Worst case analysis: selection sort (2)
Outer loop condition (1) executed n times Statements inside outer loop but not in inner loop (2, 3, 8, 9) executed n – 1 times Inner loop If i = 0, (4) executed n times, (5,6,7) n – 1 times If i = 1, (4) executed n-1 times, (5,6,7) n – 2 times … i = n-2, (4) executed 2 times, (5,6,7) 1 time In total (4) executed n(n+1)/2 – 1 times (5,6,7) executed n(n+1)/2 – 2 times Therefore T(n) = n + 4(n-1) + n(n+1)/2 – 1 + 3(n(n-1)/2) = 2n2 + 4n – 5 = O(n2) 6/17/2019 Data Structures: Lecture 9

9 Abstract data types (ADTs)
Processing data requires Collection of data items Basic operations to be performed on those items Combination of the two: abstract data type (ADT) “Abstract” part: definition of type separated from implementation Look at storage of data without worrying about implementation Example: “store 10 values” Could use many different implementations Algorithms defined for basic operations Effectiveness of algorithm usually linked to underlying data structures 6/17/2019 Data Structures: Lecture 9

10 C-style data structures
Can be more efficient than C++ implementation Example: array vs. C++ vector May simplify implementation but add overhead in form of operations that aren’t used Key C-style structures Arrays (1-D or greater) Structures 6/17/2019 Data Structures: Lecture 9

11 Review: arrays & pointers
Arrays: groups of data with same type x[10] has 10 elements, x[0] through x[9] Can also define with initial values e.g. double list[] = {1.2, 0.75, }; Must be sure to access inside bounds Array name is a pointer Arrays are always passed by address to functions Should pass size of array as additional argument e.g. void f(int arr[], int n); 6/17/2019 Data Structures: Lecture 9

12 Data Structures: Lecture 9
Review: 2D arrays Declared similarly to 1D arrays Example (see below): int x[3][4]; Index elements similarly to 1-D arrays Initialize: int y[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; Typically used with nested for loops Can pass to functions—must specify # columns e.g. void f2(int arr[ ][4], int nRows); Col. 0 Col. 1 Col. 2 Col. 3 Row 0 x[0][0] x[0][1] x[0][2] x[0][3] Row 1 x[1][0] x[1][1] x[1][2] x[1][3] Row 2 x[2][0] x[2][1] x[2][2] x[2][3] 6/17/2019 Data Structures: Lecture 9

13 Data Structures: Lecture 9
Time ADT to represent time Data to be stored: hours, minutes, AM/PM, military Operations: set time, display time, advance time, compare times Will define ADT using C-style implementation Will re-define later using OOP implementation 6/17/2019 Data Structures: Lecture 9

14 Time structure, prototypes
struct Time { unsigned hour, minute; char AMorPM; // 'A' or 'P' unsigned milTime; // military time equivalent }; void set(Time &t, unsigned hours, unsigned minutes, char AMPM); void display(const Time &t, ostream &out); void advance(Time &t, unsigned hours, unsigned minutes); bool lessThan(const Time &t1, const Time &t2); 6/17/2019 Data Structures: Lecture 9

15 Data Structures: Lecture 9
Final notes Next time Introduction to classes Reminders: HW 1 due Friday, 2/10 Problem set dealing with algorithmic complexity Program 2 to be posted; due Wednesday, 2/15 Exam 1: Friday, 2/17 Will be allowed one double-sided 8.5” x 11” note sheet No electronic devices 6/17/2019 Data Structures: Lecture 9


Download ppt "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017"

Similar presentations


Ads by Google