MSIS 655 Advanced Business Applications Programming Week 12 Searching and Sorting (Ch. 16) In this chapter you will learn: To search for a given value in an array using linear search and binary search. To sort arrays using the iterative selection and insertion sort algorithms. To sort arrays using the recursive merge sort algorithm. To determine the efficiency of searching and sorting algorithms. 12/7/2018 12.1
Introduction Searching Sorting Determining whether a search key is present in data Sorting Places data in order based on one or more sort keys In chapter 17 (Data Structure), the linked Lists (nodes) class and linear search function on the list are explained. Also, class Collections (chapter 19) has a method, binarySearch( list ). Those are class specific functions. What I am trying here is more on the logics to figure out the functions. Examples of searching Looking up a phone number Accessing a Web site Checking a word in the dictionary 12/7/2018
Searching Algorithms Linear search Searches each element sequentially If search key is not present Tests each element When algorithm reaches end of array, informs user search key is not present If search key is present Test each element until it finds a match 12/7/2018
LinearArray.java (Fig. 16.2 p. 788) 12/7/2018
Efficiency of Linear Search Big O Notation Indicates the worst-case run time for an algorithm O(1): Constant O(n): Linear O(n2): Quadratic Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realize maximum performance. 12/7/2018
Binary Search More efficient than linear search Requires elements to be sorted Tests the middle element in an array If it is the search key, algorithm returns Otherwise, if the search key is smaller, eliminates larger half of array If the search key is larger, eliminates smaller half of array Each iteration eliminates half of the remaining elements 12/7/2018
BinaryArray.java (pp. 792-793) 12/7/2018
Efficiency of Binary Search Each comparison halves the size of the remaining array Results in O(log n) Called logarithmic run time 12/7/2018
Sorting Algorithms Sorting data Placing data into some particular order A bank sorts checks by account number Telephone companies sort accounts by name End result is always the same – a sorted array Choice of algorithm affects how you achieve the result and, most importantly, how fast you achieve the result 12/7/2018
Selection Sort Simple, but inefficient sorting algorithm First iteration selects smallest element in array and swaps it with the first element Each iteration selects the smallest remaining unsorted element and swaps it with the next element at the front of the array After i iterations, the smallest i elements will be sorted in the first i elements of the array 12/7/2018
(pp. 798-799) SelectionSort.java 12/7/2018 For each of the i’s in the first for loop, rest of the array is looked through using the second for loop, and the index of the smallest value is chosen as “smallest” (including ith element and forward). Swap (int i, int smallest) will swap the value of the Array elements data[ i ] and data[ smallest ]. – data[ i ] becomes the smallest of the array (I’th and forward). 12/7/2018
Efficiency of Selection Sort Outer for loop iterates over n – 1 elements Inner for loop iterates over remaining elements in the array Results in O(n2) 12/7/2018
Insertion Sort Another simple, but inefficient sorting algorithm First pass takes the second element and inserts it into the correct order with the first element Each iteration takes the next element in the array and inserts it into the sorted elements at the beginning of the array After i iterations, the first i elements of the array are in sorted order 12/7/2018
12/7/2018
Merge Sort Combination of Sort algorithm and recursion To be continued… 12/7/2018
Lab activities (Week 12) Exercises (pp. 814-815) 16.5, 16.6 Advanced: 16.7 12/7/2018