Building Java Programs

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
CSE 143 Lecture 4: testing and complexity reading:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Analysis of Algorithm.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Abstract Data Types (ADTs) Data Structures The Java Collections API
CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Computer Science Searching & Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
CSC 211 Data Structures Lecture 13
CSE 341 Lecture 4 merge sort; basic efficiency issues Ullman slides created by Marty Stepp
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Data Structure Introduction.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Binary search and complexity reading:
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
1 CSE 373 Algorithm Analysis and Runtime Complexity slides created by Marty Stepp ©
Building Java Programs Chapter 13 Searching and Sorting Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Searching and Sorting Searching algorithms with simple arrays
Lecture 25: Searching and Sorting
19 Searching and Sorting.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Teach A level Computing: Algorithms and Data Structures
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Lecture 15: binary search reading:
CSE 154 Sorting reading: 13.3, 13.4
Lecture 5: complexity reading:
Adapted from slides by Marty Stepp and Stuart Reges
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
slides created by Marty Stepp
Building Java Programs
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
slides created by Marty Stepp
Building Java Programs
CSE 143 Sorting reading: 13.3, 13.4.
Building Java Programs Chapter 13
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
slides created by Marty Stepp and Hélène Martin
Building Java Programs
Building Java Programs
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
slides adapted from Marty Stepp
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Presentation transcript:

Building Java Programs Chapter 13: Searching and Sorting

Chapter outline Searching and sorting in the Java class libraries sequential and binary search sorting shuffling custom ordering with Comparators Program efficiency algorithm analysis complexity classes and Big-Oh notation Implementing searching and sorting algorithms binary search selection sort merge sort

Searching and sorting in the Java class libraries reading: 13.1

Sequential search Imagine searching for a particular word in an ArrayList of words from a book: int index = words.indexOf(word); if (index >= 0) { System.out.println(word + " is word #" + index); } else { System.out.println(word + " is not found."); } sequential search: One that examines each element of a list in sequence until it finds the target value or reaches the end of the list. The indexOf method above uses a sequential search. index 1 2 3 4 5 6 ... value It was the best of times it

Binary search algorithm binary search: An algorithm that searches for a value in a sorted list by repeatedly eliminating half the list from consideration. Can be written iteratively or recursively implemented in Java as method Arrays.binarySearch in java.util package Algorithm pseudocode (searching for a value K): Start out with the search area being from indexes 0 to length-1. Examine the element in the middle of the search area. If it is K, stop. Otherwise, If it is smaller than K, eliminate the upper half of the search area. If it is larger than K, eliminate the lower half of the search area. Repeat the above examination.

Binary search example

Using binarySearch Java provides two binary search methods: Arrays.binarySearch (for an array) // binary search on an array: int[] numbers = {-3, 2, 8, 12, 17, 29, 44, 58, 79}; int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index); Collections.binarySearch (for a List) // binary search on ArrayList with the same values: int index = Collections.binarySearch(list, 29); Note that the values in the array / list are in sorted order. If they are not, binarySearch is not guaranteed to work properly.

Sorting sorting: Rearranging the values in a list into a given order (often into their natural ascending order). One of the fundamental problems in computer science Many sorts are comparison-based (must determine order through comparison operations on the input data) <, >, compareTo, … index 1 2 3 4 5 6 7 value 15 8 17 10 12 index 1 2 3 4 5 6 7 value 8 10 12 15 17

Sorting in the class libraries Java provides two sorting methods: Arrays.sort (for an array) // demonstrate the Arrays.sort method String[] strings = {"c", "b", "g", "h", "d", "f", "e", "a"}; System.out.println(Arrays.toString(strings)); Arrays.sort(strings); Output: [c, b, g, h, d, f, e, a] [a, b, c, d, e, f, g, h] Collections.sort (for a List)

Shuffling shuffling: Rearranging the elements of a list into a random order. Java has a shuffle method for a List, Collections.shuffle: String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"}; ArrayList<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { deck.add(rank + " of " + suit); } Collections.shuffle(deck); System.out.println("Top card = " + deck.get(0)); Output (for one example run): Top card = 10 of Spades

Custom ordering with Comparators reading: 13.1

