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.

Slides:



Advertisements
Similar presentations
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Advertisements

Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CSC 211 Data Structures Lecture 13
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
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.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
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
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS1010 Programming Methodology
CSCI 104 Sorting Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Quicksort "There's nothing 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 Hat, Harry Potter.
Chapter 13: Searching and Sorting
Topic 14 Searching and Simple Sorts
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
Searching.
Introduction to Search Algorithms
Selection Sort Sorted Unsorted Swap
CSc 110, Spring 2017 Lecture 39: searching.
CSC215 Lecture Algorithms.
Chapter 8 Search and Sort
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
CSE 373 Data Structures and Algorithms
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
Data Structures for Java William H. Ford William R. Topp
Topic 14 Searching and Simple Sorts
Sorting "There's nothing 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 Hat, Harry Potter.
Topic 24 sorting and searching arrays
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
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
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Searching.
Module 8 – Searching & Sorting Algorithms
Applications of Arrays
Presentation transcript:

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 Hat, Harry Potter and the Sorcerer's Stone

Searching Given an array of ints, find the index of an int key For key = 27 and the above array, a search function returns 2 If it's not present, often returns negative value What if more than one occurrence of the key? index 1 2 3 4 5 value 89 27 -5 42 11

Question Given an array of 1,000,000 distinct elements in random order, how many elements do you expect to look at on average when searching if: key present key not present A. 1 1,000,000 B. 500,000 1,000,000 C. 1,000,000 1,000,000 D. 1,000 500,000 E. 20 1,000,000

Linear or Sequential Search Examine each array value successively, comparing to the key, until the key is found or end of array is reached int linearSearch(int arr[], int N, int key) { for(int i = 0; i < N; i++) { if(arr[i] == key) return i; } return -1; Worst case runtime: every array element is compared to the key. O(N) Average case runtime: N/2 comparisons before key is found. O(N) Best case runtime: 1 comparison before key is found. O(1)

Searching in a Sorted List If array elements are sorted (e.g., in increasing order), then we can divide and conquer Dividing your work in half with each step a good thing! Searching is more efficient if the array is sorted

Binary Search Problem: Searching for a key in an array of N elements Binary search assumes that the array is sorted (i.e., array elements are in increasing order) Binary search successively eliminates half of the elements Algorithm: Examine the middle element of the array If it's too big: eliminate the right half of the array and repeat If it's too small: eliminate the left half of the array and repeat Else it's the key we're searching for of the sub-array size is 0, so stop Which indices does the algorithm examine to find value 42? What is the runtime complexity of binary search? index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -5 20 23 25 32 36 42 51 57 66 84 90 101 min mid max

Binary Search mid max min mid max min min mid max index 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -5 20 23 25 32 36 42 51 57 66 84 90 101 mid max min index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -5 20 23 25 32 36 42 51 57 66 84 90 101 mid max min index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -5 20 23 25 32 36 42 51 57 66 84 90 101 min mid max

Binary Search Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 19 23 29 31 37 41 43 47 53

Binary Search int binarySearch(int arr[], int N, int key) { int mid; int min = 0; int max = N-1; while(min <= max) { mid = (min + max)/2; if(key == arr[mid]) return mid; else if(key < arr[mid]) max = mid – 1; else min = mid + 1; } return -1;

Binary Search Runtime For an array of size N, binary search eliminates ½ until 1 element remains N, N/2, N/22, N/23, ..., 2, 1. How many divisions are required? N/2k = 1  2k = N  k = log2N Binary search is O(logN), i.e., it's in the logarithmic complexity class Exercise: What is T(N)? Prove that T(N) = O(logN). T(N) = 4 + 5logN Note that log16 = 4. Let N0 = 2 and c = 9. Whenever N >= 2, T(N) = 5logN + 4 <= 5logN + 4logN = 9logN. Therefore, T(N) = O(logN).

Sorting XKCD xkcd.com/1185

Insertion Sort insertion sort: order a list of values by repetitively inserting a particular value into a sorted subset of the list To start: consider the first item to be a sorted sublist L[0:0] of length 1 Step 1: Insert second item into sorted sublist, swapping with first item as needed Now L[0:1] is sorted Step 2: Insert 3rd item into sorted sublist, swapping to left as needed Now L[0:2] is sorted ... Repeat until all values have been sorted into their proper positions L[0:N-1] is sorted Think of the list as having 2 regions: a region at the beginning of the list that is sorted among itself, and a second region to the right that is not yet sorted. One at a time, you add elements to the sorted region, until you've added all elements in the array. Each time you add a new element to the sorted region, it's at the end, and it might not be in the right position among the sorted elements. Compare it to the element to its left, swapping with that element, until it's in the right place. Makes N-1 passes over array At end of pass i, A[0:i] is sorted

Insertion Sort Example Make N-1 passes over array After pass i: L[0:i] is sorted Index Value 15 1 2 3 17 4 10 5 12 6 pass 1 pass 2 pass 3 pass 4 pass 5 pass 6 Pass i: Put L[i] in correct position relative to sorted array to its right What arrangement of data would make it slower: If its backwards! You have to slow every elt all the way over. If its sorted already, nothing has to be swapped, so it is faster.

Insertion Sort Example 44 68 191 119 37 83 82 191 45 158 130 76 153 39 25 Inserting a sorted element into a sorted chunk over and over again. Insertion sort dancing: https://www.youtube.com/watch?v=ROalU379l3U Insertion sort from Harvard's CS 50: https://www.youtube.com/watch?v=DFG-XuyPYUQ

Insertion Sort for i = 1 to N-1 elt = arr[i] j = i while(j > 0 and arr[j-1] > elt) arr[j] = arr[j-1] j = j – 1 arr[j] = elt Homework: Implement insertion sort in C Insert elt = arr[i] into sorted subarray arr[0:i-1] Keep sliding elt to the left of the array until it's in proper position j is current index for elt as its being moved left in the sorted region of the array Iterate over sorted portion once we find the right position for elt – shift elements in sorted portion that are less than elt to the rlght

Insertion Sort Analysis Worst case: the array is in reverse sorted order, and each element inserted into the sorted subarray must be swapped all the way down to arr[0] Worst case runtime? for i = 1 to N-1 elt = arr[i] j = i while(j > 0 and arr[j-1] > elt) arr[j] = arr[j-1] j = j – 1 arr[j] = elt

Selection Sort To sort a list into increasing order: For i = 0 to N-2: Find the smallest item in the array, and swap it with the first array element Find the second smallest item in the array, and swap it with the second array element ... For i = 0 to N-2: Step i: Find the smallest item in arr[i:N-1] and swap it with arr[i]