chapter 18 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CHAPTER 18 PowerPoint Presentation
Download Presentation
CHAPTER 18

Loading in 2 Seconds...

play fullscreen
1 / 84

CHAPTER 18 - PowerPoint PPT Presentation


  • 140 Views
  • Uploaded on

CHAPTER 18. SORTING AND SEARCHING. CHAPTER GOALS. To study the several searching and sorting algorithms To appreciate that algorithms for the same task can differ widely in performance To understand big-Oh notation To learn how to estimate and compare the performance of algorithms

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CHAPTER 18' - avent


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 18

CHAPTER 18

SORTING AND SEARCHING

chapter goals
CHAPTER GOALS
  • To study the several searching and sorting algorithms
  • To appreciate that algorithms for the same task can differ widely in performance
  • To understand big-Oh notation
  • To learn how to estimate and compare the performance of algorithms
  • To learn how to measure the running time of a program
sorting an array of integers
Sorting an Array of Integers
  • The selection sort algorithm repeatedly finds the smallest element in the unsorted tail region of the array and moves it to the front
sorting an array of integers4
Sorting an Array of Integers
  • Array in original order
  • Find the smallest and swap it with the first element
sorting an array of integers5
Sorting an Array of Integers
  • Find the next smallest. It is already in the correct place
  • Find the next smallest and swap it with the first element of unsorted portion
sorting an array of integers6
Sorting an Array of Integers
  • Repeat
  • When the unsorted portion is of length 1, we are done
file selectionsorter java
File SelectionSorter.java

01: /**

02: This class sorts an array, using the selection sort

03: algorithm

04: */

05: public class SelectionSorter

06: {

07: /**

08: Constructs a selection sorter.

09: @param anArray the array to sort.

10: */

11: public SelectionSorter(int[] anArray)

12: {

13: a = anArray;

14: }

15:

16: /**

17: Sorts the array managed by this selection sorter.

slide8
18: */

19: public void sort()

20: {

21: for (int i = 0; i < a.length - 1; i++)

22: {

23: int minPos = minimumPosition(i);

24: swap(minPos, i);

25: }

26: }

27:

28: /**

29: Finds the smallest element in a tail range of the array

30: @param from the first position in a to compare

31: @return the position of the smallest element in the

32: range a[from]...a[a.length - 1]

33: */

34: private int minimumPosition(int from)

35: {

36: int minPos = from;

37: for (int i = from + 1; i < a.length; i++)

slide9
38: if (a[i] < a[minPos]) minPos = i;

39: return minPos;

40: }

41:

42: /**

43: Swaps two entries of the array.

44: @param i the first position to swap

45: @param j the second position to swap

46: */

47: private void swap(int i, int j)

48: {

49: int temp = a[i];

50: a[i] = a[j];

51: a[j] = temp;

52: }

53:

54: private int[] a;

55: }

file selectionsorttest java
File SelectionSortTest.java

01: /**

02: This program tests the selection sort algorithm by

03: sorting an array that is filled with random numbers.

04: */

05: public class SelectionSortTest

06: {

07: public static void main(String[] args)

08: {

09: int[] a = ArrayUtil.randomIntArray(20, 100);

10: ArrayUtil.print(a);

11:

12: SelectionSorter sorter = new SelectionSorter(a);

13: sorter.sort();

14:

15: ArrayUtil.print(a);

16: }

17: }

file arrayutil java
File ArrayUtil.java

01: import java.util.Random;

02:

03: /**

04: This class contains utility methods for array

05: manipulation.

06: */

07: public class ArrayUtil

08: {

09: /**

10: Creates an array filled with random values.

11: @param length the length of the array

12: @param n the number of possible random values

13: @return an array filled with length numbers between

14: 0 and n-1

15: */

16: public static int[] randomIntArray(int length, int n)

17: { int[] a = new int[length];

slide12
18: Random generator = new Random();

19:

20: for (int i = 0; i < a.length; i++)

21: a[i] = generator.nextInt(n);

22:

23: return a;

24: }

25:

26: /**

27: Prints all elements in an array.

28: @param a the array to print

29: */

30: public static void print(int[] a)

31: {

32: for (int i = 0; i < a.length; i++)

33: System.out.print(a[i] + " ");

34: System.out.println();

35: }

36: }

