Recitation 13 Searching and Sorting.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Simple Sorting Algorithms
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Lecture 12. Searching Algorithms and its analysis 1.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CSCI 51 Introduction to Programming March 12, 2009.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Searching algorithms with simple arrays
Sorting Mr. Jacobs.
Week 13: Searching and Sorting
CSCI 104 Sorting Algorithms
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Simple Sorting Algorithms
Data Structures in Java with JUnit ©Rick Mercer
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.
Data Structures and Algorithms
Insertion Sort Sorted Unsorted
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Sorting Algorithms Written by J.J. Shepherd.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Linear and Binary Search
Algorithm design and Analysis
Selection Sort Sorted Unsorted Swap
Lecture 11 Searching and Sorting Richard Gesick.
Data Structures Review Session
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Sorting … and Insertion Sort.
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
Searching and Sorting Topics Sequential Search on an Unordered File
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
Data Structures Sorted Arrays
Algorithms Analysis Algorithm efficiency can be measured in terms of:
Quicksort.
Search,Sort,Recursion.
Analysis of Algorithms
Simple Sorting Algorithms
Chapter 10 Sorting Algorithms
Simple Sorting Algorithms
Simple Sorting Algorithms
Searching.
Module 8 – Searching & Sorting Algorithms
Quicksort.
Presentation transcript:

Recitation 13 Searching and Sorting

Announcements Project 5 is posted Milestone due on Thursday December 3rd Final submission due on Thursday December 10th

Questions?

Searching We have a collection of data an array of integers a list of 1 million names We are asked to find a given data find the number 10 in the array find the name “Nikola Tesla” in the list If we don’t know anything about the data The numbers are randomly generated The names are not in any order We have to search every element linear search or brute force approach: O(n)

Linear Search Very lucky if at the begining of the array public static int find( int[] array, int number ) { for( int i = 0; i < array.length; i++ ) if( array[i] == number ) return i; return -1; } Very lucky if at the begining of the array Very unlucky if at the end of the array On average, we need to look at n/2 elements O(n/2) = O(n) What if the list is in order, i.e. sorted ? BINARY SEARCH!

