Searching and Sorting

Similar documents
CSE 143 Lecture 16 (B)

CSE 143 Lecture 22. Sorting. reading: 13.1, slides adapted from Marty Stepp and Hélène Martin

Building Java Programs Chapter 13

CSE 373. Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort reading: Weiss Ch. 7. slides created by Marty Stepp

CSE 143 Lecture 14. Sorting

CSc 110, Spring 2017 Lecture 39: searching

Building Java Programs

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Building Java Programs

Lecture 15: Algorithms. AP Computer Science Principles

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

The Comparable Interface. reading: 10.2

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4

CSE 143 Lecture 2. reading:

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

Lecture 6 Sorting and Searching

Faster Sorting Methods

Searching in General

Sorting Pearson Education, Inc. All rights reserved.

COMP Data Structures

CSC 273 Data Structures

O(n): printing a list of n items to the screen, looking at each item once.

Sorting. Bubble Sort. Selection Sort

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

We can use a max-heap to sort data.

Measuring algorithm efficiency

3/20/2015. Chapter 19 Sorting and Searching SELECTION SORT INSERTION SORT MERGE SORT THE QUICKSORT ALGORITHM MERGE SORT VS SELECTION SORT

Sorting and Searching Algorithms

Introduction to Computers and Programming. Today

Intro to Algorithms. Professor Kevin Gold

Sorting is ordering a list of objects. Here are some sorting algorithms

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm

ECE 122. Engineering Problem Solving Using Java

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Priority queues. Priority queues. Priority queue operations

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Lecture 7: Searching and Sorting Algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

1/ COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16. Programming Fundamentals using Java

I. Algorithms. examples of cubic, quadratic, NlogN, linear, logarithmic, and constant

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Searching and Sorting Chapter 18. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Searching, Sorting. Arizona State University 1

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Lecture 5 Sorting Arrays

CSC 222: Object-Oriented Programming. Fall 2015

CSE 2123 Sorting. Jeremy Morris

Algorithmic Analysis. Go go Big O(h)!

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

4/27/2012. Chapter Fourteen: Sorting and Searching. Chapter Goals

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Sorting. Weiss chapter , 8.6

CSC 222: Object-Oriented Programming Spring 2012

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Lecture 2: Implementing ADTs

Sorting. Bringing Order to the World

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

C/C++ Programming Lecture 18 Name:

CS 211: Binary Search and Sorting

Parallel Sorting Algorithms

Algorithm Analysis. Performance Factors

Unit-2 Divide and conquer 2016

Sorting Algorithms. + Analysis of the Sorting Algorithms

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Simple Sorting Algorithms

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Sorting & Searching (and a Tower)

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

LECTURE 08 SEARCHING AND SORTING ARRAYS

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Data Structures and Algorithms for Engineers

CSE 214 Computer Science II Searching

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Chapter Contents. An Introduction to Sorting. Selection Sort. Selection Sort. Selection Sort. Iterative Selection Sort. Chapter 9

Chapter 8 Search and Sort

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Sorting and Searching

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Unit 10: Sorting/Searching/Recursion

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Transcription:

Searching and Sorting

Sequential search sequential search: Locates a target value in an array/list by examining each element from start to finish. How many elements will it need to examine? Example: Searching the array below for the value 42: index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103 i Notice that the array is sorted. Could we take advantage of this? 2

Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. How many elements will it need to examine? Example: Searching the array below for the value 42: index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103 min mid max 3

Arrays.binarySearch // searches an entire sorted array for a given value // returns its index if found; a negative number if not found // Precondition: array is sorted Arrays.binarySearch(array, value) // searches given portion of a sorted array for a given value // examines minindex (inclusive) through maxindex (exclusive) // returns its index if found; a negative number if not found // Precondition: array is sorted Arrays.binarySearch(array, minindex, maxindex, value) The binarysearch method in the Arrays class searches an array very efficiently if the array is sorted. You can search the entire array, or just a range of indexes (useful for "unfilled" arrays such as the one in ArrayIntList) If the array is not sorted, you may need to sort it first 4

Using binarysearch // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int[] a = {-4, 2, 7, 9, 15, 19, 25, 28, 30, 36, 42, 50, 56, 68, 85, 92}; int index = Arrays.binarySearch(a, 0, 16, 42); // index1 is 10 int index2 = Arrays.binarySearch(a, 0, 16, 21); // index2 is -7 binarysearch returns the index where the value is found if the value is not found, binarysearch returns: -(insertionpoint + 1) where insertionpoint is the index where the element would have been, if it had been in the array in sorted order. To insert the value into the array, negate insertionpoint + 1 int indextoinsert21 = -(index2 + 1); // 6 5

Binary search and objects Can we binarysearch an array of Strings? Operators like < and > do not work with String objects. But we do think of strings as having an alphabetical ordering. natural ordering: Rules governing the relative placement of all values of a given type. comparison function: Code that, when given two values A and B of a given type, decides their relative ordering: A < B, A == B, A > B 6

The compareto method (10.2) The standard way for a Java class to define a comparison function for its objects is to define a compareto method. Example: in the String class, there is a method: public int compareto(string other) A call of A.compareTo(B) will return: a value < 0 if A comes "before" B in the ordering, a value > 0 if A comes "after" B in the ordering, or 0 if A and B are considered "equal" in the ordering. 7

