Announcements: HW1 due today 11:59p. PA1 due 02/03, 11:59p. Quizzes - - PowerPoint PPT Presentation

announcements
SMART_READER_LITE
LIVE PREVIEW

Announcements: HW1 due today 11:59p. PA1 due 02/03, 11:59p. Quizzes - - PowerPoint PPT Presentation

Announcements: HW1 due today 11:59p. PA1 due 02/03, 11:59p. Quizzes Warm-up: Weird Mystery Function 1 void ___________ (vector<string> & candy, int loc){ 2 // assumes candy[0:loc-1] is sorted, loc valid 3 string temp = candy[loc];


slide-1
SLIDE 1

Announcements:

HW1 due today 11:59p. PA1 due 02/03, 11:59p. Quizzes

Warm-up: Weird Mystery Function

Good name? Does it work? Running time:

void ___________ (vector<string> & candy, int loc){ // assumes candy[0:loc-1] is sorted, loc valid string temp = candy[loc]; int j = loc; while (j > 0 && candy[j-1] > temp) { candy[j] = candy[j-1]; j--; } candy[j] = temp; } 1 2 3 4 5 6 7 8 9

slide-2
SLIDE 2

You write Insertion Sort…

void insertionSort (vector<string> & candy){ } 1 2 3 4 5 6

Functionality? Running Time? Correctness? 1) Iterative variable: 2) Loop invariant:

slide-3
SLIDE 3

Insertion Sort

vector<string> insertionSort (vector<string> & candy){ for (int i = 1; i < candy.size(); i++) { ____________(candy, i); return candy; } 1 2 3 4

3) Base case: 4) IH: 5) Inductive step: 6) Termination:

slide-4
SLIDE 4

Linear Sorts, recap

We have learned and analyzed selection sort and insertion sort. Which is better?

  • Asymptotically?
  • Empirically?
  • What if list is already sorted?
  • What if list is almost sorted?
  • What if list is in reverse order?

https://www.toptal.com/developers/sorting-algorithms

slide-5
SLIDE 5

Something NEW!!

Make at least 3 observations about this code:

  • 1

2 3 4 5 6 template <class LIT> struct Node{ LIT data; Node * next; Node(LIT nData, Node * next=NULL): data(nData) {} };

slide-6
SLIDE 6

Switching gears… for line 4:

slide-7
SLIDE 7

Line 4 (pointers):

Backtrack to variables: Special variable type:

loc name val type

slide-8
SLIDE 8

Pointers and dynamic memory:

loc name val type loc name val type

int * p;