profiling the selection sort algorithm
Profiling the Selection Sort Algorithm
  • We want to measure the time the algorithm takes to execute
    • Exclude the time the program takes to load
    • Exclude output time
profiling the selection sort algorithm14
Profiling the Selection Sort Algorithm
  • Create a StopWatch class to measure execution time of an algorithm
    • It can start, stop and give elapsed time
    • Use System.currentTimeMillis method
profiling the selection sort algorithm15
Profiling the Selection Sort Algorithm
  • Create a Stopwatch object
    • Start the stopwatch just before the sort
    • Stop the stopwatch just after the sort
    • Read the elapsed time
file stopwatch java
File StopWatch.java

01: /**

02: A stopwatch accumulates time when it is running. You can

03: repeatedly start and stop the stopwatch. You can use a

04: stopwatch to measure the running time of a program.

05: */

06: public class StopWatch

07: {

08: /**

09: Constructs a stopwatch that is in the stopped state

10: and has no time accumulated.

11: */

12: public StopWatch()

13: {

14: reset();

15: }

16:

17: /**

slide17
18: Starts the stopwatch. Time starts accumulating now.

19: */

20: public void start()

21: {

22: if (isRunning) return;

23: isRunning = true;

24: startTime = System.currentTimeMillis();

25: }

26:

27: /**

28: Stops the stopwatch. Time stops accumulating and is

29: is added to the elapsed time.

30: */

31: public void stop()

32: {

33: if (!isRunning) return;

34: isRunning = false;

35: long endTime = System.currentTimeMillis();

36: elapsedTime = elapsedTime + endTime - startTime;

37: }

slide18
38:

39: /**

40: Returns the total elapsed time.

41: @return the total elapsed time

42: */

43: public long getElapsedTime()

44: {

45: if (isRunning)

46: {

47: long endTime = System.currentTimeMillis();

48: elapsedTime = elapsedTime + endTime - startTime;

49: startTime = endTime;

50: }

51: return elapsedTime;

52: }

53:

54: /**

55: Stops the watch and resets the elapsed time to 0.

56: */

57: public void reset()

slide19
58: {

59: elapsedTime = 0;

60: isRunning = false;

61: }

62:

63: private long elapsedTime;

64: private long startTime;

65: private boolean isRunning;

66: }

file selectionsorttimer
File SelectionSortTimer

01: import javax.swing.JOptionPane;

02:

03: /**

04: This program measures how long it takes to sort an

05: array of a user-specified size with the selection

06: sort algorithm.

07: */

08: public class SelectionSortTimer

09: {

10: public static void main(String[] args)

11: {

12: String input = JOptionPane.showInputDialog(

13: "Enter array size:");

14: int n = Integer.parseInt(input);

15:

16: // construct random array

17:

slide21
18: int[] a = ArrayUtil.randomIntArray(n, 100);

19: SelectionSorter sorter = new SelectionSorter(a);

20:

21: // use stopwatch to time selection sort

22:

23: StopWatch timer = new StopWatch();

24:

25: timer.start();

26: sorter.sort();

27: timer.stop();

28:

29: System.out.println("Elapsed time: "

30: + timer.getElapsedTime() + " milliseconds");

31: System.exit(0);

32: }

33: }

selection sort on various size arrays24
Selection Sort on Various Size Arrays
  • Doubling the size of the array more than doubles the time needed to sort it
analyzing the selection sort algorithm
Analyzing the Selection Sort Algorithm
  • In an array of size n, count how many times an array element is visited
    • To find the smallest, visit n elements + 2 visits for the swap
    • To find the next smallest, visit (n-1) elements + 2 visits for the swap
    • The last term is 2 elements visited to find the smallest + 2 visits for the swap
analyzing the selection sort algorithm26
Analyzing the Selection Sort Algorithm
  • The number of visits:
    • n + 2 + (n-1) + 2 + (n-2) +2 + . . .+ 2 + 2
    • This can be simplified to n2 /2  +  n/2  - 3
    • n/2 - 3 is small compared to n2 /2 - so let's ignore it
    • also ignore the 1/2 - it divides out when comparing ratios
