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