Binary Search Assuming the list is in ascending order public static int binarySearch( int[] array, int number ) { int left = 0; int right = array.length – 1; while (right >= left) { int middle = (left + right) / 2; if (array[middle] == number) return middle; else if (array[middle] > number) right = middle - 1; else //array[middle] < number left = middle + 1; } return -1;

Binary Search Given a sorted array in ascending order, find a specific number in this array. Say, looking for 35. left middle right 26 28 30 33 35 37 41 46 0 1 2 3 4 5 6 7 left middle right 26 28 30 33 35 37 41 46 0 1 2 3 4 5 6 7 left right 26 28 30 33 35 37 41 46 0 1 2 3 4 5 6 7

Binary Search What if the number is not in the array? Say, looking for 29. left middle right 26 28 30 33 35 37 41 46 0 1 2 3 4 5 6 7 left middle right 26 28 30 33 35 37 41 46 0 1 2 3 4 5 6 7 left middle right 26 28 30 33 35 37 41 46 0 1 2 3 4 5 6 7 right left 26 28 30 33 35 37 41 46 0 1 2 3 4 5 6 7 right smaller than left, the number is not in this array, return -1

Running time of Binary Search Each time, we divide the array into two and search in only one half later on. Running time how many times n could be divide by 2 O(log n) times Remember: we mean log base 2 -> O(log2 n) If n = 1024, log n = 10 we can find any number in at most 10 iterations compare to average time n/2= 512 of linear search

Sorting Binary search requires a sorted list How can we sort an array of numbers bubble sort: O(n2) insertion sort: O(n2) merge sort: O(n logn) bucket sort: O(n+m) m is the range of the numbers

Bubble sort Idea: Large elements “bubble” to the end of the list for( int i = 0; i < array.length – 1; i++ ) for( int j = 0; j < array.length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } Idea: Large elements “bubble” to the end of the list until nothing is out of order What is wrong with this code? it always goes through the array n-1 times even if it is in sorted order much earlier What happens if the list is already sorted at the beginning?

Bubble sort v2 Stops when the list gets sorted boolean workToDo = true; while (workToDo) { workToDo = false; for( int j = 0; j < array.length - 1; j++ ) if( array[j] > array[j + 1] )      {         int temp = array[j];         array[j] = array[j + 1];         array[j + 1] = temp;           workToDo = true;      }  } Stops when the list gets sorted stops going through the array if there were no swaps on the preceding iteration, i.e. workToDo = false Now, what happens if the list is already sorted at the beginning?

Insertion sort Idea: keep a growing sorted list Add the ith number to the sorted numbers for( int i = 1; i < array.length; i++ ) for( int j = i; j > 0; j-- ) //count back if( array[j - 1] > array[j] ) { int temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; } else break; Idea: keep a growing sorted list In each inner loop, array[0] ... array[i-1] are sorted. Add one more new number into the sorted numbers by swapping with larger numbers. After iteration i, array[0] ... array[i] are in perfect sorted order. Swap with larger numbers until it is in the correct position Stop when reach some smaller number

Insertion sort 45 24 13 77 71 58 66 10 1 number is sorted Add this one to the right place 24 45 13 77 71 58 66 10 2 numbers are sorted 13 24 45 77 71 58 66 10 No need for swapping 13 24 45 77 71 58 66 10 Should go here 13 24 45 71 77 58 66 10 array[0] ... array[i-1] are sorted array[i] 13 24 45 58 71 77 66 10 Now, array[0] ... array[i] are sorted

Insertion sort We should do this n-1 times to sort n numbers 13 24 45 58 71 77 66 10 13 24 45 58 66 71 77 10 10 13 24 45 58 66 71 77 Running time = 1 + 2 + 3 + … + (n-1) = (n-1)n / 2 = O(n2)

Insertion sort in descending order Swap with smaller numbers this time. swap when they are not in correct order. Just change “>” to “<” in the if statement Should go here 77 71 45 24 13 58 66 10 i-1 numbers sorted Add this one to the right place 77 71 58 45 24 13 66 10

Merge Sort Divide and conquer It has two steps: It takes O(nlogn) time Split: keep dividing in half until you have one element in your lists Merge: merge the lists while sorting in order It takes O(nlogn) time The best time in comparison based sorting algorithms

Merge Sort 45 24 13 77 71 58 66 10 45 24 13 77 71 58 66 10 45 24 13 77 71 58 66 10 45 24 13 77 71 58 66 10 24 45 13 77 58 71 10 66 13 24 45 77 10 58 66 71 10 13 24 45 58 66 71 77

Bucket Sort A better sorting algorithm: O(n) If the numbers are in a small range For example: exam grades are in [1,100] let m be the length of the range, i.e. number of possible values Algorithm: Create an array of m, array of values Traverse your array of integers for a particular value, increment the count in the corresponding index in the array of values Traverse the values array and print the index as many times as the count it holds Idea: Count how many times a particular number occurs in the array.

Bucket Sort array of integers to sort 6 2 3 5 8 9 10 1 7 array of values 2 3 1 1 2 3 4 5 6 7 8 9 10 array of integers after sorting 1 2 3 5 6 7 8 9 10

Bucket Sort Here’s bucket sort in code with a range of [min, max]: int[] values = new int[max - min + 1]; for( int i = 0; i < array.length; i++ ) values[array[i] - min]++; int count = 0; for( int i = 0; i < values.length; i++ ) { for( int j = 0; j < values[i]; j++ ) { array[count] = i + min; count++; }

Bucket Sort Actually, it runs in O(n+m) times m is the length of the range, for exam grades m=100 But since m <= n, at the worst case it is O(2n) = O(n) remember, we can drop constants in asymptotic analysis

Questions?