analyzing the selection sort algorithm27
Analyzing the Selection Sort Algorithm
  • The number of visits is of the order n2
  • Using big-Oh notation: The number of visits is O(n2)
  • Multiplying the number of elements in an array by 2 multiplies the processing time by 4
merge sort
Merge Sort
  • Divide an array in half and sort each half
merge sort29
Merge Sort
  • Merge the two sorted arrays into a single sorted array
merge sort30
Merge Sort
  • Dramatically faster than the selection sort.
file mergesorter java
File MergeSorter.java

01: /**

02: This class sorts an array, using the merge sort

03: algorithm

04: */

05: public class MergeSorter

06: {

07: /**

08: Constructs a merge sorter.

09: @param anArray the array to sort

10: */

11: public MergeSorter(int[] anArray)

12: {

13: a = anArray;

14: }

15:

16: /**

17: Sorts the array managed by this merge sorter

slide32
18: */

19: public void sort()

20: {

21: if (a.length <= 1) return;

22: int[] first = new int[a.length / 2];

23: int[] second = new int[a.length - first.length];

24: System.arraycopy(a, 0, first, 0, first.length);

25: System.arraycopy(a, first.length, second, 0, second.length);

26: MergeSorter firstSorter = new MergeSorter(first);

27: MergeSorter secondSorter = new MergeSorter(second);

28: firstSorter.sort();

29: secondSorter.sort();

30: merge(first, second);

31: }

32:

33: /**

34: Merges two sorted arrays into the array to be sorted by this

35: merge sorter.

36: @param first the first sorted array

37: @param second the second sorted array

slide33
38: */

39: private void merge(int[] first, int[] second)

40: {

41: // merge both halves into the temporary array

42:

43: int iFirst = 0;

44: // next element to consider in the first array

45: int iSecond = 0;

46: // next element to consider in the second array

47: int j = 0;

48: // next open position in a

49:

50: // as long as neither i1 nor i2 past the end, move

51: // the smaller element into a

52: while (iFirst < first.length && iSecond < second.length)

53: {

54: if (first[iFirst] < second[iSecond])

55: {

56: a[j] = first[iFirst];

57: iFirst++;

slide34
58: }

59: else

60: {

61: a[j] = second[iSecond];

62: iSecond++;

63: }

64: j++;

65: }

66:

67: // note that only one of the two while loops

68: // below is executed

69:

70: // copy any remaining entries of the first array

71: System.arraycopy(first, iFirst, a, j, first.length - iFirst);

72:

73: // copy any remaining entries of the second half

74: System.arraycopy(second, iSecond, a, j, second.length - iSecond);

75: }

76:

77: private int[] a;

78: }

file mergesorttest java
File MergeSortTest.java

01: /**

02: This program tests the merge sort algorithm by

03: sorting an array that is filled with random numbers.

04: */

05: public class MergeSortTest

06: {

07: public static void main(String[] args)

08: {

09: int[] a = ArrayUtil.randomIntArray(20, 100);

10: ArrayUtil.print(a);

11: MergeSorter sorter = new MergeSorter(a);

12: sorter.sort();

13: ArrayUtil.print(a);

14: }

15: }

merge sort timing vs selection sort
Merge Sort Timing Vs Selection Sort

Merge sort – rectangles

Selection sort - circles

analyzing merge sort algorithm
Analyzing Merge Sort Algorithm
  • In an array of size n, count how many times an array element is visited
  • Assume n is a power of 2:    n = 2m
  • Calculate the number of visits to create the two sub-arrays and then merge the two sorted arrays
  • 3 visits to merge each element or 3n visits
  • 2n visits to create the two sub-arrays
  • total of 5n visits
analyzing merge sort algorithm39
Analyzing Merge Sort Algorithm
  • Let T(n) denote the number of visits to sort an array of n elements then
    • T(n) = T(n/2) + T(n/2) + 5n or
    • T(n) = 2*T(n/2) + 5n
