Searching & Sorting. Contents. I. Searching 6
|
|
|
- Ginger Pitts
- 9 years ago
- Views:
Transcription
1 Searching & Sorting Dr. Chris Bourke Department of Computer Science & Engineering University of Nebraska Lincoln Lincoln, NE 68588, USA /04/29 12:14:51 These are lecture notes used in CSCE 155 and CSCE 156 (Computer Science I & II) at the University of Nebraska Lincoln. Contents 1. Overview CSCE 155E Outline CSCE 156 Outline I. Searching 6 2. Introduction 6 3. Linear Search Pseudocode Example Analysis Binary Search Pseudocode: Recursive Pseudocode: Iterative Example Analysis
2 5. Searching in Java Linear Search Binary Search Preventing Arithmetic Errors Searching in Java: Doing It Right Examples Searching in C Linear Search Binary Search Searching in C: Doing it Right Linear Search Binary Search Examples II. Sorting Bubble Sort Basic Idea Pseudocode Analysis Code Samples Selection Sort Basic Idea Pseudocode Analysis Code Samples Insertion Sort Basic Idea Pseudocode Analysis Code Samples Quick Sort Basic Idea Pseudocode Analysis Code Samples Merge Sort Basic Idea Pseudocode Analysis
3 11.4. Code Samples Heap Sort Tim Sort Summary In Practice: Sorting in Java Considerations Examples In Practice: Sorting in C Sorting Pointers to Elements Examples A. Java: equals and hashcode methods 38 A.1. Example B. Java: The Comparator Interface 40 B.1. Examples B.2. Exercises C. C: Function Pointers 42 C.1. Full Example D. C: Comparator Functions 44 D.1. Examples E. Master Theorem 46 References 46 List of Algorithms 1. Linear Search Binary Search Recursive Binary Search Iterative Bubble Sort Selection Sort Insertion Sort Quick Sort In-Place Partition
4 9. Merge Sort Code Samples 1. Java Linear Search for integers Java Recursive Binary Search for integers Java Iterative Binary Search for integers Java Search Examples C Linear Search for integers C Recursive Binary Search for integers C Iterative Binary Search for integers C Search Examples Java Bubble Sort for integers C Bubble Sort for integers Java Selection Sort for integers C Selection Sort for integers Java Insertion Sort for integers C Insertion Sort for integers Java Quick Sort for integers Java Partition subroutine for integers C Quick Sort for integers Java Merge Sort for integers Java Merge for integers C Merge Sort for integers C Merge Sort for integers Handling Null Values in Java Comparators Using Java Collection s Sort Method C Comparator Function for Strings Sorting Structures via Pointers Handling Null Values Using C s qsort function Java Student class Java Comparator Examples for the Student class C Function Pointer Examples C Structure Representing a Student C Comparator Function Examples for Student structs List of Figures 1. Illustrative example of the benefit of ordered (indexed) elements, Windows Unsorted Array
5 1. Overview 1.1. CSCE 155E Outline 1. Introduction 2. Linear Search (basic idea, example, code, brief analysis) 3. Binary Search (basic idea, example, code, brief analysis, comparative analysis) 4. Bubble Sort (Basic idea, example, code, brief analysis) 5. Selection Sort (Basic idea, example, code, brief analysis) 6. Quick Sort (Basic idea, example, comparative analysis only) 7. Function pointers 8. Sorting & Searching in C 9. Honors: Comparators, Searching, Sorting in Java 1.2. CSCE 156 Outline 1. Introduction 2. Linear Search (basic idea, pseudocode, full analysis) 3. Binary Search (basic idea, pseudocode, full analysis, master theorem application, comparative analysis) 4. Bubble Sort (Basic idea, example, pseudocode, full analysis) 5. Selection Sort (Basic idea, example, pseudocode, full analysis) 6. Insertion Sort (Basic idea, example, pseudocode, full analysis) 7. Quick Sort (Basic idea, example, pseudocode, full analysis) 8. Merge Sort (Basic idea, example, pseudocode, full analysis) 9. Sorting Stability 10. Comparators in Java 11. Equals & Hash Code in Java 12. Searching in Java 13. Sorting in Java 5
6 Part I. Searching 2. Introduction Problem 1 (Searching). Given: a collection of elements, A = {a 1, a 2,..., a n } and a key element e k Output: The element a i in A that matches e k Variations: Find the first such element Find the last such element Find the index of the element Key versus equality based Find all such elements Find extremal element(s) How to handle failed searches (element does not exist)? Note: the problem is stated in general terms; in practice, searching may be done on arrays, lists, sets, or even solution spaces (for optimization problems). 3. Linear Search A basic and straightforward solution to the problem is the linear search algorithm (also known as sequential search). Basic idea: iterate over each element in the collection, compare with the key e k 6
7 3.1. Pseudocode Input : A collection of elements A = {a 1,..., a n } and a key e k Output: An element a A such that a = e k according to some criteria; φ if no such element exists 1 foreach a i A do 2 if a i = e k then 3 output a i 4 end 5 end 6 output φ Algorithm 1: Linear Search 3.2. Example Consider the array of integers in Table 1. Walk through the linear search algorithm on this array searching for the following keys: 20, 42, 102, 4. index element Table 1: An array of integers 3.3. Analysis As the name suggests, the complexity of linear search is linear. 1. Input: the collection of elements A 2. Input size: n as there are n elements 3. Elementary Operation: the comparison to the key in line 2 4. Best case: we immediately find the element, O(1) comparisons 5. Worst case: we don t find it, or we find it at the last elements, n comparisons, O(n) 6. Average case (details omitted): an expected number of comparisons of n O(n) 2 4. Binary Search A much more efficient way to search is the binary search algorithm. The basic idea is as follows. Under the assumption that the collection is sorted, we can: Examine the middle element: 7
8 1. If the the middle element is what we are searching for, done 2. If the element we are searching for is less than the middle element, it must lie in the lower-half of the list 3. Otherwise, it must lie in the upper-half of the list In either case: list is cut in half (at least); we can repeat the operation on the sub-list We continue until either: we find the element that matches the key, or the sub-list is empty 4.1. Pseudocode: Recursive Input : A sorted collection of elements A = {a 1,..., a n }, bounds 1 l, h n, and a key e k Output: An element a A such that a = e k according to some criteria; φ if no such element exists 1 if l > h then 2 output φ 3 end 4 m h+l 2 5 if a m = e k then 6 output a m 7 else if a m < e k then 8 BinarySearch(A, m + 1, h, e) 9 else 10 BinarySearch(A, l, m 1, e) 11 end Algorithm 2: Binary Search Recursive 8
9 4.2. Pseudocode: Iterative Input : A sorted collection of elements A = {a 1,..., a n } and a key e k Output: An element a A such that a = e k according to some criteria; φ if no such element exists 1 l 1 2 h n 3 while l h do 4 m h+l 2 5 if a m = e k then 6 output a m 7 else if a m < e k then 8 l (m + 1) 9 else 10 r (m 1) 11 end 12 end 13 output φ Algorithm 3: Binary Search Iterative 4.3. Example Consider the array of integers in Table 2. Walk through the linear search algorithm on this array searching for the following keys: 20, 0, 2000, 4. index element Table 2: A sorted array of integers 4.4. Analysis 1. Input: collection of elements, A 2. Input size: n, the number of elements 3. Elementary Operation: comparison, performed once for each middle element 4. Analysis: Let C(n) be the (maximum) number of comparisons made by binary search on a collection of size n 9
10 On each recursive call, one comparison is made, plus the number of comparisons on the next recursive call The list is cut in half; so the next recursive call made will contribute C ( ) n 2 In total: ( n ) C(n) = C By case 2 of the Master Theorem, Binary search is O(log n) 5. Another view: We begin with a list of size n After the first iteration, it is halved: n 2 After the second, it is halved again: n 2 2 After i such iterations: n = n } {{ 2} 2 i i The algorithm terminates when the list is of size 1 (actually, zero, but we ll treat the last iteration separately): n 2 i = 1 Solving for i = log n, thus O(log n) iterations Contrast binary search with linear search: suppose we wanted to search a database with 1 trillion (10 12 ) records. Linear search: approximately comparisons required Binary search: approximately log 2 (10 12 ) 40 comparisons Suppose further that a list initially has n elements and its size is doubled; then Linear search: twice as many more comparisons on the new list Binary search: log 2 (2n) = log 2 (2) + log (n) = log (n) + 1; that is, only one more comparison than before Binary search requires an up-front, O(n log n) cost to sort (or less if an order is maintained) If only done once, no need to sort, just use linear search If repeated, even a small amount, O(log n) searches say, then it pays to sort and use binary search 10
11 Figure 1: Illustrative example of the benefit of ordered (indexed) elements, Windows 7 5. Searching in Java 5.1. Linear Search Code Sample 1: Java Linear Search for integers 1 /** 2 * This function returns the index at which the given element 3 * first appears in the given array. Returns -1 if the array does 4 * not contain the element 5 */ 6 public static int linearsearch ( int a[], int element ) 7 { 8 for ( int i =0; i<a. length ; i ++) { 9 if(a[i] == element ){ 10 return i; 11 } 12 } 13 return -1; 14 } 5.2. Binary Search Code Sample 2: Java Recursive Binary Search for integers 1 /** 2 * This function returns an index at which the given element 3 * appears in the given array. It is assumed that the given array 4 * is sorted. This method returns -1 if the array does not 5 * contain the element 6 */ 7 public static int binarysearchrec ( int a[], int low, int high, int element ) 8 { 9 if(low > high ) 11
12 10 return -1; 11 int middle_index = ( high + low ) / 2; // see note 12 if(a[ middle_index ] == element ) 13 return middle_index ; 14 else if( a[ middle_index ] < element ) 15 return binarysearchrec ( a, middle_ index +1, high, element ); 16 else if( a[ middle_index ] > element ) 17 return binarysearchrec ( a, low, middle_ index -1, element ); 18 } Code Sample 3: Java Iterative Binary Search for integers 1 /** 2 * This function returns an index at which the given element 3 * appears in the given array. It is assumed that the given array 4 * is sorted. This method returns -1 if the array does not 5 * contain the element 6 */ 7 public static int binarysearch ( int a[], int element ) 8 { 9 int l = 0, h = a. length - 1; 10 while (l <= h) { 11 int m = (l + h) / 2; // see note 12 if(a[m] == element ) 13 return m; 14 else if(a[m] < element ) 15 l = m + 1; 16 else 17 h = m - 1; 18 } 19 return -1; 20 } Preventing Arithmetic Errors The lines of code that compute the new mid-index, such as int middle_index = (high + low)/ 2; are prone to arithmetic errors in certain situations in which you are dealing with very large arrays. In particular, if the variables high and low have a sum that exceeds the maximum value that a signed 32-bit integer can hold ( = 2, 147, 483, 647), then overflow will occur before the division by 2, leading to a (potentially) negative number. One solution would be to use a long integer instead which will prevent overflow as it would be able to handle any size array (which are limited to 32-bit signed integers (actually slightly less when considering the small amount of memory overhead for an object). 12
13 Another solution is to use operations that do not introduce this overflow. For example, l + h 2 = l + (h l) 2 but the right hand side will not be prone to overflow. Thus the code, int middle_index = low + (high - low)/ 2; would work. In fact, this bug is quite common [5] and was in the Java implementation, unreported for nearly a decade [1] Searching in Java: Doing It Right In practice, we don t reinvent the wheel: we don t write a custom search function for every type and for every sorting order. Instead we: 1. Use standard search functions provided by the language 2. Configure using a Comparator rather than Code Built-in functions require that the equals() and hashcode() methods are properly overridden (See Appendix A) Linear search: List.indexOf(Object o) Binary Search: int Collections.binarySearch(List list, Object key) Searches the specified list for the specified object using the binary search algorithm Returns the index at which key appears Returns something negative if not found Requires that the list contains elements that have a Natural Ordering Binary Search: int Collections.binarySearch(List, Object, Comparator) Searches the given list for the specified object using the binary search algorithm Uses the provided Comparator to determine order (not the equals() method), see Appendix B Binary Search with arrays: Arrays.binarySearch(T[] a, T key, Comparator) 5.4. Examples Code Sample 4: Java Search Examples 1 ArrayList < Student > roster = Student castrokey = null ; 4 int castroindex ; 13
14 5 6 // create a " key " that will match according to the Student. equals () method 7 castrokey = new Student (" Starlin ", " Castro ", , 3.95) ; 8 castroindex = roster. indexof ( castrokey ); 9 System. out. println (" at index " + castroindex + ": " + roster. get ( castroindex )); // create a key with only the necessary fields to match the comparator 12 castrokey = new Student (" Starlin ", " Castro ", 0, 0.0) ; 13 // sort the list according to the comparator 14 Collections. sort ( roster, byname ); 15 castroindex = Collections. binarysearch ( roster, castrokey, byname ) ; 16 System. out. println (" at index " + castroindex + ": " + roster. get ( castroindex )); // create a key with only the necessary fields to match the comparator 19 castrokey = new Student ( null, null, , 0. 0) ; 20 // sort the list according to the comparator 21 Collections. sort ( roster, bynuid ); 22 castroindex = Collections. binarysearch ( roster, castrokey, bynuid ) ; 23 System. out. println (" at index " + castroindex + ": " + roster. get ( castroindex )); 6. Searching in C 6.1. Linear Search Code Sample 5: C Linear Search for integers 1 /** 2 * This function returns the index at which the given element first 3 * appears in the given array. Returns -1 if the array does not 4 * contain the element. 5 */ 6 int linearsearchint ( int * a, int size, int key ) { 7 int i; 8 for (i =0; i< size ; i ++) { 9 if(a[i] == key ) { 10 return i; 11 } 14
15 12 } 13 return -1; 14 } 6.2. Binary Search Code Sample 6: C Recursive Binary Search for integers 1 /** 2 * This function returns an index at which the given element 3 * appears in the given array. It is assumed that the given array 4 * is sorted. This method returns -1 if the array does not 5 * contain the element. 6 */ 7 int binarysearchrecursive ( int *a, int low, int high, int element ) 8 { 9 if(low > high ) 10 return -1; 11 int middle_index = low + ( high - low ) / 2; 12 if(a[ middle_index ] == element ) 13 return middle_index ; 14 else if( a[ middle_index ] < element ) 15 return binarysearchrecursive (a, middle_index +1, high, element ); 16 else if( a[ middle_index ] > element ) 17 return binarysearchrecursive (a, low, middle_index -1, element ) ; 18 } Code Sample 7: C Iterative Binary Search for integers 1 /** 2 * This function returns an index at which the given element 3 * appears in the given array. It is assumed that the given array 4 * is sorted. This method returns -1 if the array does not 5 * contain the element 6 */ 7 int binarysearch ( int * a, int size, int element ) 8 { 9 int l = 0, h = size -1; 10 while (l <= h) { 11 int m = l + (l - h) / 2; 12 if(a[m] == element ) 13 return m; 14 else if(a[m] < element ) 15
16 15 l = m + 1; 16 else 17 h = m - 1; 18 } 19 return -1; 20 } 6.3. Searching in C: Doing it Right In practice, we don t reinvent the wheel: we don t write a custom search function for every type and for every sorting order. Instead we: 1. Use standard search functions provided by the language 2. Configure using a comparator function rather than Code (see Appendix D) Linear Search The C library search.h provides a linear search function to search arrays. void *lfind(const void *key, const void *base, size_t *nmemb, size_t size, int(* compar)(const void *, const void *)); key - a dummy struct matching what youre looking for (can build an instance with only the required fields) base - a pointer to the array to be searched nmemb - the size of the array (number of members) size - size of each element (use sizeof()) compar - a pointer to a comparator function (see Appendices) Returns a pointer to the first instance matching the key, returns NULL if not found Binary Search The C standard library (stdlib.h) provides a binary search function to search arrays. void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); key - a dummy struct matching what you are looking for (can build an instance with only the required fields) base - a pointer to the array to be searched nmemb - the size of the array (number of members) size - size of each element (use sizeof()) compar - a pointer to a comparator function (see Appendices) 16
17 Returns the first element that it finds (not necessarily the first in the order) Returns NULL if not found Assumes the array is sorted according to the same comparator function, otherwise undefined behavior 6.4. Examples Code Sample 8: C Search Examples 1 # include < stdio.h> 2 # include < stdlib.h> 3 # include < search.h> 4 5 # include " student.h" 6 7 int main ( int argc, char ** argv ) { 8 9 int n = 0; 10 Student * roster = loadstudents (" student. data ", &n); 11 int i; 12 size_ t numelems = n; printf (" Roster : \n"); 16 printstudents ( roster, n); /* Searching */ 19 Student * castro = NULL ; 20 Student * castrokey = NULL ; 21 Student * sandberg = NULL ; 22 char * str = NULL ; castro = linearsearchstudentbynuid ( roster, 10, ) ; 25 str = studenttostring ( castro ); 26 printf (" castro : %s\n", str ); 27 free ( str ); // create a key that will match according to the NUID 30 int nuid = ; 31 Student * key = createemptystudent (); 32 key -> nuid = nuid ; // use lfind to find the first such instance : 35 // sandberg = 36 sandberg = lfind ( key, roster, & numelems, sizeof ( Student ), studentidcmp ); 17
18 37 str = studenttostring ( sandberg ); 38 printf (" sandberg : %s\n", str ); 39 free ( str ); // create a key with only the necessary fields 42 castrokey = createstudent (" Starlin ", " Castro ", 0, 0.0) ; 43 // sort according to a comparator function 44 qsort ( roster, n, sizeof ( Student ), studentlastnamecmp ); castro = bsearch ( castrokey, roster, n, sizeof ( Student ), studentlastnamecmp ); 47 str = studenttostring ( castro ); 48 printf (" castro ( via binary search ): %s\n", str ); 49 free ( str ); // create a key with only the necessary fields 52 castrokey = createstudent ( NULL, NULL, , 0. 0) ; 53 // sort according to a comparator function 54 qsort ( roster, n, sizeof ( Student ), studentidcmp ); castro = bsearch ( castrokey, roster, n, sizeof ( Student ), studentidcmp ); 57 str = studenttostring ( castro ); 58 printf (" castro ( via binary search ): %s\n", str ); 59 free ( str ); return 0; 62 } Part II. Sorting Problem 2 (Sorting). Given: a collection of orderable elements, A = {a 1, a 2,..., a n } Output: A permuted list of elements A = {a 1, a 2,..., a n} according to a specified order Simple, but ubiquitous problem Fundamental operation for data processing Large variety of algorithms, data structures, applications, etc. Sorting algorithms are usually analyzed with respect to: Number of comparisons in the worst, best, average cases Swaps 18
19 Extra memory required Other considerations Practical considerations: what ordered collections (Lists, arrays, etc.) are supported by a language Most languages provide standard (optimized, well-tested) sorting functionality; use it! Sorting stability: A sorting algorithm is said to be stable if the relative order of equal elements is preserved. Example: suppose that a list contains 10, 2 a, 5, 2 b ; a stable sorting algorithm would produce 2 a, 2 b, 5, 10 while a non-stable sorting algorithm may produce 2 b, 2 a, 5, 10. Stable sorts are important for data presentation (sorting by two columns/categories) Stability depends on inequalities used and behavior of algorithms Throughout, we will demonstrate examples of sorting based on the array in Figure Figure 2: Unsorted Array 7. Bubble Sort 7.1. Basic Idea Pass through the list and swap individual pairs of elements if they are out of order At the end of the first pass, the maximal element has bubbled up to the end of the list Repeat this process n times Pseudocode presented in Algorithm 4 Java code (for integers) presented in Code Sample 9 C code (for integers) presented in Code Sample 10 Example 19
20 7.2. Pseudocode Input : A collection A = {a 1,..., a n } Output: An array A containing all elements of A in nondecreasing order 1 for i = 1,..., (n 1) do 2 for j = 2,..., (n i + 1) do 3 if a j 1 > a j then 4 swap a j 1, a j 5 end 6 end 7 end Algorithm 4: Bubble Sort 7.3. Analysis Elementary operation: comparison Performed once on line 3 Inner loop (line 2 executed n i times Outer loop (line 1) executed n times In total: n i=1 Bubble sort is O(n 2 ) (n i+1) j=2 1 = ( n n ) (n i) = n 2 i i=1 i=1 = n2 n Code Samples Code Sample 9: Java Bubble Sort for integers 1 public static void bubblesort ( int array []) { 2 int n = array. length ; 3 int temp = 0; 4 for ( int i = 0; i < n; i ++) { 5 for ( int j = 1; j < (n-i); j ++) { 6 if( array [j -1] > array [j]) { 7 temp = array [j -1]; 8 array [j -1]= array [j]; 9 array [j]= temp ; 10 } 11 } 12 } 20
21 13 } Code Sample 10: C Bubble Sort for integers 1 void bubblesortint ( int * array, int n) { 2 int temp = 0; 3 for ( int i = 0; i < n; i ++) { 4 for ( int j = 1; j < (n-i); j ++) { 5 if( array [j -1] > array [j]) { 6 temp = array [j -1]; 7 array [j -1]= array [j]; 8 array [j]= temp ; 9 } 10 } 11 } 12 } 8. Selection Sort 8.1. Basic Idea Iterate through the elements in the list and find the minimal element in the list Swap the minimal element with the first element Repeat the process on the remaining n 1 elements Pseudocode presented in Algorithm 5 Java code (for integers) presented in Code Sample 11 C code (for integers) presented in Code Sample 12 Example Note: Selection sort is not stable, an example: 2 a, 2 b, 1, 5; the first swap would result in 1, 2 b, 2 a, 5 and no subsequent changes would be made. 21
22 8.2. Pseudocode Input : A collection A = {a 1,..., a n } Output: An array A containing all elements of A in nondecreasing order 1 for i = 1,..., (n 1) do 2 a min a i 3 for j = (i + 1),..., n do 4 if a min > a j then 5 min a j 6 end 7 end 8 swap a min and a i 9 end Algorithm 5: Selection Sort 8.3. Analysis Comparison performed: once, inner loop, outer loop In total: n 1 n i=1 j=(i+1) n 1 n 1 1 = (n i) = n(n 1) i = n(n 1) i=1 i=1 n(n 1) 2 = n(n 1) 2 Selection Sort is O(n 2 ) 8.4. Code Samples Code Sample 11: Java Selection Sort for integers 1 public static void selectionsort ( int array []) { 2 int n = array. length ; 3 for ( int i =0; i<n; i ++) { 4 int index_of_min = i; 5 for ( int j=i; j<n; j ++) { 6 if( array [ index_of_min ] > array [j]){ 7 index_of_min = j; 8 } 9 } 10 int temp = array [i]; 11 array [ i] = array [ index_of_min ]; 12 array [ index_of_min ] = temp ; 13 } 22
23 14 } Code Sample 12: C Selection Sort for integers 1 void selectionsort ( int * array, int n) { 2 for ( int i =0; i<n; i ++) { 3 int index_of_min = i; 4 for ( int j=i; j<n; j ++) { 5 if( array [ index_of_min ] > array [j]){ 6 index_of_min = j; 7 } 8 } 9 int temp = array [i]; 10 array [ i] = array [ index_of_min ]; 11 array [ index_of_min ] = temp ; 12 } 13 } 9. Insertion Sort 9.1. Basic Idea If we consider just the first element, the list is sorted Now consdier the second element: we can insert it in the currently sorted sublist In general: at the k-th iteration, assume that elements a 1,..., a k are sorted Insert element a k+1 into the proper location among elements a 1,..., a k Insertion is performed by shifting a k+1 down until its in the right spot Pseudocode presented in Algorithm 6 Java code (for integers) presented in Code Sample 13 C code (for integers) presented in Code Sample 14 Example 23
24 9.2. Pseudocode Input : A collection A = {a 1,..., a n } Output: An array A containing all elements of A in nondecreasing order 1 for i = 2,..., n do 2 x a i 3 j i 4 while j > 1 and a j 1 > x do 5 a j a j 1 6 decrement j 7 end 8 a j x 9 end Algorithm 6: Insertion Sort 9.3. Analysis Best case: list is already sorted, number of comparisons is (n 1) Worst case: reversed list; each iteration shifts the element all the way down First iteration: 1 comparison, 2nd: at most 2, etc., last iteration: at most (n 1) comparisons n 1 n(n 1) i = 2 Worst case: O(n 2 ) A more complex analysis for the average case, but still O(n 2 ) i=1 Practical consideration: insertion sort is inherently adaptive; good performance in practice Very efficient on small lists, especially if there is already some structure (partially ordered) to them 9.4. Code Samples Code Sample 13: Java Insertion Sort for integers 1 public static void insertionsort ( int array []) { 2 int value ; 3 for ( int i =1; i< array. length ; i ++) { 4 value = array [i]; 5 int j = i; 24
25 6 while (j > 0 && array [j -1] > value ) { 7 array [j] = array [j -1]; 8 j - -; 9 } 10 array [j] = value ; 11 } 12 } Code Sample 14: C Insertion Sort for integers 1 void insertionsort ( int * array, int size ) { 2 int value ; 3 for ( int i =1; i< size ; i ++) { 4 value = array [i]; 5 int j = i; 6 while (j > 0 && array [j -1] > value ) { 7 array [j] = array [j -1]; 8 j - -; 9 } 10 array [j] = value ; 11 } 12 } 10. Quick Sort Due to Tony Hoare in the early 1960s [2, 3] Basic Idea Choose a pivot element a p Partition all other elements around this pivot: move all less elements to the left side of the list; all greater elements to the right Place pivot element between them Repeat the process on the two partitions until each sublist is trivially sorted (empty or a single element) Pseudocode presented in Algorithm 7 and 8 Java code (for integers) presented in Code Sample 15 C code (for integers) presented in Code Sample 17 Example 25
26 10.2. Pseudocode Input : A collection A = {a 1,..., a n }, indices l, r Output: An array A containing all elements of A in nondecreasing order 1 if l < r then 2 s = Partition(A[l... r]) 3 QuickSort(A[l... (s 1)]) 4 QuickSort(A[(s + 1)... r]) 5 end Algorithm 7: Quick Sort Input : Integer array A, sub-indices 1 l, r n Output: A partitioned integer array A such that all A[l... (j 1)] A[j] A[(j + 1)... r] and an index j of the pivot element. 1 pivot A[l] 2 i l 3 j (r + 1) 4 repeat 5 repeat 6 i (i + 1) 7 until a i pivot 8 repeat 9 j (j 1) 10 until a j pivot 11 swap a i, a j 12 until i j 13 swap a i, a j //Undo the last swap 14 swap a l, a j //Swap the pivot 15 return j Algorithm 8: In-Place Partition Analysis Performance of quick sort is highly dependent on how the even the partitioning is Partitioning depends on pivot choice Best Case Ideal pivot choice: always choose the median element 26
27 Evenly divides the list into two equal parts Two calls to quick sort on lists roughly half the size A linear number of comparisons to partition Recurrence: ( n ) C(n) = 2C + n 2 By the Master Theorem, O(n log n) Worst Case Worst pivot choice: an extremal element Divides into an empty list and a list of size (n 1) Only one recursive call is necessary, but on a list of size (n 1) Recurrence: C(n) = C(n 1) + n Back substitution yields O(n 2 ) Average Case Even if the list is is split by a constant proportion, still O(n log n) A careful average case analysis also yields C(n) 1.38n log n Still O(n log n) Pivot Choice: Pivot choice greatly affects performance; several strategies: Median-of-Three Among three elements choose the median. Guarantees that the pivot choice will never be the worst case. Does not guarantee Θ(n log n). Random Element Randomly select an index for a pivot element. Guarantees average running time of Θ(n log n). Extra work to randomly select a pivot. Linear Time Median Finding. An algorithm exists that runs in Θ(n) time to find the median of n objects. Guarantees Θ(n log n) in all cases. Complicated; recursive; huge overhead Code Samples Code Sample 15: Java Quick Sort for integers 1 private static void quicksort ( int a[], int left, int right ) { 27
28 2 int index = partition ( a, left, right ); 3 if ( left < index - 1) 4 quicksort ( a, left, index - 1); 5 if ( index < right ) 6 quicksort (a, index, right ); 7 } Code Sample 16: Java Partition subroutine for integers 1 public static int partition ( int a[], int left, int right ) 2 { 3 int i = left, j = right ; 4 int tmp ; 5 int pivot = a[( left + right ) / 2]; 6 7 while (i <= j) { 8 while (a[i] < pivot ) 9 i ++; 10 while (a[j] > pivot ) 11 j - -; 12 if (i <= j) { 13 tmp = a[i]; 14 a[i] = a[j]; 15 a[j] = tmp ; 16 i ++; 17 j - -; 18 } 19 } 20 return i; 21 } Code Sample 17: C Quick Sort for integers 1 void swap ( int *a, int *b) 2 { 3 int t=*a; *a=*b; *b=t; 4 } 5 void sort ( int arr [], int beg, int end ) 6 { 7 if ( end > beg + 1) 8 { 9 int piv = arr [ beg ], l = beg + 1, r = end ; 10 while (l < r) 11 { 12 if ( arr [l] <= piv ) 13 l ++; 14 else 15 swap (& arr [l], & arr [--r]); 28
29 16 } 17 swap (& arr [--l], & arr [ beg ]); 18 sort (arr, beg, l); 19 sort (arr, r, end ); 20 } 21 } A good tutorial with a non-recursive C implementation: quicksort/ 11. Merge Sort Due to John von Neumann, 1945 (as reported by Knuth [4]) Basic Idea Divide the list into two (roughly) equal parts and recursively sort each Recursion stops when the list is trivially sorted (empty or a single element) When returning from the recursion, merge the two lists together Since both sublists are sorted, we only need to maintain an index to each list, add the least element, etc. Only a linear amount of work to merge two sorted lists Pseudocode presented in Algorithm 9 Java code (for integers) presented in Code Sample 18 C code (for integers) presented in Code Sample 20 Example Pseudocode Input : An array, sub-indices 1 l, r n Output: An array A such that A[l,..., r] is sorted 1 if l < r then 2 MergeSort(A, l, r+l ) 2 3 MergeSort(A, r+l, r) 2 4 Merge sorted lists A[l,..., r+l 2 5 end r+l ] anda[,..., r] 2 Algorithm 9: Merge Sort 29
30 11.3. Analysis Because merge sort divides the list first, an even split is guaranteed After the recursion, a linear amount of work is done to merge the two lists Basic idea: Keep two index pointers to each sublist Copy the minimal element, advance the pointer Until one list is empty, then just copy the rest of the other Requires use of O(n)-sized temporary array Recurrence: ( n ) C(n) = 2C + n 2 By the Master Theorem, O(n log n) In fact, a guarantee (best, worst, average are the same) Code Samples Code Sample 18: Java Merge Sort for integers 1 public static void mergesort ( int a[], int low, int high ) 2 { 3 if ( low < high ) { 4 int middle = ( low + high ) / 2; 5 mergesort (low, middle ); 6 mergesort ( middle + 1, high ); 7 merge (low, middle, high ); 8 } 9 } Code Sample 19: Java Merge for integers 1 private static int MERGE_HELPER []; private static void merge ( int a[], int low, int middle, int high ) { 4 5 // Copy both parts into the helper array 6 for ( int i = low ; i <= high ; i ++) { 7 MERGE_HELPER [i] = a[i]; 8 } 9 int i = low ; 10 int j = middle + 1; 11 int k = low ; 30
31 12 // Copy the smallest values from the left or the right side back to the original array 13 while ( i <= middle && j <= high ) { 14 if ( MERGE_HELPER [i] <= MERGE_HELPER [j]) { 15 a[k] = MERGE_HELPER [i]; 16 i ++; 17 } else { 18 a[k] = MERGE_HELPER [j]; 19 j ++; 20 } 21 k ++; 22 } 23 // Copy the rest of the left side of the array into the target array 24 while ( i <= middle ) { 25 a[k] = MERGE_HELPER [i]; 26 k ++; 27 i ++; 28 } 29 // Copy the rest of the right side of the array into the target array 30 while ( j <= high ) { 31 a[k] = MERGE_HELPER [j]; 32 k ++; 33 j ++; 34 } 35 } Code Sample 20: C Merge Sort for integers 1 void mergesort ( int * array, int left, int right ) 2 { 3 int mid = ( left + right ) /2; 4 /* We have to sort only when left < right because when left = right it is anyhow sorted */ 5 if(left < right ) 6 { 7 /* Sort the left part */ 8 mergesort ( array,left, mid ); 9 /* Sort the right part */ 10 mergesort ( array, mid +1, right ); 11 /* Merge the two sorted parts */ 12 merge ( array,left,mid, right ); 13 } 14 } The function mergesort would be called as follows: 31
32 1 int *a = ( int *) malloc ( sizeof ( int ) * n); mergesort (a, 0, n -1) ; Code Sample 21: C Merge Sort for integers 1 void merge ( int * array, int left, int mid, int right ) 2 { 3 /* temporary array to store the new sorted part */ 4 int temparray [ right - left +1]; 5 int pos =0, lpos = left, rpos = mid + 1; // not strict ANSI C 6 while ( lpos <= mid && rpos <= right ) { 7 if( array [ lpos ] < array [ rpos ]) { 8 temparray [ pos ++] = array [ lpos ++]; 9 } else { 10 temparray [ pos ++] = array [ rpos ++]; 11 } 12 } 13 while ( lpos <= mid ) temparray [ pos ++] = array [ lpos ++]; 14 while ( rpos <= right ) temparray [ pos ++] = array [ rpos ++]; 15 int iter ; 16 /* copy back the sorted array to the original array */ 17 for ( iter =0; iter < pos ; iter ++) { 18 array [ iter + left ] = temparray [ iter ]; 19 } 20 return ; 21 } 12. Heap Sort Due to J. W. J. Williams, 1964 [7]. See Tree Note Set 13. Tim Sort Due to Tim Peters in 2002 [6] who originally wrote it for the Python language. It has since been adopted in Java (version 7 for arrays of non-primitive types). TODO 32
33 Algorithm Complexity Best Average Worst Stability Notes Bubble Sort O(n 2 ) O(n 2 ) O(n 2 ) Stable Selection Sort O(n 2 ) O(n 2 ) O(n 2 ) Not Stable Insertion Sort O(n) O(n 2 ) O(n 2 ) Stable Best in practice for small collections Quick Sort O(n log n) O(n log n) O(n 2 ) Not Stable Performance depends on pivot choices Merge Sort Θ(n log n) O(n log n) O(n log n) Stable Guaranteed Θ(n log n) 14. Summary Table 3: Summary of Sorting Algorithms 15. In Practice: Sorting in Java Arrays: Lists: java.util.arrays provides a sort method for all primitive types in ascending order sort(object[] a) allows you to sort arrays of objects that have a natural ordering: classes that implement the Comparable interface sort(t[] a, Comparator<? super T> c) allows you to sort objects according to the order defined by a provided Comparator (see Appendix B) java.util.collections provides two sort methods to sort List collections sort(list<t> list) sorts in ascending order according to the natural ordering sort(list<t> list, Comparator<? super T> c) sorts according to the order defined by the given comparator Java specification dictates that the sorting algorithm must be stable Java 1 6: hybrid merge/insertion sort Java 7: timsort (a bottom-up merge sort that merges runs of ordered sub lists) Considerations When sorting collections or arrays of objects, we may need to consider the possibility of uninitialized null objects. How we handle these are a design decision. We could ignore it in which case such elements would likely result in a NullPointerException and expect the user to prevent or handle such instances. This may be the preferable choice in most 33
34 instances, in fact. Alternatively, we could handle null objects in the design of our Comparator. Code Snippet 22 presents a comparator for our Student class that orders null instances first. Code Sample 22: Handling Null Values in Java Comparators 1 Comparator < Student > bynamewithnulls = new Comparator < Student >() { 3 public int compare ( Student a, Student b) { 4 if( a == null && b == null ) { 5 return 0; 6 } else if( a == null && b!= null ) { 7 return -1; 8 } else if( a!= null && b == null ) { 9 return 1; 10 } else { 11 if(a. getlastname (). equals (b. getlastname ())) { 12 return a. getfirstname (). compareto (b. getfirstname ()); 13 } else { 14 return a. getlastname (). compareto (b. getlastname ()); 15 } 16 } 17 } 18 }; Examples Code Sample 23: Using Java Collection s Sort Method 1 List < Student > roster =... 2 Student rosterarr [] =... 3 Comparator byname =... 4 Comparator bygpa = // sort by name : 7 Collections. sort ( roster, byname ); 8 Arrays. sort ( rosterarr, byname ); 9 10 // sort by GPA : 11 Collections. sort ( roster, bygpa ); 12 Arrays. sort ( rosterarr, bygpa ); 34
35 16. In Practice: Sorting in C The standard C library (stdlib.h) provides a Quick sort implementation: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) base pointer an array of elements nmemb the size of the array (number of members) size the size (in bytes) of each element (use sizeof) compar a comparator function used to order elements Sorts in ascending order according to the provided comparator function Advantages: No need to write a new sorting algorithm for every user defined type and every possible order along every possible component Only need to create a simple comparator function Less code, less chance of bugs qsort is well-designed, highly optimized, well tested, proven Prefer configuration over coding Represents a weak form of polymorphic behavior (same code can be executed on different types) Sorting Pointers to Elements Comparator functions take void pointers to the elements that they are comparing. Often, you have need to sort an array of pointers to elements. The most common use case for this is using qsort to sort strings. An array of strings can be thought of as a 2-dimensional array of chars. Specifically, an array of strings is a char ** type. That is, an array of pointers to chars. We may be tempted to use strcmp in the standard string library, passing it to qsort. Unfortunately this will not work. qsort requires two const void * types, while strcmp takes two const char * types. This difference is subtle but important; a full discussion can be found on the c-faq ( The recommended way of doing this is to define a different comparator function as follows. Code Sample 24: C Comparator Function for Strings 1 /* compare strings via pointers */ 2 int pstrcmp ( const void * p1, const void * p2) 3 { 4 return strcmp (*( char * const *) p1, *( char * const *) p2); 5 } 35
36 Observe the behavior of this function: it uses the standard strcmp function, but makes the proper explicit type casting before doing so. The *(char * const *) casts the generic void pointers as pointers to strings (or pointers to pointers to characters), then dereferences it to be compatible with strcmp. Another case is when we wish to sort user defined types. The Student structure presented earlier is small in that it only has a few fields. When structures are stored in an array and sorted, there may be many swaps of individual elements which involves a lot of memory copying. If the structures are small this is not too bad, but for larger structures this could be potentially expensive. Instead, it may be preferred to have an array of pointers to structures. Swapping elements involves only swapping pointers instead of the entire structure. This is far cheaper as a memory address is likely to be far smaller than the actual structure it points to. This is essentially equivalent to the string scenario: we have an array of pointers to be sorted, our comparator function then needs to deal with pointers to pointers. A full discussion can be found on c-faq ( An example appears in Code Snippet 25. Code Sample 25: Sorting Structures via Pointers 1 // An array of pointers to Students 2 Student ** roster = ( Student **) malloc ( sizeof ( Student *) * n); 3 qsort ( roster, n, sizeof ( Student *), studentptrlastnamecmp ); int studentptrlastnamecmp ( const void *s1, const void *s2) { 8 // we receive a pointer to an individual element in the array 9 // but individual elements are POINTERS to students 10 // thus we cast them as ( const Student **) 11 // then dereference to get a poitner to a student! 12 const Student * a = *( const Student **) s1; 13 const Student * b = *( const Student **) s2; 14 int result = strcmp (a-> lastname, b-> lastname ); 15 if( result == 0) { 16 return strcmp (a-> firstname, b-> firstname ); 17 } else { 18 return result ; 19 } 20 } Another issue when sorting arrays of pointers is that we may now have to deal with NULL elements. When sorting arrays of elements this is not an issue as a properly initialized array will contain non-null elements (though elements could still be uninitialized, the memory space is still valid). How we handle NULL pointers is more of a design decision. We could ignore it and any attempt to access a NULL structure will result in undefined behavior (or segmentation faults, etc.). Or we could give NULL values an explicit ordering with respect to other elements. That is, we could order all NULL pointers before non-null elements (and consider 36
37 all NULL pointers to be equal). An example with respect to our Student structure is given in Code Snippet 26. Code Sample 26: Handling Null Values 1 int studentptrlastnamecmpwithnulls ( const void *s1, const void *s2 ) { 2 const Student * a = *( const Student **) s1; 3 const Student * b = *( const Student **) s2; 4 if( a == NULL && b == NULL ) { 5 return 0; 6 } else if( a == NULL && b!= NULL ) { 7 return -1; 8 } else if ( a!= NULL && b == NULL ) { 9 return 1; 10 } 11 int result = strcmp (a-> lastname, b-> lastname ); 12 if( result == 0) { 13 return strcmp (a-> firstname, b-> firstname ); 14 } else { 15 return result ; 16 } 17 } Examples Code Sample 27: Using C s qsort function 1 # include < stdio.h> 2 # include < stdlib.h> 3 4 # include " student.h" int main ( int argc, char ** argv ) { 8 9 int n = 0; 10 Student * roster = loadstudents (" student. data ", &n); 11 int i; 12 size_ t numelems = n; printf (" Roster : \n"); 15 printstudents ( roster, n); printf ("\n\n\ nsorted by last name / first name : \n"); 18 qsort ( roster, numelems, sizeof ( Student ), studentlastnamecmp ); 19 printstudents ( roster, n); 37
38 20 21 printf ("\n\n\ nsorted by ID: \n"); 22 qsort ( roster, numelems, sizeof ( Student ), studentidcmp ); 23 printstudents ( roster, n); printf ("\n\n\ nsorted by ID, descending : \n"); 26 qsort ( roster, numelems, sizeof ( Student ), studentidcmpdesc ); 27 printstudents ( roster, n); printf ("\n\n\ nsorted by GPA : \n"); 30 qsort ( roster, numelems, sizeof ( Student ), studentgpacmp ); 31 printstudents ( roster, n); return 0; 34 } A. Java: equals and hashcode methods Every class in Java is a sub-class of the java.lang.object class Object defines several methods whose default behavior is to operate on Java Virtual Machine memory addresses: public String tostring() returns a hexadecimal representation of the memory address of the object public int hashcode() may convert the memory address to an integer and return it public boolean equals(object obj) will compare obj to this instance and return true or false if they have the same or different memory address When defining your own objects, its best practice to override these methods to be dependent on the entire state of the object Doing so is necessary if you use your objects in the Collections library If not, then creating a key object will never match an array element as equals will always return false (since the key and any actual element will not have the same memory address) Good tutorial: Eclipse Tip: Let Eclipse do it for you! Source Generate equals and hashcode, Generate tostring, etc. A.1. Example Code Sample 28: Java Student class 38
39 1 package unl. cse. searching_sorting ; 2 3 public class Student { 4 5 private String firstname ; 6 private String lastname ; 7 private Integer nuid ; 8 private Double gpa ; 9 10 public Student ( String firstname, String lastname, int nuid, double gpa ) { 11 this. firstname = firstname ; 12 this. lastname = lastname ; 13 this. nuid = nuid ; 14 this. gpa = gpa ; 15 } public String tostring () { 19 StringBuilder sb = new StringBuilder (); 20 sb. append ( lastname ) 21. append (", ") 22. append ( firstname ) 23. append (" (") 24. append ( nuid ) 25. append ("), ") 26. append ( gpa ); 27 return sb. tostring (); 28 } public boolean equals ( Object obj ) { 32 if ( this == obj ) 33 return true ; 34 if ( obj == null ) 35 return false ; 36 if ( getclass ()!= obj. getclass ()) 37 return false ; 38 Student that = ( Student ) obj ; 39 boolean equal = true ; 40 equal = equal && ( this. firstname!= null? this. firstname. equals ( that. firstname ) : that. firstname == null ); 41 equal = equal && ( this. lastname!= null? this. lastname. equals ( that. lastname ) : that. lastname == null ); 42 equal = equal && ( this. nuid!= null? this. nuid. equals ( that. nuid ) : that. nuid == null ); 39
40 43 equal = equal && ( this. gpa!= null? this. gpa. equals ( that. gpa ) : that. gpa == null ); 44 return equal ; 45 } public int hashcode () { 49 int hash = 7; 50 hash = (31 * hash ) + nuid. hashcode (); 51 hash = (31 * hash ) + gpa. hashcode (); 52 hash = (31 * hash ) + firstname. hashcode (); 53 hash = (31 * hash ) + lastname. hashcode (); 54 return hash ; 55 } } B. Java: The Comparator Interface Resource: Interface documentation: Objects may have a natural ordering (built-in types or objects that are Comparable), otherwise Comparator classes may be created that define an ordering Comparator<T> is a parameterized interface T is the type that the comparator is used on (Integer, Double, Student) As an interface, it only specifies one method: public int compare(t a, T b) Basic contract: returns Something negative if a < b Zero if a equals b Something positive if a > b Usual to create comparators as anonymous classes (classes created and defined inline, not in a separate class; comparators are ad-hoc, use-as-needed classes) Design Tip: Don t Repeat Yourself; utilize comparison functions already provided by the language! Take care with algebraic tricks (subtraction) to return a difference: Some combinations may not give correct results due to overflow Differences with floating point numbers may give incorrect results when trun- 40
41 cated to integers B.1. Examples Code Sample 29: Java Comparator Examples for the Student class 1 Comparator < Student > byname = new Comparator < Student >() { 3 public int compare ( Student a, Student b) { 4 if(a. getlastname (). equals (b. getlastname ())) { 5 return a. getfirstname (). compareto (b. getfirstname ()); 6 } else { 7 return a. getlastname (). compareto (b. getlastname ()); 8 } 9 } 10 }; Comparator < Student > bynamedesc = new Comparator < Student >() { 14 public int compare ( Student a, Student b) { 15 if(b. getlastname (). equals (a. getlastname ())) { 16 return b. getfirstname (). compareto (a. getfirstname ()); 17 } else { 18 return b. getlastname (). compareto (a. getlastname ()); 19 } 20 } 21 }; Comparator < Student > bynuid = new Comparator < Student >() { 25 public int compare ( Student a, Student b) { 26 return (a. getnuid () - b. getnuid ()); 27 } 28 }; B.2. Exercises 1. Write comparator instances to order Student objects by GPA, NUID descending, NUID as a padded out string 2. Write a custom sorting algorithm that uses a comparator to sort 3. Consider what happens with these comparators if we attempt to compare a null object with another object. How can we implement these differently? 41
42 C. C: Function Pointers Pointers (references) point to memory locations Pointers can point to: simple data types (int, double), user types (structs), arrays, etc. Pointers can be generic, called void pointers: (void *) Void pointers just point to the start of a memory block, not a specific type A program s code also lives in memory; each function s code lives somewhere in memory Therefore: pointers can also point to functions (the memory address where the code for the function is stored)! The pointer to a function is simply its identifier (function name) Usage: in callbacks: gives the ability to specify a function that another function should call/use (Graphical User Interface and Event Driven Programming) Declaration syntax: int (*ptrtofunc)(int, double, char)= NULL; (ptrtofunc is a pointer to a function that takes three parameters (an int, double, and char) and returns an int Components: Return type Name of the pointer Parameter list type Assignment Syntax: ptrtofunc = functionname; or ptrtofunc = &functionname; Function pointers can be used as function parameters Functions can also be called (invoked) using a function pointer Passing as an argument to a function: just pass the function name! Function Pointers in C tutorial: Note: some languages treat functions as first-class citizens, meaning that variables can be numeric, strings, etc., but also functions! In C, such functionality is achieved through the use of function pointers. C.1. Full Example Code Sample 30: C Function Pointer Examples 1 # include < stdio.h> 2 # include < stdlib.h> 42
43 3 4 int function01 ( int a, double b); 5 void function02 ( double x, char y); 6 7 void runafunction ( int (* thefunc )(int, double )); 8 9 int main ( int argc, char ** arg ) { int i = 5; 12 double d = 3. 14; 13 char c = Q ; // calling a function normally int j = function01 (i, d); 17 function02 (d, c); // function pointer declaration 20 int (* pt2func01 )(int, double ) = NULL ; 21 void (* pt2func02 )( double, char ) = NULL ; // assignment 24 pt2func01 = function01 ; 25 // or: 26 pt2func01 = & function01 ; 27 pt2func02 = & function02 ; // you can invoke a function using a pointer to it: 30 j = pt2func01 (i, d); 31 pt2func02 (d, c); // alternatively, you can invoke a function by dereferencing it: 34 j = (* pt2func01 )(i, d); 35 (* pt2func02 )(d, c); // With function pointers, you can now pass entire functions as arguments to another function! 38 printf (" Calling runafunction...\ n"); 39 runafunction ( pt2func01 ); 40 // we should not pass in the second pointer as it would not match the signature : 41 // syntactically okay, compiler warning, undefined behavior 42 // runafunction ( pt2func02 ); 43 } void runafunction ( int (* thefunc )( int, double )) { printf (" calling within runafunction...\ n"); 43
44 48 int result = thefunc (20,.5) ; 49 printf (" the result was %d\n", result ); return ; 52 } int function01 ( int a, double b) { 55 printf (" You called function01 on a = %d, b = %f\n", a, b); 56 return a + 10; 57 } void function02 ( double x, char y) { printf (" You called function02 on x = %f, y = %c\n", x, y); } D. C: Comparator Functions Motivation: Compiler knows how to compare built-in primitive types (you can directly use comparison operators, <, >, <=, >=, ==,!= Need a way to search and sort for user defined types Sorting user-defined types (Student, Album, etc.) according to some field or combination of fields would require a specialized function for each type Ascending, descending, with respect to any field or combination of fields: requires yet-another-function? Should not have to reinvent the wheel every time we should be able to use the same basic sorting algorithm but configured to give us the order we want In C: we make use of a comparator function and function pointers Utilize standard library s sort (qsort) and search (bsearch) functions Comparator Function: All comparator functions have the signature: int(*compar)(const void *a, const void *b) compar is the function name, it should be unique and descriptive Arguments: two elements to be compared to each other; note: const and void * Returns an integer Function contract: Returns 44
45 Something negative if a precedes (is less than ) b Zero if a is equal to b Something positive if a succeeds (is greater than ) b Argument types are const, so guaranteed not to change Argument types are generic void pointers: void * to make comparator functions as general as possible Standard Implementation Pattern: 1. Make the general void pointers into a pointer to a specific type by making an explicit type cast: 2. Use their state (one of their components or a combination of components) to determine their order 3. Return an integer that expresses this order Design tips: Make use of available comparator functions (strcmp, etc.) Reverse order: use another comparator and flip its value! Take care with algebraic tricks (subtraction) to return a difference: Some combinations may not give correct results due to overflow Differences with floating point numbers may give incorrect results when truncated to integers D.1. Examples Code Sample 31: C Structure Representing a Student 1 /** 2 * A structure to represent a student 3 */ 4 typedef struct { 5 char * firstname ; 6 char * lastname ; 7 int nuid ; 8 double gpa ; 9 } Student ; Code Sample 32: C Comparator Function Examples for Student structs 1 /** 2 * A comparator function to order Students by last name / first name 3 * in alphabetic order 4 */ 5 int studentbynamecmp ( const void * s1, const void * s2) { 45
46 6 7 const Student * a = ( const Student *) s1; 8 const Student * b = ( const Student *) s2; 9 int result = strcmp (a-> lastname, b-> lastname ); 10 if( result == 0) { 11 return strcmp (a-> firstname, b-> firstname ); 12 } else { 13 return result ; 14 } 15 } /** 18 * A comparator function to order Students by last name / first name 19 * in reverse alphabetic order 20 */ 21 int studentbynamecmpdesc ( const void *s1, const void *s2) { int result = studentbynamecmp ( s1, s2); 24 return -1 * result ; 25 } /* 28 Comparator function that orders by NUID in ascending 29 numerical order 30 */ 31 int studentidcmp ( const void * s1, const void * s2) { const Student * a = ( const Student *) s1; 34 const Student * b = ( const Student *) s2; 35 return (a-> nuid - b-> nuid ); } E. Master Theorem Theorem 1 (Master Theorem). Let T (n) be a monotonically increasing function that satisfies ( n ) T (n) = at + f(n) b where f(n) O(n d ) (that is, f is bounded by some polynomial) and a 1, b 2, d > 0. O(n d ) if a < b d T (n) = O(n d log n) if a = b d O(n log b a ) if a > b d 46
47 References [1] Joshua Bloch. Extra, extra - read all about it: Nearly all binary searches and mergesorts are broken. extra-extra-read-all-about-it-nearly.html, [2] C. A. R. Hoare. Algorithm 64: Quicksort. Commun. ACM, 4(7):321, July [3] C. A. R. Hoare. Quicksort. The Computer Journal, 5(1):10 16, [4] Donald E. Knuth. Von neumann s first computer program. ACM Comput. Surv., 2(4): , December [5] Richard E. Pattis. Textbook errors in binary searching. In Proceedings of the Nineteenth SIGCSE Technical Symposium on Computer Science Education, SIGCSE 88, pages , New York, NY, USA, ACM. [6] Tim Peters. [Python-Dev] Sorting. Python-Dev mailing list, org/pipermail/python-dev/2002-july/ html, July [7] J. W. J. Williams. Algorithm 232: Heapsort. Communications of the ACM, 7(6): ,
Analysis of Binary Search algorithm and Selection Sort algorithm
Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to
6. Standard Algorithms
6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary
1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++
Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The
Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge
Algorithms. Margaret M. Fleck. 18 October 2010
Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o
Converting a Number from Decimal to Binary
Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive
Biostatistics 615/815
Merge Sort Biostatistics 615/815 Lecture 8 Notes on Problem Set 2 Union Find algorithms Dynamic Programming Results were very ypositive! You should be gradually becoming g y g comfortable compiling, debugging
A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:
Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)
UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming
UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming 1 2 Foreword First of all, this book isn t really for dummies. I wrote it for myself and other kids who are on the team. Everything
Binary Heap Algorithms
CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks [email protected] 2005 2009 Glenn G. Chappell
Java Interview Questions and Answers
1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java
Basic Programming and PC Skills: Basic Programming and PC Skills:
Texas University Interscholastic League Contest Event: Computer Science The contest challenges high school students to gain an understanding of the significance of computation as well as the details of
Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.
Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Today Registration should be done. Homework 1 due 11:59 pm next Wednesday, January 14 Review math essential
The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!
The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >
Informatica e Sistemi in Tempo Reale
Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)
Section IV.1: Recursive Algorithms and Recursion Trees
Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller
Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007
Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007 The Java Type System By now, you have seen a fair amount of Java. Time to study in more depth the foundations of the language,
Why? A central concept in Computer Science. Algorithms are ubiquitous.
Analysis of Algorithms: A Brief Introduction Why? A central concept in Computer Science. Algorithms are ubiquitous. Using the Internet (sending email, transferring files, use of search engines, online
Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search
Chapter Objectives Chapter 9 Search Algorithms Data Structures Using C++ 1 Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential
Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited
Hash Tables Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited We ve considered several data structures that allow us to store and search for data
Pseudo code Tutorial and Exercises Teacher s Version
Pseudo code Tutorial and Exercises Teacher s Version Pseudo-code is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy
Lecture Notes on Linear Search
Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is
Chapter 13: Query Processing. Basic Steps in Query Processing
Chapter 13: Query Processing! Overview! Measures of Query Cost! Selection Operation! Sorting! Join Operation! Other Operations! Evaluation of Expressions 13.1 Basic Steps in Query Processing 1. Parsing
Questions 1 through 25 are worth 2 points each. Choose one best answer for each.
Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A
1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.
1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated
CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.
Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure
Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., 2001. 1995-2001 Barnette ND, McQuain WD
1 Slides 1. Table of Contents 2. Definitions 3. Simple 4. Recursive Execution Trace 5. Attributes 6. Recursive Array Summation 7. Recursive Array Summation Trace 8. Coding Recursively 9. Recursive Design
Data Structure with C
Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which
Chapter 7: Sequential Data Structures
Java by Definition Chapter 7: Sequential Data Structures Page 1 of 112 Chapter 7: Sequential Data Structures We have so far explored the basic features of the Java language, including an overview of objectoriented
Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists
Lecture 11 Doubly Linked Lists & Array of Linked Lists In this lecture Doubly linked lists Array of Linked Lists Creating an Array of Linked Lists Representing a Sparse Matrix Defining a Node for a Sparse
Binary search algorithm
Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than
1 Abstract Data Types Information Hiding
1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content
Stacks. Linear data structures
Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations
Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example
Announcements Memory management Assignment 2 posted, due Friday Do two of the three problems Assignment 1 graded see grades on CMS Lecture 7 CS 113 Spring 2008 2 Safe user input If you use scanf(), include
Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)
Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse
Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays
Motivation Suppose that we want a program that can read in a list of numbers and sort that list, or nd the largest value in that list. To be concrete about it, suppose we have 15 numbers to read in from
CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting
CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)
Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.
Coding Rules Section A: Linux kernel style based coding for C programs Coding style for C is based on Linux Kernel coding style. The following excerpts in this section are mostly taken as is from articles
arrays C Programming Language - Arrays
arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)
Data Structures in the Java API
Data Structures in the Java API Vector From the java.util package. Vectors can resize themselves dynamically. Inserting elements into a Vector whose current size is less than its capacity is a relatively
MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C
Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection
AP Computer Science Java Subset
APPENDIX A AP Computer Science Java Subset The AP Java subset is intended to outline the features of Java that may appear on the AP Computer Science A Exam. The AP Java subset is NOT intended as an overall
Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC
Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Sorting Let elem be a type with a operation, which is a total order A vector
Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *
Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest
Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void
1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of
5 Arrays and Pointers
5 Arrays and Pointers 5.1 One-dimensional arrays Arrays offer a convenient way to store and access blocks of data. Think of arrays as a sequential list that offers indexed access. For example, a list of
Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A
Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A Developed By Brian Weinfeld Course Description: AP Computer
Unit 1. 5. Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]
Unit 1 1. Write the following statements in C : [4] Print the address of a float variable P. Declare and initialize an array to four characters a,b,c,d. 2. Declare a pointer to a function f which accepts
Analysis of a Search Algorithm
CSE 326 Lecture 4: Lists and Stacks 1. Agfgd 2. Dgsdsfd 3. Hdffdsf 4. Sdfgsfdg 5. Tefsdgass We will review: Analysis: Searching a sorted array (from last time) List ADT: Insert, Delete, Find, First, Kth,
Sources: On the Web: Slides will be available on:
C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,
Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic
1 Algorithm Analysis []: if-else statements, recursive algorithms COSC 011, Winter 004, Section N Instructor: N. Vlajic Algorithm Analysis for-loop Running Time The running time of a simple loop for (int
Sequential Data Structures
Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year
CS 111 Classes I 1. Software Organization View to this point:
CS 111 Classes I 1 Software Organization View to this point: Data Objects and primitive types Primitive types operators (+, /,,*, %). int, float, double, char, boolean Memory location holds the data Objects
What Is Recursion? Recursion. Binary search example postponed to end of lecture
Recursion Binary search example postponed to end of lecture What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call Direct recursion Recursion
Introduction to Java
Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1 Java Overview A high
Why you shouldn't use set (and what you should use instead) Matt Austern
Why you shouldn't use set (and what you should use instead) Matt Austern Everything in the standard C++ library is there for a reason, but it isn't always obvious what that reason is. The standard isn't
Semantic Analysis: Types and Type Checking
Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors
Chapter 5 Names, Bindings, Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named
Data Structures and Data Manipulation
Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval
Symbol Tables. Introduction
Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The
Object Oriented Software Design
Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 14, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction
TREE BASIC TERMINOLOGIES
TREE Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing hierarchical relationship between the grand father and his children and
Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms
Name : Jariya Phongsai Class : MAC 286. Data Structure Research Paper on Sorting Algorithms Prof. Lawrence Muller Date : October 26, 2009 Introduction In computer science, a ing algorithm is an efficient
Sample Questions Csci 1112 A. Bellaachia
Sample Questions Csci 1112 A. Bellaachia Important Series : o S( N) 1 2 N N i N(1 N) / 2 i 1 o Sum of squares: N 2 N( N 1)(2N 1) N i for large N i 1 6 o Sum of exponents: N k 1 k N i for large N and k
10CS35: Data Structures Using C
CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a
Lecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative
Loop Invariants and Binary Search
Loop Invariants and Binary Search Chapter 4.3.3 and 9.3.1-1 - Outline Ø Iterative Algorithms, Assertions and Proofs of Correctness Ø Binary Search: A Case Study - 2 - Outline Ø Iterative Algorithms, Assertions
Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C
Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate
Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb
Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations
C++ Programming Language
C++ Programming Language Lecturer: Yuri Nefedov 7th and 8th semesters Lectures: 34 hours (7th semester); 32 hours (8th semester). Seminars: 34 hours (7th semester); 32 hours (8th semester). Course abstract
APP INVENTOR. Test Review
APP INVENTOR Test Review Main Concepts App Inventor Lists Creating Random Numbers Variables Searching and Sorting Data Linear Search Binary Search Selection Sort Quick Sort Abstraction Modulus Division
CompSci-61B, Data Structures Final Exam
Your Name: CompSci-61B, Data Structures Final Exam Your 8-digit Student ID: Your CS61B Class Account Login: This is a final test for mastery of the material covered in our labs, lectures, and readings.
CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013
Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100)
http://algs4.cs.princeton.edu dictionary find definition word definition book index find relevant pages term list of page numbers
ROBERT SEDGEWICK KEVI WAYE F O U R T H E D I T I O ROBERT SEDGEWICK KEVI WAYE ROBERT SEDGEWICK KEVI WAYE Symbol tables Symbol table applications Key-value pair abstraction. Insert a value with specified.
Quick Introduction to Java
Quick Introduction to Java Dr. Chris Bourke Department of Computer Science & Engineering University of Nebraska Lincoln Lincoln, NE 68588, USA Email: [email protected] 2015/10/30 20:02:28 Abstract These
Data Structures Fibonacci Heaps, Amortized Analysis
Chapter 4 Data Structures Fibonacci Heaps, Amortized Analysis Algorithm Theory WS 2012/13 Fabian Kuhn Fibonacci Heaps Lacy merge variant of binomial heaps: Do not merge trees as long as possible Structure:
Lecture 3: Finding integer solutions to systems of linear equations
Lecture 3: Finding integer solutions to systems of linear equations Algorithmic Number Theory (Fall 2014) Rutgers University Swastik Kopparty Scribe: Abhishek Bhrushundi 1 Overview The goal of this lecture
Quick Sort. Implementation
Implementation Next, recall that our goal is to partition all remaining elements based on whether they are smaller than or greater than the pivot We will find two entries: One larger than the pivot (staring
Output: 12 18 30 72 90 87. struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;
50 20 70 10 30 69 90 14 35 68 85 98 16 22 60 34 (c) Execute the algorithm shown below using the tree shown above. Show the exact output produced by the algorithm. Assume that the initial call is: prob3(root)
Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.
Arrays Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html 1 Grid in Assignment 2 How do you represent the state
11 Multivariate Polynomials
CS 487: Intro. to Symbolic Computation Winter 2009: M. Giesbrecht Script 11 Page 1 (These lecture notes were prepared and presented by Dan Roche.) 11 Multivariate Polynomials References: MC: Section 16.6
PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?
1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members
Introduction to Programming System Design. CSCI 455x (4 Units)
Introduction to Programming System Design CSCI 455x (4 Units) Description This course covers programming in Java and C++. Topics include review of basic programming concepts such as control structures,
Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.
Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java
16 Collection Classes
16 Collection Classes Collections are a key feature of the ROOT system. Many, if not most, of the applications you write will use collections. If you have used parameterized C++ collections or polymorphic
Figure 1: Graphical example of a mergesort 1.
CSE 30321 Computer Architecture I Fall 2011 Lab 02: Procedure Calls in MIPS Assembly Programming and Performance Total Points: 100 points due to its complexity, this lab will weight more heavily in your
AP Computer Science Java Mr. Clausen Program 9A, 9B
AP Computer Science Java Mr. Clausen Program 9A, 9B PROGRAM 9A I m_sort_of_searching (20 points now, 60 points when all parts are finished) The purpose of this project is to set up a program that will
Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction
Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know
IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS
Volume 2, No. 3, March 2011 Journal of Global Research in Computer Science RESEARCH PAPER Available Online at www.jgrcs.info IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE
The C Programming Language course syllabus associate level
TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming
1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders
1.00 Lecture 1 Course Overview Introduction to Java Reading for next time: Big Java: 1.1-1.7 Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders
Glossary of Object Oriented Terms
Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction
Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R
Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent
Chapter 15 Functional Programming Languages
Chapter 15 Functional Programming Languages Introduction - The design of the imperative languages is based directly on the von Neumann architecture Efficiency (at least at first) is the primary concern,
CISC 181 Project 3 Designing Classes for Bank Accounts
CISC 181 Project 3 Designing Classes for Bank Accounts Code Due: On or before 12 Midnight, Monday, Dec 8; hardcopy due at beginning of lecture, Tues, Dec 9 What You Need to Know This project is based on
Chapter 2: Elements of Java
Chapter 2: Elements of Java Basic components of a Java program Primitive data types Arithmetic expressions Type casting. The String type (introduction) Basic I/O statements Importing packages. 1 Introduction
