Presentation is loading. Please wait.

Presentation is loading. Please wait.

SEARCHING AND SORTING.

Similar presentations


Presentation on theme: "SEARCHING AND SORTING."— Presentation transcript:

1 SEARCHING AND SORTING

2 Searching and Sorting on AP Exam Searching Sorting
Sequential (Linear) Binary Selection Insertion Merge

3 Searching

4 Searching Examples Suppose you have a telephone book and you want to search for a person’s telephone number. You know the person’s first and last name. How difficult is this? Suppose you have the same telephone book and you want to find a person that has a certain telephone number. How would you find it? Why is this more difficult?

5 Linear / Sequential Search
The Linear Search searches through a list sequentially (one element at time) looking for a match. The index position of a match is returned if found or -1 is returned if no match is found. It must go through the entire list in order to determine if there is no match.

6 Linear Search with Primitives
public int linearSearch(int[] nums, int val) { for(int i=0; i< nums.length; i++) if (nums[i] == val ) return i; } return -1; //returns -1 if not found

7 Linear Search with Strings
// Suppose we have an array of names (Strings) public int linearSearch(String[] ray, String searchValue) { for(int i=0; i<ray.length; i++) if (ray[i].equals(searchValue)) // OR if (ray[i].compareTo(searchValue)==0) return i; } return -1; //returns -1 if not found // Note: same as linear search with primitives except // you use equals method instead of ==

8 Linear Search with Objects
// Can be generalized to work with any object, not // only Strings. Just substitute Object for String in // the parameter list public int linearSearch(Object[] ray, Object searchValue) { for(int i=0; i<ray.length; i++) if (ray[i].equals(searchValue)) // OR if (ray[i].compareTo(searchValue)==0) return i; } return -1; //returns -1 if not found

9 Summary of Linear / Sequential Search
The Linear/Sequential Search works fine if the array is relatively small (a few hundred elements). However, if the array is large, it could become slow. Average number of elements searched is n/2, where n is the array length. This search method works fine when the array is sorted or even when it is not sorted.

10 open LinearSearch.java LinearSearchTester.java

11 Hint: Sort the array by using Arrays.sort(arrayName);
Work on NumberSearch class Hint: Sort the array by using Arrays.sort(arrayName);

12 Binary Search

13 Binary Search The method of linear search works well for arrays that are fairly small (a few hundred elements). As the array gets very large (thousands or millions of elements), the behavior of the search degrades. When we have an array of elements that are in ascending order, such as a list of numbers or names, there is a much better way to proceed, using an algorithm known as binary search. This method is much faster than linear search for very large arrays.

14 Binary Search The Binary Search is only guaranteed to work with sorted lists (normally in ascending order). The basic idea of binary search is to examine the element at the array's midpoint on each pass through the search loop. If the current element matches the search value, we return its position If the current element is less than the search value, then we search the part of the array to the right of the midpoint (containing the positions of the greater items).

15 Binary Search Otherwise, we search the part of the array to the left of the midpoint (containing the positions of the lesser items). On each pass through the loop, the current leftmost position or the current rightmost position is adjusted to track the portion of the array being searched.

16 Binary Search public int binarySearch (int [] nums, int searchValue )
{ int bot= 0, top = nums.length-1; while(bot<=top) int middle = (bot + top) / 2; if (nums[middle] == searchValue ) return middle; else if (nums[middle] > searchValue ) top = middle-1; bot = middle+1; } return -1;

17 BinarySearch int[] nums = {1, 6, 8, 10, 14, 22, 30, 50}; If you are searching for 25, how many times will you check nums? 1st pass 0 + 7 = 7 / 2 = 3 nums[3] = 10 2nd pass 4 + 7 = 11 / 2 = 5 nums[5] = 22 3rd pass 6 + 7 = 13 / 2 = 6 nums[6] = 30

18 Binary Search ShortCut
Given a list of N items. What is the next largest power of 2? If N is 100, the next largest power of 2 is 7. Log2(100) = 27 = 128. It would take 7 checks max to determine if an item existed in a list of 100 items.

19 Binary Search Example Have 3 volunteers come to the front of the room and look at 3 different telephone books Each volunteer should report the number of pages in the book for that city Each volunteer should look for the same name (for example David Daniels) by simulating a binary search Report how many times they have to check a page before finding the page that contains the name