analyzing merge sort algorithm40
Analyzing Merge Sort Algorithm
  • The visits for an array of size n/2 is    T(n/2) = 2*T(n/4) + 5n/2
    • So T(n) = 2 * 2*T(n/4) +5n + 5n
  • The visits for an array of size n/4 is     T(n/4) = 2*T(n/8) + 5n/4
    • So T(n) = 2 * 2 * 2*T(n/8) + 5n + 5n + 5n
analyzing merge sort algorithm41
Analyzing Merge Sort Algorithm
  • Repeating the process k times:    T(n) = 2kT(n/2k) +5nk
  • since n = 2m, when k = m:    T(n) = 2mT(n/2m) +5nm
  • T(n) = nT(1) +5nm
  • T(n) = n + 5nlog2(n)
analyzing merge sort algorithm42
Analyzing Merge Sort Algorithm
  • To establish growth order
    • Drop the lower-order term n
    • Drop the constant factor 5
    • Drop the base of the logarithm since all logarithms are related by a common factor
    • We are left with n log(n)
analyzing merge sort algorithm43
Analyzing Merge Sort Algorithm
  • Using big-Oh notation: The number of visits is O( nlog(n) )
merge sort vs selection sort44
Merge Sort Vs Selection Sort
  • Selection sort is an O( n2 ) algorithm
  • Merge sort is an O( nlog(n) ) algorithm
  • The nlog(n) function grows much more slowly than n2
sorting in a java program
Sorting in a Java Program
  • The Arrays class contains static sort methods
  • To sort an array of integers

int[] intArray = . . . ;

Arrays.sort(intArray);

linear search
Linear Search
  • Also called sequential search
  • Examines all values in an array until it finds a match or reaches the end
  • Number of visits for a linear search of an array of n elements:
    • The average search visits n/2 elements
    • The maximum visits is n
  • A linear search is an O(n) algorithm
file linearsearcher java
File LinearSearcher.java

01: /**

02: A class for executing linear searches through an array.

03: */

04: public class LinearSearcher

05: {

06: /**

07: Constructs the LinearSearcher.

08: @param anArray an array of integers

09: */

10: public LinearSearcher(int[] anArray)

11: {

12: a = anArray;

13: }

14:

15: /**

16: Finds a value in an array, using the linear search

17: algorithm.

slide48
18: @param v the value to search

19: @return the index at which the value occurs, or -1

20: if it does not occur in the array

21: */

22: public int search(int v)

23: {

24: for (int i = 0; i < a.length; i++)

25: {

26: if (a[i] == v)

27: return i;

28: }

29: return -1;

30: }

31:

32: private int[] a;

33: }

file linearsearchtest java
File LinearSearchTest.java

01: import javax.swing.JOptionPane;

02:

03: /**

04: This program tests the linear search algorithm.

05: */

06: public class LinearSearchTest

07: {

08: public static void main(String[] args)

09: {

10: // construct random array

11:

12: int[] a = ArrayUtil.randomIntArray(20, 100);

13: ArrayUtil.print(a);

14: LinearSearcher searcher = new LinearSearcher(a);

15:

16: boolean done = false;

17: while (!done)

slide50
18: {

19: String input = JOptionPane.showInputDialog(

20: "Enter number to search for, Cancel to quit:");

21: if (input == null)

22: done = true;

23: else

24: {

25: int n = Integer.parseInt(input);

26: int pos = searcher.search(n);

27: System.out.println("Found in position " + pos);

28: }

29: }

30: System.exit(0);

31: }

32: }

binary search
Binary Search
  • Locates a value in a sorted array by
    • Determining whether the value occurs in the first or second half
    • Then repeating the search in one of the halves
binary search52
Binary Search
  • Count the number of visits to search an sorted array of size n
    • We visit one element (the middle element) then search either the left or right subarray
    • Thus:     T(n) = T(n/2) + 1
binary search53
Binary Search
  • If n is n/2, then      T(n/2) = T(n/4) + 1
  • Substituting into the original equation:     T(n) = T(n/4) + 2
  • This generalizes to:    T(n) = T(n/2k) + k
binary search54
Binary Search
  • Assume n is a power of 2,    n = 2m
  • Then:     T(n) = T(n/2m) + m
  • Since m = log2(n), then:    T(n) = 1 + log2(n)
