Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
CSE 373: Data Structures and Algorithms
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Building Java Programs
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
COMP s1 Computing 2 Complexity
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 19 Searching, Sorting and Big O
Chapter 19: Searching and Sorting Algorithms
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
CSE 373 Data Structures and Algorithms
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
3 – SIMPLE SORTING ALGORITHMS
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.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Searching and Sorting Searching algorithms with simple arrays
16 Searching and Sorting.
Searching and Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
Computer Science 112 Fundamentals of Programming II
COMP 53 – Week Seven Big O Sorting.
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Teach A level Computing: Algorithms and Data Structures
Building Java Programs
Lecture 14: binary search and complexity reading:
Algorithm design and Analysis
Lecture 11 Searching and Sorting Richard Gesick.
Lecture 15: binary search reading:
MSIS 655 Advanced Business Applications Programming
24 Searching and Sorting.
Building Java Programs
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:

Sorting and Searching Pepper

Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary Search –  assumes sort, faster

Static Method Tools Classes Arrays ◦ binarySearch (array, value) ◦ sort(array) Collections ◦ binarySearch (list, value) ◦ shuffle(list) ◦ sort(list) ◦ max(list) ◦ min(list)

Sorting – need order Comparable interface ◦ Only one order – chosen as natural order ◦ Implemented inside order  Implements Comparable ◦ int compareTo (SelfType o)  int compareTo (Object o)  Compare this item to the parm ◦ Used automatically by Collections sort ◦ Used automatically by TreeMap and TreeSet

Sorting – Custom Order Comparator ◦ Custom order ◦ Independent of the object's definition  Uses 2 objects and compares them  int compare (Selftype s1, Selftype s2)  No this  just 2 input objects to compare ◦ Implements comparator  Outside the class being sorted ◦ Why  Collections sort can use it  TreeSet can use it  TreeMap can use it

The Comparator Interface Syntax to implement ◦ public class lengthCom implements Comparator {  public int compare(String s1, String s2){  Return s1.length() = s2.length(); }} Sorted list: dog, cat, them, bunnies

How to use the Comparator Collections sort can take in a comparator ◦ Passed to methods as an extra parm ◦ Object passed into Collections.sort ◦ Ex: Collections.sort(myarray, new myArraySorter()) Use ◦ Arrays.sort(stringArray, new lengthCom()); ◦ Collections.sort(stringList, new lengthCom()); ◦ new TreeSet (new lengthCom());

Stock Comparators String ◦ CASE_INSENSITIVE_ORDER Collections ◦ reverseOrder() – returns comparator ◦ reverseOrder(comparator object)  Returns opposite order of comparator Use ◦ Collections.sort(myStringList, CASE_INSENSITIVE_ORDER) ◦ Collections.sort(myStringList, Collections.reverseOrder(new lengthCom()))

Searching and Sorting Searching and Sorting Complexity measuring Search algorithms Sort algorithms

Complexity Measurements Empirical – log start and end times to run ◦ Over different data sets Algorithm Analysis ◦ Assumed same time span (though not true):  Variable declaration and assignment  Evaluating mathematical and logical expressions  Access or modify an array element  Non-looping method call

How many units – worst case: Sample code – find the largest value var M = A[ 0 ]; //lookup 1, assign 1 for ( var i = 0; i < n; i++) { // i = o 1; test 1 // retest 1; i++ 1; if ( A[ i ] >= M ) { // test 1 ; lookup 1 M = A[ i ];}} // lookup 1, assign n + 4n F(n) = 4 + 2n + 4n = 4 + 6n Fastest growing term with no constant: F(n) = n (n is your array size)

Practice with asymptote finding – no constant f( n ) = n 2 + 3n gives f( n ) = n 2 f( n ) = n + sqrt(n) gives f( n ) = n F(n) = 2 n + 12 gives f(n) = 2 n F(n) = 3 n + 2 n gives f(n) = 3 n ◦ Just test with large numbers

Practice with asymptote finding (dropping constant) f( n ) = 5n + 12 gives f( n ) = n. ◦ Single loop will be n; ◦ called linear f( n ) = 109 gives f( n ) = 1. ◦ Need a constant 1 to show not 0 ◦ Means no repetition ◦ Constant number of instructions

Determining f(n) for loops for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) System.out.println(a[i][j][k]);}}} F(n) = n 3

Big O Notation – Growth Rate F(n) = n 3 gives Big O Notation of O( n 3 ) Which will be slower than O(n) Which will be slower than O(1)

Searching Sequential : ◦ Go through every item once to find the item ◦ Measure worst case ◦ 0(N)

Binary Search First Sorted Dictionary type search – keep looking higher or lower Takes 0 seconds, but cannot be O(1) because it has a loop As input grows, number of times to divide min-max range grows as: ◦ 2 repetitions is approximately the Number of elements in the array ◦ So, repetitions = log 2 N -  O(log 2 N)

Binary Search code public static int findTargetBinary(int[] arr, int target){ int min = 0; int max = arr.length - 1; while( min <= max) { int mid = (max + min) / 2; if (arr[mid] == target){ return mid; } else if (arr[mid] < target) { min = mid + 1; } else { max = mid - 1; } } return -1; }

Selection Sort Find the smallest value's index Place the smallest value in the beginning via swap Repeat for the next smallest value Continue until there is no larger value Go through almost every item in the array for as many items as you have in the array ◦ Complexity: O (N 2 )

Bubble Sort Initial algorithm Check every member of the array against the value after it; if they are out of order swap them. As long as there is at least one pair of elements swapped and we haven’t gone through the array n times: If the data is in order, it can be as efficient as O(n) or as bad as O(n 2 )

Merge Sort Two sorted subarrays can quickly be merged into a sorted array. Divide the array in half and sort the halves. Merge the halves. Picture: sorting-algorithms/merge-sort/ sorting-algorithms/merge-sort/ Video: / /

Merge Sort Complexity Split array in half repeatedly until each subarray contains 1 element. ◦ 2 repetitions is approximately the Number of elements in the array ◦ So, repetitions of division= log 2 N ◦ O(log N) At each step, do a merge, go through each element once ◦ O(N) Together: O (N log 2 N)

Comparison speed Sequential Search - O(N) Binary Search - O(log2 N) Selection Sort - O(N2) Bubble Sort - O(N2) Merge Sort - O (N log2 N)

Summary Relative complexity – O Notation Arrays and Collections class Different Search methods ◦ Sequential Search – keep looking one by one ◦ Binary Search – dictionary type split search Different Sort methods ◦ Selection Sort – look through all to find smallest and put it at the beginning – repeatedly ◦ Bubble Sort – continual swapping pairs – repeatedly ◦ Merge Sort - continually divide and sort then merge