announcements
play

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];


  1. 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]; 4 int j = loc; 5 while (j > 0 && candy[j-1] > temp) { 6 candy[j] = candy[j-1]; 7 j--; } 8 candy[j] = temp; 9 } Good name? Does it work? Running time:

  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:

  3. Insertion Sort vector<string> insertionSort (vector<string> & candy){ 1 for (int i = 1; i < candy.size(); i++) { 2 ____________(candy, i); 3 return candy; } 4 3) Base case: 4) IH: 5) Inductive step: 6) Termination:

  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

  5. Something NEW!! Make at least 3 observations about this code: template <class LIT> 1 struct Node{ 2 LIT data; 3 Node * next; 4 Node(LIT nData, Node * next=NULL): data(nData) {} 5 }; 6 • • • •

  6. Switching gears… for line 4:

  7. Line 4 (pointers): Backtrack to variables: loc name val type Special variable type:

  8. Pointers and dynamic memory: loc name val type loc name val type int * p;

Recommend


More recommend