binary search55
Binary Search
  • Binary search is an O( log(n) ) algorithm
file binarysearcher java
File BinarySearcher.java

01: /**

02: A class for executing binary searches through an array.

03: */

04: public class BinarySearcher

05: {

06: /**

07: Constructs a BinarySearcher.

08: @param anArray a sorted array of integers

09: */

10: public BinarySearcher(int[] anArray)

11: {

12: a = anArray;

13: }

14:

15: /**

16: Finds a value in a sorted array, using the binary

17: search algorithm.

slide57
18: @param v the value to search

19: @return the index at which the value occurs, or -1

20: if it does not occur in the array

21: */

22: public int search(int v)

23: {

24: int low = 0;

25: int high = a.length - 1;

26: while (low <= high)

27: {

28: int mid = (low + high) / 2;

29: int diff = a[mid] - v;

30:

31: if (diff == 0) // a[mid] == v

32: return mid;

33: else if (diff < 0) // a[mid] < v

34: low = mid + 1;

35: else

36: high = mid - 1;

37: }

slide58
38: return -1;

39: }

40:

41: private int[] a;

42: }

file binarysearchtest java
File BinarySearchTest.java

01: import java.util.Arrays;

02: import javax.swing.JOptionPane;

03:

04: /**

05: This program tests the binary search algorithm.

06: */

07: public class BinarySearchTest

08: {

09: public static void main(String[] args)

10: {

11: // construct random array

12:

13: int[] a = ArrayUtil.randomIntArray(20, 100);

14: Arrays.sort(a);

15: ArrayUtil.print(a);

16: BinarySearcher searcher = new BinarySearcher(a);

17:

slide60
18: boolean done = false;

19: while (!done)

20: {

21: String input = JOptionPane.showInputDialog(

22: "Enter number to search for, Cancel to quit:");

23: if (input == null)

24: done = true;

25: else

26: {

27: int n = Integer.parseInt(input);

28: int pos = searcher.search(n);

29: System.out.println("Found in position " + pos);

30: }

31: }

32: System.exit(0);

33: }

34: }

searching a sorted array in a program
Searching a Sorted Array in a Program
  • The Arrays class contains a static binarySearch method
  • The method returns either
    • The index of the element, if element is found
    • Or -k - 1 where k is the position before which the element should be inserted
searching a sorted array in a program62
Searching a Sorted Array in a Program
  • To search an integer array

int[] intArray = { 1, 4, 9 };

int v = 7;

int pos = Arrays.binarySearch(intArray, v);

//returns -3;

//v should be inserted before position 2

sorting and searching arrays of objects
Sorting and Searching Arrays of Objects
  • The Arrays class contain methods for searching or sorting collections of objects that implement the Comparable interface
sorting and searching arrays of objects64
Sorting and Searching Arrays of Objects
  • Comparable interface

public interface Comparable

{

int compareTo(Object otherObject);

}

sorting and searching arrays of objects65
Sorting and Searching Arrays of Objects
  • The call    a.compareTo(b)
    • Returns a negative number is a should come before b
    • Returns 0 if a and b are the same
    • Returns a positive number otherwise
compareto for bankaccount
CompareTo for BankAccount

public class BankAccount

{

. . .

public int compareTo(Object otherObject)

{

BankAccount other = (BankAccount)otherObject;

if (balance < other.balance) return -1;

if (balance == other.balance return 0;

return 1;

}

. . .

}

compareto method
CompareTo Method
  • The implementation must define a total ordering relationship
    • Antisymmetric
    • Reflexive
    • Transitive
compareto method68
CompareTo Method
  • Antisymmetric:

sign( x.compareTo(y) ) = -sign(y.compareTo(x) )

compareto method69
CompareTo Method
  • Reflexive :

x.compareTo(x) = 0

compareto method70
CompareTo Method
  • Transitive :

If x.compareTo(y) <= 0 and y.compareTo(Z) <= 0, then x.compareTo(z) <= 0

sorting and searching using comparator
Sorting and Searching Using Comparator
  • The Arrays class contains a sort method that can sort array of objects that implement the Comparator interface
sorting and searching using comparator72
Sorting and Searching Using Comparator
  • The Comparator interface

public interface Comparator

{

