



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
// Searches the specified array of objects using a linear search // //----------------------------------------------------------------- algorithm. Returns null if the target is not found. public static Comparable linearSearch (Comparable[] data, Comparable target) { Comparable result = null; int index = 0; while (result == null && index < data.length) { if (data[index].compareTo(target) == 0) result = data[index]; index++; } return result; } Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
// Searches the specified array of objects using a binary search // //----------------------------------------------------------------- algorithm. Returns null if the target is not found. public static Comparable binarySearch (Comparable[] data, Comparable target) { Comparable result = null; int first = 0, last = data.length-1, mid; while (result == null && first <= last) { mid = (first + last) / 2; // determine midpoint if (data[mid].compareTo(target) == 0) result = data[mid]; else if (data[mid].compareTo(target) > 0) last = mid - 1; else first = mid + 1; } return result; }
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
//******************************************************************** // SortPlayerList.java Java Foundations // // Demonstrates a selection sort of Comparable objects. //******************************************************************** public { class SortPlayerList //----------------------------------------------------------------- // Creates an array of Contact objects, sorts them, then prints // //----------------------------------------------------------------- them. public { static void main (String[] args) Contact[] players = new Contact[7]; players[0] players[1] == newnew ContactContact ("Rodger", "Federer", "610-555-7384");("Andy", "Roddick", "215-555-3827"); players[2] players[3] == newnew ContactContact ("Maria", "Sharapova", "733-555-2969");("Venus", "Williams", "663-555-3984"); players[4] players[5] == newnew ContactContact ("Lleyton",("Eleni", "Daniilidou", "Hewitt", "464-555-3489"); "322-555-2284"); players[6] = new Contact ("Serena", "Williams", "243-555-2837"); Sorting.selectionSort(players); for (^) System.out.println(Comparable player : (player); players) }^ } Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
// Contact.java Java Foundations //******************************************************************** public class Contact implements Comparable { private String firstName, lastName, phone; public Contact (String first, String last, String telephone) { firstName = first; lastName = last; phone = telephone; } public int compareTo (Object other) { int result; if (lastName.equals(((Contact)other).lastName)) result = firstName.compareTo(((Contact)other).firstName); else result = lastName.compareTo(((Contact)other).lastName); return result; } (…) (^) Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
// Sorts the specified array of objects using an insertion // //----------------------------------------------------------------- sort algorithm. public static void insertionSort (Comparable[] data) { for (int index = 1; index < data.length; index++) { Comparable key = data[index]; int position = index; // Shift larger values to the right while (position > 0 && data[position-1].compareTo(key) > 0) { data[position] = data[position-1]; position--; } data[position] = key; } } Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
// Sorts the specified array of objects using a bubble sort // //----------------------------------------------------------------- algorithm. public static void bubbleSort (Comparable[] data) { int position, scan; for (position = data.length - 1; position >= 0; position--) { for (scan = 0; scan <= position - 1; scan++) if (data[scan].compareTo(data[scan+1]) > 0) swap (data, scan, scan+1); } } Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
// Sorts the specified array of objects using the quick sort // algorithm. //----------------------------------------------------------------- public static void quickSort (Comparable[] data, int min, int max) { int pivot; if (min < max) { pivot = partition (data, min, max); // make partitions quickSort(data, min, pivot-1); // sort left partition quickSort(data, pivot+1, max); // sort right partition } } Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
// (^) privateCreates static the partitions int partition needed (Comparable[] for quick sort. data, int min, int max) { // Use first element as the partition value Comparable partitionValue = data[min]; int left = min; int right = max; while { (left < right) // Search for an element that is > the partition element while left++; (data[left].compareTo(partitionValue) <= 0 && left < right) // Search while (data[right].compareTo(partitionValue) for an element that is < the partitionelement > 0) right--; if (left < right) swap(data, left, right); } // swap (data, min, Move the partition element right); to its final position return right; // That will become the pivot } Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley
Copyright © 2007 Pearson Education, Inc. Publishing s Pearson Addison-Wesley