20 open BinarySearch.java BinarySearchTester.java

21 The Selection Sort

22 Selection Sort Selection sort swaps the current element with the lowest element from the remaining elements in the list. Selection Sort does not swap each time it finds elements out of position. Selection sort makes a complete pass while searching for the next item to swap. At the end of a pass once the item is located, one swap is made.

23 Array length is 5. Number of passes = 5 – 1 = 4
Selection Sort original 9 2 8 5 1 pass 1 1 2 8 5 9 pass 2 1 2 8 5 9 pass 3 1 2 5 8 9 Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop. pass 4 1 2 5 8 9 Array length is 5. Number of passes = 5 – 1 = 4

24 Selection Sort public void selectionSort( int[] ray ) {
for(int i=0; i< ray.length-1; i++) { int min = i; //min = location of lowest value for(int j = i+1; j< ray.length; j++) if(ray[j] < ray[min]) min = j; //find location of lowest value } if( min != i) int temp = ray[min]; ray[min] = ray[i]; ray[i] = temp; //put lowest value in pos i Selection sort is pretty effective for small lists, but pretty horrible if used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop.

25 How many swaps per pass? Selection Sort W/Objects (ascending)
public void selSort(Car[] cars) { for(int i=0; i<cars.length-1; i++) int spot = i; for(int j=i+1; j<cars.length; j++) if(cars[j].compareTo(cars[spot])<0) spot = j; } if(spot==i) continue; //skip remaining lines; go to top of loop Car save = cars[i]; cars[i] = cars[spot]; cars[spot] = save; How many swaps per pass? When using a sort an against object, the object needs to have extended the Comparable interface. The Object class needs to redefine the compareTo() method so that the sort knows how to compare objects. The Comparable interface ony contains one method: compareTo() Selection Sort W/Objects (ascending)

26 Selection Sort W/Objects (descending)
public void selSort(Car[] cars) { for(int i=0; i<cars.length-1; i++) int spot = i; for(int j=i+1; j<cars.length; j++) if(cars[j].compareTo(cars[spot])>0) spot = j; } if(spot==i) continue; //skip remaining lines; go to top of loop Car save = cars[i]; cars[i] = cars[spot]; cars[spot] = save; When using a sort an against object, the object needs to have extended the Comparable interface. The Object class needs to redefine the compareTo() method so that the sort knows how to compare objects. The Comparable interface ony contains one method: compareTo() Selection Sort W/Objects (descending)

27 Selection Sort in Action
(descending order) Original List Integer[] ray = {90,40,20,30,10,67}; pass pass pass pass pass

28 Summary of Selection Sort
Selection sort is pretty effective for small lists, but pretty horrible if used on large lists Selection sort consists of two loops The outer loops run based on the number of items in the list The inner loop runs to find the items that need to be moved Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop.

29 Summary of Selection Sort
The inner loop either locates the spot with the smallest value or the spot with the largest value (depending on whether you are sorting it in ascending or descending order) After the inner loop completes, a swap may occur if needed At most, selection sort will make one swap per pass A pass is one complete execution of the inner loop Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop.

30 open SelectionSort.java SelectionSortTester.java

31 The Insertion Sort

32 Insertion Sort The insertion sort first selects an item and moves items up or down based on the comparison to the selected item. The idea is to get the selected item in proper position by shifting items around in the list. This is analogous to the way some people pick up playing cards and order them in their hands.

33 Insertion Sort 0 1 2 3 4 original 9 2 8 5 1 after pass 1 1 5 8 9 2
original 9 2 8 5 1 after pass 1 1 5 8 9 2 after pass 2 1 5 9 8 2 after pass 3 1 9 8 5 2 Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop. after pass 4 9 8 5 2 1 Blue signifies elements checked during the pass. They are sorted relative to each other. Highlighted element is the one inserted during the pass.

34 Insertion Sort w/primitives
public void insertionSort( int[] nums) { for (int i=1; i< nums.length; i++) int val = nums[i]; //item to insert int j=i; while(j>0 && val<nums[j-1]) nums[j] = nums[j-1]; j--; } nums[j]=val; Insertion Sort w/primitives

35 Insertion Sort w/Strings
public void insertionSort( String[] nums ) { for (int i=1; i< nums.length; i++) String val = nums[i]; //item to insert int j=i; while(j>0 && val.compareTo(nums[j-1])<0) nums[j] = nums[j-1]; j--; } nums[j]=val; Insertion Sort w/Strings

36 Sort Boxes By Volume public class Box implements Comparable {
private double volume; //constructor and other methods not listed public int compareTo(Object obj) { Box otherBox = (Box) obj; if (volume < otherBox.volume) return -1; if (volume == otherBox.volume) return 0; return 1; } Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop.

37 Insertion Sort Animation
Sorting cards: Sorting bars: Demo of Selection sort and insertion sort: Comparing many different sort algorithms (includes code): Racing different sort algorithms Java sorting overview: Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop.

38 Summary of Insertion Sort
Insertion Sort is normally more efficient than a Selection Sort Insertion Sort continually sorts the left side of the list while gradually moving to the end. This type of sort is similar to how many people sort cards in their hands Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop.

39 open InsertionSort.java InsertionSortTester.java

40 Java Sorts and Searches

41 frequently used methods for arrays
Arrays class frequently used methods for arrays Name Use sort(x) puts all items in array x in ascending order binarySearch(x,y) Prerequisite: array x must be sorted. Checks x for the location of y. This method returns the index where y is found. If y is not found, it returns -1 minus the position in the array list where x would be added. equals(x,y) checks if arrays x and y have same values fill(x, y) fills all spots in array x with value y The Arrays class methods above are very useful methods for manipulating Java arrays. sort() will naturally order the items in an array. binarySearch() will find an item in the array and return the spot at which the item was found. equals() will see if two arrays contain the exact same items in the exact same order. fill() will fill in all spots in the array with a provided value. This is the array sort method you should use in your programs. It uses a Quicksort algorithm.

42 frequently used methods for array lists
Collections class frequently used methods for array lists Name Use sort(x) puts all items in array list x in ascending order binarySearch(x,y) Prerequisite: array list x must be sorted. Checks x for the location of y. This method returns the index where y is found. If y is not found, it returns -1 minus the position in the array list where x would be added. fill(x,y) fills all spots in array list x with value y rotate(x,y) shifts items in array list x left or right y spots reverse(x) reverses the order of the items in array list x The Collections class methods above are very useful methods for manipulating Java Collections. sort() will naturally order the items in the collection. binarySearch() will find an item in the array and return the spot at which the item was found. fill() will fill in all spots in the array with a provided value. rotate() will shift items to the left(- negative x) a specified amount or shift items to the right(+ positive x) a specified amount. reverse() will reverse the order of all items. This is the sort method you should use for ArrayLists It uses a Mergesort algorithm.

43 Java Searches OUTPUT -1 2 -6 String s = "abcdefghijklmnop";
System.out.println(s.indexOf(“R")); int[] ray = {3,4,5,6,11,18,91}; System.out.println(Arrays.binarySearch(ray,5)); System.out.println(Arrays.binarySearch(ray,15)); indexOf() and binarySearch() seach and array for specified value. indexOf() will return the spot at which the item was found. It will return -1 if the item was not present. binarySearch() will return the spot at which the item was found. It will return -1 - location(where the item should be) if the item was not present.

44 Java Sorts OUTPUT-5 2 6 13 17 18 int[] ray = {13,6,17,18,2,-5};
Arrays.sort(ray); for(int i = 0; i < ray.length; i++) { System.out.println(ray[i]); } OUTPUT sort() will naturally order the items in an array. This sort uses the Quicksort sorting algorithm.

45 Java Sorts OUTPUT-1 2 3 13 21 ArrayList<Integer> ray;
ray=new ArrayList<Integer>(); ray.add(21); ray.add(2); ray.add(13); ray.add(-1); ray.add(3); Collections.sort(ray); for(int num : ray ) System.out.println(num); OUTPUT sort() will naturally order the items in the Collection.

46 Sorting Objects public class Athlete implements Comparable {
private String lastName, firstName; //constructor and other methods not listed public int compareTo(Object obj) { Athlete otherAthlete = (Athlete) obj; if (lastName.compareTo(other Athlete.lastName)<0) return -1; if (lastName.compareTo(other Athlete.lastName)>0) return 1; return 0; } Selection sort is pretty effective for small lists, but pretty horrible is used on large lists. Selection sort consists of two loops. The outer loops run based on the number of items in the list. The inner loop runs to find the items that need to be moved. The inner loop either locates the spot with the smallest value or the spot with the largest value. After the inner loop completes, a swap may occur if needed. At most, selection sort will make one swap per pass. A pass is one complete execution of the inner loop.

47 Sorting Objects ArrayList<Athlete> list; list=new ArrayList<Athlete>(); list.add(new Athlete(“Bob”,”Smith”)); list.add(new Athlete(“Tom”,”Jones”)); list.add(new Athlete(“Sue”,”Adams”)); list.add(new Athlete(“Joe”,”Bass”)); list.add(new Athlete(“Sara”,”Weiss”)); Collections.sort(list); for(Athlete athlete : list ) System.out.println(athlete); OUTPUT Sue Adams Joe Bass Tom Jones Bob Smith Sara Weiss sort() will naturally order the items in the Collection. Athlete object class needs to implement Comparable Have to redefine compareTo() method toString() method will make the print work as desired

48 Sorting an Array int[] intArray = {9, 2, 4, -12};
Arrays.sort(intArray); // [-12, 2, 4, 9] String[] strArray = {"y", "b", "H"}; Arrays.sort(strArray); // [H, b, y] // Case-insensitive sort Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER); // [b, H, y] // Reverse-order sort Arrays.sort(strArray, Collections.reverseOrder()); // [y, b, H] // Case-insensitive reverse-order sort Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER); Collections.reverse(Arrays.asList(strArray)); // [y, H, b] sort() will naturally order the items in an array. This sort uses the Quicksort sorting algorithm.

49 open SortArray.java

50 CashRegister Widget Corp. Store A Store B Store C Register 1
Money Money Money Money Money Money Money Money

51 i++ and ++i OUTPUT 10 10 20 OUTPUT 10 20 20 What is the difference?
i++ assigns i value before incrementing: int[] array = {10, 20, 30, 40}; i = 0; System.out.println(array[i]); System.out.println(array[i++]); ++i assigns i value after incrementing: int i = 0; System.out.println(array[++i]); OUTPUT OUTPUT sort() will naturally order the items in an array. This sort uses the Quicksort sorting algorithm.

52 Divide and Conquer Algorithms
32 16 16 8 8 8 8 4 4 4 4 4 4 4 4

53 The Merge Sort

54 Merge Sort Merge sort splits the list into smaller sections working its way down to groups of two or one. Once the smallest groups are reached, the merge method is called to organize the smaller lists. Merge copies from the sub list to a temp array. The items are put in the temp array in sorted order.

55 Merge Sort Merge sort chops in half repeatedly to avoid processing the whole list at once.

56 mergeSort Algorithm Collections.sort( ) uses mergeSort.
public void mergeSort(Comparable[] ray, int front, int back) { int mid = (front+back)/2; if(mid==front) return; mergeSort(ray, front, mid); mergeSort(ray, mid, back); merge(ray, front, back); } Collections.sort( ) uses mergeSort. Arrays.sort( ) uses mergeSort for objects.

57 public void merge(Comparable[] ray, int front, int back)
{ Comparable[] temp = new Comparable[back-front]; int i = front, j = (front+back)/2, k =0, mid =j; while( i<mid && j<back) if(ray[i].compareTo(ray[j])<0) temp[k++]= ray[i++]; else temp[k++]= ray[j++]; } while(i<mid) while(j<back) for(i = 0; i<back-front; ++i) ray[front+i]=temp[i]; Merge W/Objects

58 Merge Sort in Action Original List Integer[] ray = {90,40,20,30,67,10}; pass pass pass pass pass

59 The mergeSort method alone has a Log2N run time, but cannot be run without the merge method.
The merge method alone has an N run time and can be run without the mergeSort method. mergeSort Merge

60 open MergeSort.java

61 Racing the Sort Algorithms:
Speed Racing the Sort Algorithms:

62 Sort Algorithm Comparisons
Name Best Case Avg. Case Worst Selection Sort Insertion Sort Merge Sort @ If the data is sorted, Insertion sort should only make one pass through the list. If this case is present, Insertion sort would have a best case of O(n).


Download ppt "SEARCHING AND SORTING."

Similar presentations


Ads by Google