Ch 14: Search and Sorting Yonglei Tao.

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

Garfield AP Computer Science
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Searching and Sorting Algorithms Based on D. S
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Sorting Algorithms: Selection, Insertion and Bubble.
Chapter 18 Searching and Sorting
Sorting Text Read Shaffer, Chapter 7 Sorting O(N 2 ) sorting algorithms: – Insertion, Selection, Bubble O(N log N) sorting algorithms – HeapSort, MergeSort,
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fourteen: Sorting and Searching.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Sorting and Searching.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
Building Java Programs Chapter 13 Searching reading: 13.3.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Computer Science Searching & Sorting.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 8: Sorting and Searching Java Software Structures: Designing.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Chapter 9 Searching and Sorting
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
Chapter 15: Algorithms 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 15 Algorithms.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
1 Sorting and Searching "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
COS 312 DAY 24 Tony Gauvin. Ch 1 -2 Agenda Questions? Last Capstone Progress reports over due Assignment 6 corrected – 2 A, 1 B, 1 C, 1 F and 2 MIA’s.
Searching and Sorting Chapter 18 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
Chapter 14 Sorting and Searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Searching and Sorting 14ACEHRPT Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Fibonacci Sequence Fibonacci sequence is a sequence of numbers defined by f 1 = 1 f 2 = 1 f n = f n-1 + f n-2 First ten terms – 1, 1, 2, 3, 5, 8, 13, 21,
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Lecture 25: Searching and Sorting
Sorting Mr. Jacobs.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Data Structures I (CPCS-204)
Comparing Objects in Java
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 7 Single-Dimensional Arrays
Chapter 13: Searching and Sorting
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Lecture 14: binary search and complexity reading:
Building Java Programs
SORTING AND SEARCHING.
Chapter 19 Sorting and Searching
Outline Late Binding Polymorphism via Inheritance
Lecture 11 Searching and Sorting Richard Gesick.
Lecture 15: binary search reading:
QuickSort Previous slides based on ones by Ethan Apter & Marty Stepp
14 SORTING AND SEARCHING CHAPTER
CSE 143 Lecture 16 (A) Searching and Comparable
Sub-Quadratic Sorting Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 14 – Sorting and Searching
Algorithms: Design and Analysis
Module 8 – Searching & Sorting Algorithms
Workshop for CS-AP Teachers
Sorting and Searching -- Introduction
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Chapter 18 Searching and Sorting
Presentation transcript:

Ch 14: Search and Sorting Yonglei Tao

Linear Search /** Finds a value in an array, using the linear search algorithm. @param v the value to search @return the index at which the value occurs, or -1 if it does not occur in the array */ public int search(int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) return i; } return -1;

Binary Search /** A class for executing binary searches through an array. */ public class BinarySearcher { private int[] a; Constructs a BinarySearcher. @param anArray a sorted array of integers public BinarySearcher(int[] anArray) { a = anArray; }

public int search(int v) { int low = 0; int high = a.length - 1; while (low <= high) { int mid = (low + high) / 2; if (a[mid] == v) return mid; else if (a[mid] < v) low = mid + 1; else high = mid - 1; } return -1;

Insertion Sort void insertionsort (int [] list) { for (int i = 1; i < list.length; i++) int temp = list[i]; for (int j = i; j >= 1 && temp < list[j-1]; j--) list[j] = list[j-1]; list[j] = temp; } Running time: O(n2)

Selection Sort void selectionsort (int[] list) { int min;         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;                    swap (list[min], list[i]);         }     } Running time: O(n2)

Performance of Selection Sort Milliseconds 10,000 786 20,000 2,148 30,000 4,796 40,000 9,192 50,000 13,321 60,000 19,299 * Obtained with a Pentium processor, 2 GHz, Java 6, Linux

Merge Sort int [] A = new int[50]; void merge ( int, int, int ); void mergeSort ( int left, int right ) { int mid; if ( left < right ) { mid = ( left + right ) / 2; mergeSort ( left, mid ); mergeSort ( mid + 1, right ); merge ( left, mid, right ); } Running time: O(n log n)

void merge ( int left, int mid, int right ) { int p = left, q = mid + 1, k = left, int [] B = new int[50]; while ( p <= mid && q <= right ) { if (A[p] <= A[q]) B[k++] = A[p++]; else B[k++] = A[q++]; } while ( p <= mid ) while ( q <= right ) for ( k = left; k <= right; k++ ) A[k] = B[k];

Merge Sort vs. Selection Sort O(n log n) O(n2) n Merge Sort (milliseconds) Selection Sort (milliseconds) 10,000 40 786 20,000 73 2,148 30,000 134 4,796 40,000 170 9,192 50,000 192 13,321 60,000 205 19,299

Merge Sort vs. Selection Sort (Cont.)

Interface Comparable public interface Comparable { public int compareTo (Object other); } obj1.compareTo(obj2) < 0 if obj1 < obj2 obj1.compareTo(obj2) == 0 if obj1 == obj2 obj1.compareTo(obj2) > 0 if obj1 > obj2

An Example public class Country implements Comparable<Country> { private String name; private double area; public Country (String aName, double anArea) { name = aName; area = anArea; } public String getName () { return name; } public double getArea () { return area; } public int compareTo (Country other) { // compare two countries by area if (area < other.area) return -1; if (area > other.area) return 1; return 0;

An Example (Cont.) import java.util.*; public class CountrySortTester { public static void main (String[] args) { ArrayList<Country> countries = new ArrayList<Country>(); countries.add (new Country("Uruguay", 176220)); countries.add (new Country("Thailand", 514000)); countries.add (new Country("Belgium", 30510)); Collections.sort(countries); // sort the array list by area for (Country c : countries) System.out.println(c.getName() + " " + c.getArea()); }

Discussion: Class Shape public abstract class Shape implements Comparable<Shape> { private String name; public abstract double area (); public Shape( String shapeName ) { name = shapeName; } final public boolean compareTo ( Shape other ) { return this.area() - other.area(); final public String toString () { return name + " of area " + area();

Discussion: Class Point public abstract class Point implements Comparable<Point> { private int x, y; … final public boolean compareTo ( Point other ) { }

Exercise: Insertion Sort on Objects How to modify the code below to sort a list of Country objects? void insertionsort (int [] list) { for (int i = 1; i < list.length; i++) int temp = list[i]; for (int j = i; j >= 1 && temp < list[j-1]; j--) list[j] = list[j-1]; list[j] = temp; }

Quick Sort public static void quickSort (Comparable[] data, int min, int max) { int pivot; if (min < max) pivot = partition (data, min, max); quickSort(data, min, pivot-1); quickSort(data, pivot+1, max); }

The Partition Method private static int partition (Comparable[] data, int min, int max) { Comparable pivot = data[min]; int left = min, right = max; while (left < right) { while (data[left].compareTo(pivot) <= 0 && left < right) left++; while (data[right].compareTo(pivot) > 0) right--; if (left < right) swap(data, left, right); } swap (data, min, right); return right;

Comparison of Sorting Algorithms