CS110_09ccOPTIONALSortExamples

CS110_09ccOPTIONALSortExamples - EECS110: 9cc OPTIONAL...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 9cc OPTIONAL Selection Sort, QuikSort Examples Jack Tumblin jet@cs.northwestern.edu . 1 Employee Roster Example staff[0] staff[1] staff[2] staff[3] staff[4] . . . Name2, Name1 Cratchit, Bob Scrooge, Ebenezer Marner, Silas Fogg, Phineas Low, Sampson Searle, Aloysius Chuzlewit, Martin Salary 10.00 250.00 15.00 20.00 75.00 75.00 20.00 Dep. Borough . . . 7 Dingley Dell 1 Strand 1 Durden 2 Devonshire 4 Hampstead 3 Bishopsgate 5 Fenchurch . . . And so on for about 250 entries . . . Store it using an array of pointers-to-struct: workerT *pStaff[300]; // up to 300 entries . . . // add a new entry: pStaff[k] = (workerT *)malloc(1*sizeof(workerT)); strncpy(pStaff[k]->name,"Fogg, Phineas",80); pStaff[k]->salary = 20.00; pStaff[k]->dep = 2; . . . 2 Sort How can we make pStaff alphabetical? Many answers (It's an important CS problem!) (Others include Bubble Sort, Insertion sort, Qsort, Heapsort...) Simplest: `Selection sort' Algorithm Find item that should be first on the list, and Swap it with the actual first item on the list. Shorten the list to exclude the first item, Repeat until the list length is 1. 3 Selection Sort Example Lets' sort by 1st 2 letters of pStaff[k]->name field: Only those 2 letters are shown below. (Recall that each element of pStaff array is a pointer to a workerT structure). k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ne La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ba Ph Ca He Ra Nu Fo Implementation: Define 3 array indices for pStaff: A `begin list' index bgn and an `end list' index end A linear search result index called fnd 4 Selection Sort Example Step 1: Setup Set indices for the start and end of the list k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ne La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ba Ph Ca He Ra Nu Fo pStaff[bgn] bgn=0; end=19; fnd=bgn; for(k=bgn; k<=end; k++) /* linear search; alphabetic */ { diff = strncmp(pStaff[k]->name, pStaff[fnd]->name,2); if(diff<0) fnd = k; 5 } pStaff[end] Selection Sort Example Step 2: Search Use Linear Search to find the item that SHOULD be first on our list (e.g. alphabetically first) strcmp()does alphabetical comparisons... k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ne La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ba Ph Ca He Ra Nu Fo pStaff[bgn] bgn=0; end=19; fnd=bgn; for(k=bgn; k<=end; k++) // linear search; alphabetic { diff = strnicmp(pStaff[k]->name, pStaff[fnd]->name,2); if(diff<0) fnd = k; 6 } pStaff[end] Selection Sort Example Step 2: Search Use Linear Search to find the item that SHOULD be first on our list (e.g. alphabetically first) strcmp()does alphabetical comparisons... What's strnicmp()? () k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . String compare, work on Ca He Ra Nu Fo Ne La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ba Ph 1st `n' chars only, but char ignore upper/lowercase pStaff[bgn] pStaff[end] bgn=0; end=19; fnd=bgn; for(k=bgn; k<=end; k++) // linear search; alphabetic { diff = strnicmp(pStaff[k]->name, pStaff[fnd]->name,2); if(diff<0) fnd = k; 7 } Selection Sort Example Step 2: Search Use Linear Search to find the item that SHOULD be first on our list (e.g. alphabetically first) k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ne La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ba Ph Ca He Ra Nu Fo pStaff[bgn] bgn=0; end=19; fnd=bgn; for(k=bgn; k<=end; k++) // linear search; alphabetic { diff = strnicmp(pStaff[k]->name, pStaff[fnd]->name,2); if(diff<0) fnd = k; 8 } pStaff[fnd] pStaff[end] Selection Sort Example Step 3: Exchange Swap the 1st item on list with the item we found k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ne La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ba Ph Ca He Ra Nu Fo pStaff[bgn] pStaff[fnd] pStaff[end] workerT tmp; . . . // swap values tmp = pStaff[bgn]; pStaff[bgn] = pStaff[fnd]; pStaff[fnd] = tmp; . . . 9 Selection Sort Example Step 3: Exchange Swap the 1st item on list with the item we found k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ne Ph Ca He Ra Nu Fo pStaff[bgn] pStaff[fnd] pStaff[end] workerT tmp; . . . // swap values tmp = pStaff[bgn]; pStaff[bgn] = pStaff[fnd]; pStaff[fnd] = tmp; . . . 10 Selection Sort Example Step 4: Shorten the list by 1 item Move starting index to leave behind the item we just found, and... k=0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba La QuMo Lo Ce Pa Fe Ga Na Ho Fa Mi Ne Ph Ca He Ra Nu Fo pStaff[bgn] workerT tmp; . . . // swap values tmp = pStaff[bgn]; pStaff[bgn] = pStaff[fnd]; pStaff[fnd] = tmp; . . . pStaff[end] 11 Selection Sort Example Step 5: Repeat until list shrinks to 1 item. Sorted k=0 1 2 3 4 5 6 7 8 9 10 11 12 Unsorted 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fo Fe Ga Ho La Lo QuMo Mi Ne Ph Pa He Ra Nu Na pStaff[bgn] for(bgn=0; bgn<end; bgn++) { fnd = LinearSearch(bgn,end); swap(fnd,bgn); } pStaff[end] 12 Selection Sort Efficiency? For an N-item list, For each item, search entire list; hence ~NxN = N2 computing cycles: Quadratic Hmm. Not very good... Many other choices (often very complicated) Need a much better, quick sorting method? Don't want to write it yourself? Then... 13 Selection Sort Efficiency? For an N-item list, For each item, search entire list; hence ~NxN = N2 computing cycles: Quadratic Hmm. Not very good... Many other choices (often very complicated) Need a much better, quick sorting method? Use QuickSort! 14 QuickSort or qsort() (not in book) Part of C's stdlib.h libraries: needs #include <stdlib.h> Fast, `in-place' sorting of any array Very flexible, fairly easy to use YOU write the comparison function! 15 QuickSort or qsort() (not in book) #include <stdlib.h> // for qsort, rand, etc . . . int myTest(const int *pA, const int *pB); // prototype . . . int main() An array of items; { (anything: int, ptr, struct, int i, myArray[45]; pointer-to-structs, etc...) for(i=0; i<45; i++) myArray[i] = rand(); qsort(myArray, 45, sizeof(int), myTest); . . . } int myTest(const int *pA, const int *pB) // return value: <0 if A<B, 0 if A==B, >1 if A>B { return( pA[0] pB[0]); } 16 QuickSort or qsort() (not in book) #include <stdlib.h> // for qsort, rand, etc. . . . int myTest(const int *pA, const int *pB); // prototype . . . int main() Call qsort(): { (array address, # of items, item size, int i, myArray[45]; your compare function's name) for(i=0; i<45; i++) myArray[i] = rand(); qsort(myArray, 45, sizeof(int), myTest); . . . } int myTest(const int *pA, const int *pB) // return value: <0 if A<B, 0 if A==B, >1 if A>B { return( pA[0] pB[0]); } 17 QuickSort or qsort() (not in book) #include <stdlib.h> // for qsort, rand, etc. . . . int myTest(const int *pA, const int *pB); // prototype . . . int main() Call qsort(): { (array address, # of items, item size, int i, myArray[45]; your compare function's name) for(i=0; i<45; i++) myArray[i] = rand(); qsort(myArray, 45, sizeof(int), myTest); . . . } int myTest(const int *pA, const int *pB) // return value: <0 if A<B, 0 if A==B, >1 if A>B { return( pA[0] pB[0]); } 18 QuickSort or qsort() (not in book) #include <stdlib.h> // for qsort, rand, etc. . . . int myTest(const int *pA, const int *pB); // prototype . . . int main() Call qsort(): { (array address, # of items, item size, int i, myArray[45]; your compare function's name) for(i=0; i<45; i++) myArray[i] = rand(); qsort(myArray, 45, sizeof(int), myTest); . . . } int myTest(const int *pA, const int *pB) // return value: <0 if A<B, 0 if A==B, >1 if A>B { return( pA[0] pB[0]); } 19 QuickSort or qsort() (not in book) #include <stdlib.h> // for qsort, rand, etc. . . . int myTest(const int *pA, const int *pB); // prototype . . . int main() Call qsort(): { (array address, # of items, item size, int i, myArray[45]; your compare function's name) for(i=0; i<45; i++) myArray[i] = rand(); qsort(myArray, 45, sizeof(int), myTest); . . . } int myTest(const int *pA, const int *pB) // return value: <0 if A<B, 0 if A==B, >1 if A>B { return( pA[0] pB[0]); } 20 QuickSort or qsort() (not in book) #include <stdlib.h> . . . int myTest(const int *pA, const int *pB); // prototype . . . int main() Compare function must have: { 2 Formal input Parameters; BOTH int i, myArray[45]; const pointer-to-item for(i=0; i<45; i++) myArray[i] = rand(); qsort(myArray, 45, sizeof(int), myTest); . . . } int myTest(const int *pA, const int *pB) // return value: <0 if A<B, 0 if A==B, >1 if A>B { return( pA[0] pB[0]); } 21 QuickSort or qsort() (not in book) #include <stdlib.h> . . . int myTest(const int *pA, const int *pB); // prototype . . . int main() Compare function must have: { `int' return type, whose value is: int i, myArray[45]; <0 if (item A < item B) =0 if (item A == item B) for(i=0; i<45; i++) myArray[i] = rand(); >0 if (item A > item B) qsort(myArray, 45, sizeof(int), myTest); . . . } int myTest(const int *pA, const int *pB) // return value: <0 if A<B, 0 if A==B, >1 if A>B { return( pA[0] pB[0]); } 22 QuickSort or qsort() (not in book) CAUTIONS: `Array address' can be ANY memory-- including dynamically-allocated blocks. `Compare' function arguments are tricky: MUST use const keyword MUST Pass-by-reference args are pointers to items, not the items themselves. VERY confusing if items we sort are pointers 23 ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online