Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 14: Search and Sorting Yonglei Tao.

Similar presentations


Presentation on theme: "Ch 14: Search and Sorting Yonglei Tao."— Presentation transcript:

1 Ch 14: Search and Sorting Yonglei Tao

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

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

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

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

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

7 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

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

9 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];

10 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

11 Merge Sort vs. Selection Sort (Cont.)

12 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

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

14 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", )); countries.add (new Country("Thailand", )); 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()); }

15 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();

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

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

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

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

20 Comparison of Sorting Algorithms


Download ppt "Ch 14: Search and Sorting Yonglei Tao."

Similar presentations


Ads by Google