Computer Science Searching & Sorting
Searching and Sorting As you know computers are fantastic machines for completing repetitive tasks They are really good at Searching - looking for things Sorting –organizing things Many techniques searching and sorting techniques have developed as computers have evolved We are going to look at just a few of these techniques Since these techniques all require a series of steps they are called algorithms
Types of Searches & Sorts Sequential or Linear Searches Binary Searches Sorts Insertion Sorts Selection Sorts Bubble Sorts Shell Sorts
Sequential Search Looking for something in a systematic way Algorithm: Start at first item Is it the item you are looking for? No Go to the next item Repeat until item is found
Example 1 – 10.1 pg 382 Carter The method performs a sequential search on an array of String values for the value called item. If the search is successful, the method returns the index in the array of item, if not it returns -1. public int seqSearch(String[] list, String item) { int location=-1 for(int i = 0; i < list.length; i++) if(list[i].equals(item)) location=i; return location; }
Binary Search The Binary search is also known as the divide and conquer search This requires the items to be sorting in some kind of order for example from smallest to largest Algorithm: Sort items in order Find the middle point Ignore half that does not contain item Find middle point of the half Repeat until item is found Note: Look at Example 1 pg 386 Carter
Binary Search list low item middle item high item list Is middle item what we are looking for? If not is it more or less than the target item? (Assume lower) list low middle high item item item and so forth…
Example 2 – 10.2 pg 388 Carter This examples uses a binary search for the value item in an array list of double value It will only keep search as long as bottom ≤ top public static int bsearch(double[] list, double item){ int location = -1; int bottom = 0; int top = list.length - 1; int mid; while( bottom == -1 && !found){ mid = (bottom+top)/2); if(list[mid] == item){ found=true; location=mid; } else if(list[mid]<item) bottom = mid + 1; else top = mid - 1; return result;
Types of Sorts Insertion Sort Selection Sort Bubble Sort Shellsort
Selection Sort On 1st pass, locate the smallest array element and swap it with the first array element On 2nd pass, locate the second smallest element and swap it with the second element of the array On the 3rd pass, locate the next smallest element and swap it with the third element of the array and so forth.... If the array contains N elements, it will be completely sorted after at most N–1 passes.
Insert & Selection Sort Both use the concept of swapping Insertion Sort - Algorithm Rearrange items from smallest to largest by comparing the first two items and swapping if the first item is greater than the second item Repeat the process for the next value Selection Sort - Algorithm Search through the list and find the smallest element swap the smallest element with the first element repeat starting at second element and find the second smallest element
Selection Sort Example Sort an array 5 random numbers ranging from 1-10. Example : Number are: 2 8 4 6 3 2 8 4 6 3 first iteration, 2 is already the lowest number and it is in first spot – do nothing 2 3 4 6 8 Next lowest number is 3 exchange with 8 4 is the lowest number – do nothing 6 is the lowest number after that - done
Selection Sort Example public static void selectionSort(int[] list){ int min; int temp; for(int i = 0; i < list.length - 1; i++){ min = i; for(int j = i + 1; j < list.length; j++) if(list[j] < list[min]) min = j; temp = list[i]; list[i] = list[min]; list[min] = temp; }
Bubble Sort Bubble Sort is very similar to Selection Sort The key difference is that it compares neighbours, not the smallest value. Algorithm Compare adjacent pairs of data. If the pairs are not in order, swap them. Continue until all data is processed.
Bubble Sort Example Sort an array 5 random numbers ranging from 1-10 Example : Numbers are: 2 8 4 6 3 First Pass 2 8 4 6 3 is 2 > 8 = No 2 4 8 6 3 is 8 > 4 = Yes - move 2 4 6 8 3 is 8 > 6 = Yes - move 2 4 6 3 8 is 8> 3 = Yes - done Second Pass 2 4 6 3 8 is 2 > 4 = No - leave 2 4 6 3 8 is 4 > 6 = No - leave 2 4 3 6 8 is 6 > 3 = Yes - move 2 4 3 6 8 is 6 > 8 = No - done Third Pass 2 4 3 6 8 is 2 > 4 = No – leave 2 4 3 6 8 is 4 > 3 = Yes - move 2 3 4 6 8 is 4 > 6 = No – leave 2 3 4 6 8 is 6 > 8 = No - done
ShellSort Created by Donald Shell in 1959 Wanted to stop moving data small distances (in the case of insertion sort and bubble sort) and stop making swaps that are not helpful (in the case of selection sort) Started with sub arrays created by looking at data that is far apart and then reduce the gap size
Shell Sort Example Gap of five –swap every 5 numbers lowest to highest 46 2 83 41 102 5 17 31 64 49 18 5 2 83 41 102 18 17 31 64 49 46 5 2 83 41 102 17 18 31 64 49 46 5 2 31 41 102 18 17 83 64 49 46 5 2 31 41 49 18 17 83 64 102 46 5 2 31 41 49 18 17 83 64 102 46 5 2 17 41 31 18 46 83 49 102 64 5 2 17 18 31 41 46 83 49 102 64 2 5 17 18 31 41 46 49 64 83 102 Sort 46, 5 18 Swap lowest to highest Sort 2, 17 No swap already in order Sort 83, 31 Swap Sort 41, 64 No swap Sort 102,49 Swap – done for gap 5 Gap remaining 2 Sort 5,31, 49, 17, 64,46 Gap 2 Sort 2,41, 18, 83, 102 Gap 1 Use insertion sort
Quick Sort Invented by C.A.R. (Tony) Hoare A divide and conquer approach that uses recursion If the list has 0 or 1 elements it is sorted otherwise, pick any element p in the list. This is called the pivot value Partition the list minus the pivot into two sub lists according to values less than or greater than the pivot. (equal values go to either) return the quicksort of the first list followed by the quicksort of the second list
Merge Sort Algorithm If a list has 1 element or 0 elements it is sorted If a list has more than 2 split into 2 separate lists Perform this algorithm on each of those smaller lists Take the 2 sorted lists and merge them together One of the oldest algorithm developed Jon von Neumann
Merge Sort CS 307 Fundamentals of Computer Science Sorting and Searching