    • public int compare( Object firstObject, Object secondObject);

}

sorting and searching using comparator73
Sorting and Searching Using Comparator
  • If comp is a Comparator object, then the call comp.compare(a, b)
    • Returns a negative number if a should come before b
    • Returns 0 if a and b are the same
    • Returns a positive number otherwise
comparator class for coins
Comparator Class for Coins

public class CoinComparator implements Comparator

{

public int compare(Object firstObject, Object secondObject)

{

Coin first = (Coin)firstObject;

Coin second = (Coin)secondObject;

if ( first.getValue() < second.getValue() )

return -1;

if ( first.getValue() == second.getValue() )

return 0;

return 1;

}

}

sorting an array of objects
Sorting an Array of Objects
  • Using a Comparator :

Coin[] s = . . . ;

Comparator comp = new CoinComparator();

Arrays.sort(coinArray, comp);

sorting and searching arraylists
Sorting and Searching ArrayLists
  • The Collections class contains static sort and

binarySearchmethods

  • Given you have a Comparator object for that class of object
  • These methods can be used to sort an ArrayList of object
sorting an arraylist of objects
Sorting an ArrayList of Objects
  • Using a Comparator :

ArrayList Coins = new ArrayList();

//add coins

. . .

Comparator comp = new CoinComparator();

Collections.sort(coins, comp);

file purse java
File Purse.java

01: import java.util.ArrayList;

02: import java.util.Collections;

03: import java.util.Comparator;

04:

05: /**

06: A purse holds a collection of coins.

07: */

08: public class Purse

09: {

10: /**

11: Constructs an empty purse.

12: */

13: public Purse()

14: {

15: coins = new ArrayList();

16: }

17:

slide79
18: /**

19: Add a coin to the purse.

20: @param aCoin the coin to add

21: */

22: public void add(Coin aCoin)

23: {

24: coins.add(aCoin);

25: }

26:

27: /**

28: Returns a string describing the purse contents,

29: sorted by coin value.

30: @return the string describing the purse contents

31: */

32: public String toString()

33: {

34: // sort the coins first

35: class CoinComparator implements Comparator

36: {

37: public int compare(Object firstObject, Object secondObject)

slide80
38: {

39: Coin first = (Coin)firstObject;

40: Coin second = (Coin)secondObject;

41: if (first.getValue() < second.getValue()) return -1;

42: if (first.getValue() == second.getValue()) return 0;

43: return 1;

44: }

45: }

46:

47: Comparator comp = new CoinComparator();

48: Collections.sort(coins, comp);

49:

50: String r = "Purse[coins=";

51: for (int i = 0; i < coins.size(); i++)

52: {

53: if (i > 0) r = r + ",";

54: r = r + coins.get(i);

55: }

56: return r + "]";

57: }

slide81
58:

59: private ArrayList coins;

60: }

file pursetest java
File PurseTest.java

01: import javax.swing.JOptionPane;

02:

03: /**

04: This class tests the Purse class by prompting the

05: user to add coins into a purse and printing the

06: purse contents, sorted by coin value.

07: */

08: public class PurseTest

09: {

10: public static void main(String[] args)

11: {

12: double NICKEL_VALUE = 0.05;

13: double DIME_VALUE = 0.1;

14: double QUARTER_VALUE = 0.25;

15:

16: Purse myPurse = new Purse();

17:

slide83
18: boolean done = false;

19: while (!done)

20: {

21: String input

22: = JOptionPane.showInputDialog("Enter coin name or Cancel");

23: if (input == null)

24: done = true;

25: else

26: {

27: double value = 0;

28: if (input.equals("nickel"))

29: value = NICKEL_VALUE;

30: else if (input.equals("dime"))

31: value = DIME_VALUE;

32: else if (input.equals("quarter"))

33: value = QUARTER_VALUE;

34: if (value != 0)

35: {

36: Coin c = new Coin(value, input);

37: myPurse.add(c);

slide84
38: System.out.println("The contents of the purse is "

39: + myPurse);

40: }

41: }

42: }

43: System.exit(0);

44: }

45: }