Announcements: PA1 available, due 1/28, 11:59p. Today’s plan: Selection Sort Weird function Insertion Sort
Selection Sort 1 vector<string> __________ (vector<string> & candy){ 2 for (int i = 0; i < candy.size(); i++){ 3 int min = findMin(candy,i); 4 string temp = candy[i]; 5 candy[i] = candy[min]; 6 candy[min] = temp; 7 } 8 return candy;} Functionality? Running Time? What if list is already sorted? Correctness? loop invariant –
Selection Sort: (proof continued) vector<string> __________ (vector<string> & candy){ 1 for (int i = 0; i < candy.size(); i++){ 2 int min = findMin(candy,i); 3 string temp = candy[i]; 4 candy[i] = candy[min]; 5 candy[min] = temp; 6 } 7 return candy;} 8
Weird mystery function vector<string> ________ (vector<string> & candy, int loc){ 1 // assumes candy[0:loc-1] is sorted, loc > 0 2 string temp = candy[loc]; 3 int j = loc; 4 while (candy[j-1] > temp) { 5 candy[j] = candy[j-1]; 6 j--; } 7 candy[j] = temp; 8 return candy; } 9 Functionality? Running Time? Correctness? loop invariant -
Weird mystery function vector<string> ________ (vector<string> & candy, int loc){ 1 // assumes candy[0:loc-1] is sorted, loc > 0 2 string temp = candy[loc]; 3 int j = loc; 4 while (candy[j-1] > temp) { 5 candy[j] = candy[j-1]; 6 j--; } 7 candy[j] = temp; 8 return candy; } 9
Insertion Sort 1 vector<string> ________ (vector<string> & candy){ 2 for (int i = 1; i < candy.size(); i++) { 3 string temp = candy[i]; 4 int j = i; 5 while (candy[j-1] > temp) { 6 candy[j] = candy[j-1]; 7 j--; } 8 candy[j] = temp; } 9 return candy; } Functionality? Running Time? What happens if list is already sorted? Correctness? loop invariant -
Recommend
More recommend