Sequential (Linear) Binary Selection Insertion Merge.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
HST 952 Computing for Biomedical Scientists Lecture 9.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Searching and Sorting I 1 Searching and Sorting 1.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
1 Algorithm Efficiency and Sorting (Walls & Mirrors - Remainder of Chapter 9)
C++ Plus Data Structures
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
Algorithm Efficiency and Sorting
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 19 Searching, Sorting and Big O
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
Computer Science Searching & Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Chapter 9 Searching and Sorting
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
CSC 211 Data Structures Lecture 13
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
CSCI 51 Introduction to Programming March 12, 2009.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
3 – SIMPLE SORTING ALGORITHMS
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching and Sorting 14ACEHRPT Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Data Structures Arrays and Lists Part 2 More List Operations.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CSCI 51 Introduction to Programming March 10, 2009.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Sequential (Linear) Binary Selection** Insertion** Merge.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
SEARCHING AND SORTING.
Lecture 14 Searching and Sorting Richard Gesick.
CSC 222: Object-Oriented Programming
Chapter 20 Searching and Sorting
Chapter 13: Searching and Sorting
SORTING AND SEARCHING.
Outline Late Binding Polymorphism via Inheritance
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting 1-D Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Sorting and Searching -- Introduction
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Sequential (Linear) Binary Selection Insertion Merge

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?

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.

int linearSearch(int[] stuff, int val) { for(int i=0; i< stuff.length; i++) { if (stuff[i] == val ) return i; } return -1; //returns -1 if not found }

// Suppose we have an array of names (Strings) int linearSearch(String[] stuff, String searchValue) { for(int i=0; i<stuff.length; i++) { if (stuff[i].equals(searchValue)) // OR if (stuff[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 ==

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

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.

Hint: Sort the array by using Arrays.sort(arrayName);

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.

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).

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.

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

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

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. Log 2 (100) = = 128. It would take 7 checks max to determine if an item existed in a list of 100 items.

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

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.

original pass pass pass pass Array length is 5. Number of passes = 5 – 1 = 4

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 }

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; }

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; }

Original List Integer[] ray = {90,40,20,30,10,67}; pass pass pass pass pass

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 (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

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.

original after pass after pass after pass after pass Blue signifies elements checked during the pass. They are sorted relative to each other. Highlighted element is the one inserted during the pass.

void insertionSort( int[] stuff) { for (int i=1; i< stuff.length; i++) { int val = stuff[i]; //item to insert int j=i; while(j>0 && val<stuff[j-1]) { stuff[j] = stuff[j-1]; j--; } stuff[j]=val; }

void insertionSort( String[] stuff ) { for (int i=1; i< stuff.length; i++) { String val = stuff[i]; //item to insert int j=i; while(j>0 && val.compareTo(stuff[j-1])<0) { stuff[j] = stuff[j-1]; j--; } stuff[j]=val; }

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; }

Sorting cards: Sorting bars: Demo of Selection sort and insertion sort: E.html E.html Comparing many different sort algorithms: Racing different sort algorithms Java sorting overview:

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

Arrays class frequently used methods for arrays NameUse 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 This is the array sort method you should use in your programs. It uses a Quicksort algorithm.

Collections class frequently used methods for array lists NameUse 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 This is the sort method you should use for ArrayLists It uses a Mergesort algorithm.

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)); int[] ray = {3,4,5,6,11,18,91}; System.out.println(Arrays.binarySearch(ray,15)); OUTPUT

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

ArrayList ray; ray=new ArrayList (); 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

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; return 0; }

ArrayList list; list=new ArrayList (); 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 Athlete object class needs to implement Comparable Have to redefine compareTo() method toString() method will make the print work as desired

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]

Widget Corp. Store AStore BStore C Register 1 Register 2 MoneyMoney MoneyMoney MoneyMoney MoneyMoney MoneyMoney MoneyMoney MoneyMoney MoneyMoney

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++]); System.out.println(array[i]); ++i assigns i value after incrementing: int i = 0; System.out.println(array[i]); System.out.println(array[++i]); System.out.println(array[i]); OUTPUT OUTPUT

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.

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

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

void merge(Comparable[] stuff, 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(stuff[i].compareTo(stuff[j])<0) temp[k++]= stuff[i++]; else temp[k++]= stuff[j++]; } while(i<mid) temp[k++]= stuff[i++]; while(j<back) temp[k++]= stuff[j++]; for(i = 0; i<back-front; ++i) stuff[front+i]=temp[i]; }

Original List Integer[] stuff = {90,40,20,30,67,10}; pass pass pass pass pass

The mergeSort method alone has a Log 2 N 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.

Racing the Sort Algorithms:

NameBest CaseAvg. CaseWorst Selection Sort Insertion Sort Merge 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).