Runtime Efficiency (13.2) efficiency: A measure of the use of computing resources by code. can be relative to speed (time), memory (space), etc. most commonly refers to run time Assume the following: Any single Java statement takes the same amount of time to run. A method call's runtime is measured by the total of the statements inside the method's body. A loop's runtime, if the loop repeats N times, is N times the runtime of the statements in its body. 8

Algorithm growth rates (13.2) We measure runtime in proportion to the input data size, N. growth rate: Change in runtime as N changes. Say an algorithm runs 0.4N 3 + 25N 2 + 8N + 17 statements. Consider the runtime when N is extremely large. We ignore constants like 25 because they are tiny next to N. The highest-order term (N 3 ) dominates the overall runtime. We say that this algorithm runs "on the order of" N 3. or O(N 3 ) for short ("Big-Oh of N cubed") 9

Complexity classes complexity class: A category of algorithm efficiency based on the algorithm's relationship to the input size N. Class Big-Oh If you double N,... Example constant O(1) unchanged 10ms logarithmic O(log 2 N) increases slightly 175ms linear O(N) doubles 3.2 sec log-linear O(N log 2 N) slightly more than doubles 6 sec quadratic O(N 2 ) quadruples 1 min 42 sec cubic O(N 3 ) multiplies by 8 55 min............ exponential O(2 N ) multiplies drastically 5 * 10 61 years 10

Sorting sorting: Rearranging the values in an array or collection into a specific order (usually into their "natural ordering"). one of the fundamental problems in computer science can be solved in many ways: there are many sorting algorithms some are faster/slower than others some use more/less memory than others some work better with specific kinds of data some can utilize multiple computers / processors,... comparison-based sorting : determining order by comparing pairs of elements: <, >, compareto, 11

Sorting methods in Java The Arrays and Collections classes in java.util have a static method sort that sorts the elements of an array/list String[] words = {"foo", "bar", "baz", "ball"}; Arrays.sort(words); System.out.println(Arrays.toString(words)); // [ball, bar, baz, foo] List<String> words2 = new ArrayList<String>(); for (String word : words) { words2.add(word); } Collections.sort(words2); System.out.println(words2); // [ball, bar, baz, foo] 12

Collections class Method name binarysearch(list, value) copy(listto, listfrom) emptylist(), emptymap(), emptyset() fill(list, value) Description returns the index of the given value in a sorted list (< 0 if not found) copies listfrom's elements to listto returns a read-only collection of the given type that has no elements sets every element in the list to have the given value max(collection), min(collection) returns largest/smallest element replaceall(list, old, new) reverse(list) shuffle(list) sort(list) replaces an element value with another reverses the order of a list's elements arranges elements into a random order arranges elements into ascending order 13

Sorting algorithms bogo sort: shuffle and pray bubble sort: swap adjacent pairs that are out of order selection sort: look for the smallest element, move to front insertion sort: build an increasingly large sorted front portion merge sort: recursively divide the array in half and sort it heap sort: place the values into a sorted tree structure quick sort: recursively partition array based on a middle value other specialized sorting algorithms: bucket sort: cluster elements into smaller groups, sort them radix sort: sort integers by last digit, then 2nd to last, then...... 14

Bogo sort bogo sort: Orders a list of values by repetitively shuffling them and checking if they are sorted. name comes from the word "bogus" The algorithm: Scan the list, seeing if it is sorted. If so, stop. Else, shuffle the values in the list and repeat. This sorting algorithm (obviously) has terrible performance! What is its runtime? 15

Selection sort selection sort: Orders a list of values by repeatedly putting the smallest or largest unplaced value into its final position. The algorithm: Look through the list to find the smallest value. Swap it so that it is at index 0. Look through the list to find the second-smallest value. Swap it so that it is at index 1.... Repeat until all values are in their proper places. 16

Selection sort example Initial array: index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 22 18 12-4 27 30 36 50 7 68 91 56 2 85 42 98 25 After 1st, 2nd, and 3rd passes: index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 18 12 22 27 30 36 50 7 68 91 56 2 85 42 98 25 index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 12 22 27 30 36 50 7 68 91 56 18 85 42 98 25 index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 22 27 30 36 50 12 68 91 56 18 85 42 98 25 17

Selection sort code // Rearranges the elements of a into sorted order using // the selection sort algorithm. public static void selectionsort(int[] a) { for (int i = 0; i < a.length - 1; i++) { // find index of smallest remaining value int min = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } } } } // swap smallest value its proper place, a[i] swap(a, i, min); 18

Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide the list into two roughly equal halves. Sort the left half. Sort the right half. Merge the two sorted halves into one sorted list. Often implemented recursively. An example of a "divide and conquer" algorithm. Invented by John von Neumann in 1945 19

Merge sort example index 0 1 2 3 4 5 6 7 value 22 18 12-4 58 7 31 42 split split 22 18 12-4 split 58 7 31 42 split 22 18 split 12-4 split 58 7 split 31 42 22 18 12-4 58 7 31 42 merge 18 22 merge -4 12 merge 7 58 merge 31 42 merge -4 12 18 22 merge 7 31 42 58 merge -4 7 12 18 22 31 42 58 20