Custom ordering Sometimes, the default ordering is not what you want. Example: The following code sorts the strings in a case-sensitive order, so the uppercase letters come first. We may have wanted a case-insensitive ordering instead. String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings); System.out.println(Arrays.toString(strings)); Output: [Charlie, DELTA, Foxtrot, alpha, bravo, echo, golf, hotel] You can describe a custom sort ordering by creating a class called a comparator.

Comparators The Comparator interface in java.util describes a method for comparing two objects of a given type: public interface Comparator<T> { public int compare(T o1, T o2); } Note that Comparator is a generic interface. T will be replaced by the type of object you are actually comparing. The compare method's job is to decide the relative ordering of the two given objects and return an appropriate integer: < 0 if o1 comes before o2, 0 if o1 and o2 are equivalent, > 0 if o1 comes after o2.

Comparator example The following Comparator compares Strings, ignoring case: public class CaseInsensitiveComparator implements Comparator<String> { public int compare(String s1, String s2) { return s1.toLowerCase().compareTo( s2.toLowerCase()); }

Sorting with Comparators The sorting methods shown previously can also be called with a Comparator as a second parameter. The sorting algorithm will use that comparator to order the elements of the array or list. String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings, new CaseInsensitiveComparator()); System.out.println(Arrays.toString(strings)); [Charlie, DELTA, Foxtrot, alpha, bravo, echo, golf, hotel] Output: [alpha, bravo, Charlie, DELTA, echo, Foxtrot, golf, hotel]

Program efficiency reading: 13.2

Efficiency efficiency: A measure of the computing resources used by a program, such as time, memory, or disk space. time efficiency: How quickly a program runs "Fast enough" is often relative to the task being performed. 5 minutes to render a complex 3D scene for a movie is fast. 5 minutes to search Google is slow. Ways to measure the efficiency of a program: empirical analysis: Program the algorithm, run it, and time it. algorithm analysis: Applying techniques to mathematically estimate the algorithm's runtime without actually coding it.

Rules for algorithm analysis Most individual statements take 1 unit of time to run. When multiple statements are executed sequentially, their runtimes are added. When statements are executed in a loop, the runtime is multiplied by the number of repetitions of the loop.

More examples Larger statements can also occur sequentially, in which case their runtimes are added. If larger statements are nested, their runtimes are multiplied.

Relative rates of growth most algorithms' runtime can be expressed as a function of the input size N rate of growth: measure of how quickly the graph of a function rises goal: distinguish between fast- and slow-growing functions we only care about very large input sizes (for small sizes, most any algorithm is fast enough) this helps us discover which algorithms will run more quickly or slowly, for large input sizes Motivation: we usually care only about algorithm performance when there are large number of inputs. We usually don’t care about small changes in run-time performance. (inaccuracy of estimates make small changes less relevant). Consider algorithms with slow growth rate better than those with fast growth rates.

Growth rate example Consider these graphs of functions. Perhaps each one represents an algorithm: n3 + 2n2 100n2 + 1000 Which grows faster?

Growth rate example How about now?

Range algorithm 1 Let's examine the efficiency and growth rate of three algorithms for finding the range (difference between largest and smallest element) in an array. First algorithm (looks at all pairs of values to find which pair is the largest and smallest): // returns the range of numbers in the given array public static int range(int[] numbers) { int maxDiff = 0; for (int i = 0; i < numbers.length; i++) { for (int j = 0; j < numbers.length; j++) { int diff = Math.abs(numbers[j] - numbers[i]); maxDiff = Math.max(maxDiff, diff); } return maxDiff;

Runtime of range 1 Observation: runtime seems to roughly quadruple when input size is doubled

