COM SCI 32 Lecture Notes - Lecture 12: Merge Sort, Quicksort, Merge Algorithm

282 views4 pages

Document Summary

Cs 32 - lec 12, 13 - sorting. Selection sort void selectionsort(int a[], int n) for (int i = 0; i < n; i++) { int minindex = i; //locate the smallest item in the array between the i and n-1 for (int j = i+1; j < n; j++) { if (a[j] < a[minindex]) minindex = j; swap(a[i], a[minindex]); } } Insertion sort - compatible with linked lists w/ previous pointers to go backwards. Best for small amounts of data void insertionsort(int a[], int n) //focus the first s=2 elements, then the first s=3 elements, etc for (int s = 2; s <= n; s++) { //make a copy of the last val in the current set this opens up a. //slot in the array for us to shift items! int sortme = a[ s - 1 ]; //shift the items in the focus region right until we find the proper.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents