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

Slides:



Advertisements
Similar presentations
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Advertisements

1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
ECE Application Programming
Searching and Sorting Arrays
ECE Application Programming
ECE Application Programming
ECE Application Programming
Introduction to Search Algorithms
ECE Application Programming
Chapter 9: Searching, Sorting, and Algorithm Analysis
Introduction to complexity
ECE Application Programming
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
ECE Application Programming
Function and class templates
Arrays … The Sequel Applications and Extensions
Algorithm design and Analysis
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
EECE.3170 Microprocessor Systems Design I
Standard Version of Starting Out with C++, 4th Edition
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Data Structures (CS212D) Week # 2: Arrays.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Data Structures and Algorithm: SEARCHING TECHNIQUES
EECE.2160 ECE Application Programming
Searching and Sorting Arrays
8. Comparison of Algorithms
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
EECE.2160 ECE Application Programming
Algorithmic complexity
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Heaps and priority queues
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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, -3.233}; 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

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

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

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

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