Range algorithm 2 The first algorithm redundantly makes each comparison twice (when i <= j, when i > j). Second, improved algorithm: public static int range2(int[] numbers) { int maxDiff = 0; for (int i = 0; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { int diff = Math.abs(numbers[j] - numbers[i]); maxDiff = Math.max(maxDiff, diff); } return maxDiff;

Runtime of range 2 Observation: about twice as fast; same growth pattern (runtime quadruples when input size is doubled)

Range algorithm 3 We can discover the largest and smallest values in a single pass over the array, rather than using many nested passes over all values. Third algorithm: public static int range3(int[] numbers) { int max = numbers[0]; int min = max; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } else if (numbers[i] < min) { min = numbers[i]; } return max - min;

Runtime of range 3 Much faster than others Runtime doubles when input size doubles

Complexity classes and Big-oh notation reading: 13.2

Big-Oh notation complexity class: Category of algorithms' runtime based on relationship to input size. Determined by the exponent of the most frequently executed line of code in the algorithm. big-Oh notation: A shorthand for describing complexity classes. Example: If the most frequently executed line of an algorithm runs approximately N3 times, we say the algorithm is "order N3" or O(N3) for short. We are concerned with how the function grows when N is large. We are not picky about constant factors. Important (not in Lewis & Chase book). Write on board. (for next slide).

Hierarchy of Big-Oh Complexity classes in increasing order of growth: constant time O(1) logarithmic O(log N) linear O(N) loglinear O(N log N) quadratic O(N2) cubic O(N3) ... exponential O(2N) An algorithm from a lower complexity class will run much faster than one from a higher complexity class when the value of N becomes very large.

Program loop runtimes for (int i = 0; i < N; i += c) // O(N) <statements>; Adding to the loop counter means that the loop runtime grows linearly when compared to its maximum value N. Loop executes its body exactly N / c times. for (int i = 0; i < N; i *= c) // O(log N) Multiplying the loop counter means that the maximum value N must grow exponentially to linearly increase the loop runtime; therefore, it is logarithmic. Loop executes its body exactly logc N times. for (int i = 0; i < N * N; i += c) // O(N2) The loop maximum is N2, so the runtime is quadratic. Loop executes its body exactly (N2 / c) times.

More loop runtimes Nesting loops multiplies their runtimes. for (int i = 0; i < N; i += c) { // O(N2) for (int j = 0; j < N; i += c) { <statements>; } Loops in sequence add together their runtimes, which means the loop set with the larger runtime dominates. for (int i = 0; i < N; i += c) { // O(N) for (int i = 0; i < N; i += c) { // O(N log N) for (int j = 0; j < N; i *= c) {

Loop runtime problems Approximate the value of the variable sum after the following code fragment, as an expression in terms of input size n. Use Big-Oh notation to describe the algorithm's overall runtime. int sum = 0; for (int i = 1; i <= N; i *= 2) { sum++; }   for (int i = 1; i <= 1000; i++) {

Loop runtime problems Approximate the value of the variable sum after the following code fragment, as an expression in terms of input size n. Use Big-Oh notation to describe the algorithm's overall runtime. int sum = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= i / 2; j += 2) { sum++; }

Implementing binary search reading: 13.3

Recall: binary search binary search: An algorithm that searches for a value in a sorted list by repeatedly eliminating half the list from consideration. Can be written iteratively or recursively Algorithm pseudocode (searching for a value K): Start out with the search area being from indexes 0 to length-1. Examine the element in the middle of the search area. If it is K, stop. Otherwise, If it is smaller than K, eliminate the upper half of the search area. If it is larger than K, eliminate the lower half of the search area. Repeat the above examination.

Binary search example searching for value 16 min mid (too big!) max 1 1 2 3 4 5 6 4 7 16 20 37 38 43 min mid (too big!) max

Binary search example searching for value 16 min mid (too small!) max 1 2 3 4 5 6 4 7 16 20 37 38 43 min mid (too small!) max

Binary search example searching for value 16 min, mid, max (found it!) 1 2 3 4 5 6 4 7 16 20 37 38 43 min, mid, max (found it!)

Binary search pseudocode binary search array a for value i: if all elements have been searched, result is -1. examine middle element a[mid]. if a[mid] equals i, result is mid. if a[mid] is greater than i, binary search lower half of a for i. if a[mid] is less than i, binary search upper half of a for i.

Binary search code The following code implements binary search: // Returns an index at which the target // appears in the given input array, or -1 if not found. // pre: array is sorted. public static int binarySearch(int[] numbers, int target) { int min = 0; int max = numbers.length - 1; while (min <= max) { int mid = (max + min) / 2; if (numbers[mid] == target) { return mid; // found it! } else if (numbers[mid] < target) { min = mid + 1; // too small } else { // numbers[mid] > target max = mid - 1; // too large } return -1; // not found

Runtime of binary search How do we analyze the runtime of binary search? The algorithm's runtime is dominated by a while loop. Each pass of that loop cuts the search space in half. How many times does this division in half take place? 2repetitions  N repetitions  log2N In binary sort, log n divisions take place.

Implementing selection sort reading: 13.3

Selection sort selection sort: orders a list of values by repetitively putting a particular value into its final position more specifically: find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places

Selection sort example

Selection sort example 2 Index 1 2 3 4 5 6 7 Value 27 63 72 64 58 14 9 1st pass 2nd pass 3rd pass … The selection sort marks the first element (27). It then goes through the remaining data to find the smallest number(1). It swaps this with the first element and the smallest element is now in its correct position. It then marks the second element (63) and looks through the remaining data for the next smallest number (9). These two numbers are then swapped. This process continues until n-1 passes have been made.

Selection sort code The following code implements selection sort: // places elements of the given array into sorted order public static void selectionSort(int[] a) { for (int i = 0; i < a.length - 1; i++) { // find index of smallest element int smallest = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[smallest]) { smallest = j; } swap(a, i, smallest); // swap smallest to front public static void swap(int[] list, int i, int j) { int temp = list[i]; list[i] = list[j]; list[j] = temp;

Selection sort runtime Observation: runtime roughly quadruples when input size is doubled makes sense because the code performs two nested loops over the array of size N its runtime should be O(N2) faster because fewer swaps are made. (Constant c is smaller on O(n^2).)

Sorting practice problem Consider the following array of int values: [22, 11, 34, -5, 3, 40, 9, 16, 6] Perform the selection sort algorithm on this data. Write the contents of the array after each pass of the outermost loop of selection sort until the array is sorted.

Implementing merge sort reading: 13.4

Merge sort merge sort: orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining Invented by John von Neumann in 1945 relies on observation that if you have two sorted lists of values, they can be combined into a single larger sorted list quickly merge sort pseudocode: split the array into two halves. merge sort the left half (recursively). merge sort the right half (recursively). combine the two halves into a sorted whole.

Merge sort example 67 45 23 14 6 33 98 42 45 23 14 98 67 6 33 42 23 98 45 14 67 6 33 42 98 23 45 14 6 67 33 42 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98

Merge sort example 2 13 6 21 18 9 4 8 20 7 13 6 21 18 3 9 4 8 20 7 13 6 1 21 18 2 3 9 4 5 8 20 6 7 13 6 1 21 2 18 3 9 4 4 5 8 6 20 7 6 13 1 18 21 2 3 4 9 5 8 20 6 7 6 13 18 21 3 4 8 9 20 7 4 6 8 9 13 18 20 21 7

Merging two sorted arrays merge operation: Given two sorted arrays, merge operation produces a sorted array with all the elements of the two arrays A 6 13 18 21 B 4 8 9 20 C 4 6 8 9 13 18 20 21 Running time of merge : O(N), where N is the number of elements in the merged array. When merging two sorted parts of the same array, we'll need a temporary array to store the merged whole.

Merge operation example

Split into halves code The following code splits an array into its two halves: // Returns the first half of the given array. public static int[] leftHalf(int[] array) { int size1 = array.length / 2; int[] left = new int[size1]; for (int i = 0; i < size1; i++) { left[i] = array[i]; } return left; // Returns the second half of the given array. public static int[] rightHalf(int[] array) { int size2 = array.length - size1; int[] right = new int[size2]; for (int i = 0; i < size2; i++) { right[i] = array[i + size1]; return right;

Merge code The following code implements the merge operation // pre : result is empty; left/right are sorted // post: result contains merged sorted lists. public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++) { if (i2 >= right.length || (i1 < left.length && left[i1] <= right[i2])) { result[i] = left[i1]; // take from left i1++; } else { result[i] = right[i2]; // take from right i2++; }

Merge sort code The following code implements the overall merge sort: // Places elements of given array into sorted order // using the merge sort algorithm. public static void mergeSort(int[] array) { if (array.length > 1) { // split array into two halves int[] left = leftHalf(array); int[] right = rightHalf(array); // recursively sort the two halves mergeSort(left); mergeSort(right); // merge the sorted halves into a sorted whole merge(array, left, right); }

Sorting practice problem Consider the following array of int values. [22, 11, 34, -5, 3, 40, 9, 16, 6] Perform the merge sort algorithm on this data. Write the contents of the array after each of the recursive calls of merge sort have finished.