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."

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

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.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Garfield AP Computer Science
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Sorting Algorithms What is it: An algorithm that puts the elements of a list in a certain order.
CS 307 Fundamentals of Computer ScienceSorting and Searching 1 Topic 11 Sorting and Searching "There's nothing in your head the sorting hat can't see.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Topic 9B – Array Sorting and Searching
Searching and Sorting Arrays
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
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.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Chapter 19: Searching and Sorting Algorithms
Computer Science Searching & Sorting.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
1 Sorting and Searching "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.
CSC 211 Data Structures Lecture 13
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
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 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
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.
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.
Searching CSCE 121 J. Michael Moore.
Topic 14 Searching and Simple Sorts
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Topic 14 Searching and Simple Sorts
Principles of Computing – UFCFA3-30-1
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
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Principles of Computing – UFCFA3-30-1
Presentation transcript:

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." -The Sorting Hat, Harry Potter and the Sorcerer's Stone 1

Searching  Given an array of ints find the index of the first occurrence of a target int  Given the above array and a target of 27 the method returns 2  What if not present?  What if more than one occurrence? index value

clicker  Given an array with 1,000,000 elements in random order, how many elements do you expect to look at when searching if: item presentitem not present A. 01,000,000 B. 500,0001,000,000 C.1,000,0001,000,000 D. 1, ,000 E. 201,000,000 3

linear or sequential search 4

Sorting XKCD cd.com/ 1185/ cd.com/ 1185/ 5

Sorting  A fundamental application for computers  Done to make finding data (searching) faster  Many different algorithms for sorting  One of the difficulties with sorting is working with a fixed size storage container (array) –if resize, that is expensive (slow) –Trying to apply a human technique of sorting can be difficult –try sorting a pile of papers and clearly write out the algorithm you follow 6

7 Selection Sort  To sort a list into ascending order: –Find the smallest item in an array, the minimum –Put that value in the first element of the array Where to put the value that was in the first location? –And now…?

8 Selection Sort in Practice animation of selection sort algorithm

9 Implementation of Selection Sort  Include println commands to trace the sort

clicker  Determine how long it takes to sort an array with 50,000 elements in random order using selection sort. When the number of elements is increased to 100,000 how long will it take to sort the array? A.About the same B.1.5 times as long C.2 times as long D.4 times as long E.16 times as long 10

11 Insertion Sort  Another of the Simple sort  The first item is sorted  Compare the second item to the first –if smaller swap  Third item, compare to item next to it –need to swap –after swap compare again  And so forth…

12 Insertion Sort in Practice animation of insertion sort algorithm

Binary Search 13

14 Searching in a Sorted List  If items are sorted then we can divide and conquer  dividing your work in half with each step –generally a good thing  The Binary Search on List in Ascending order –Start at middle of list –is that the item? –If not is it less than or greater than the item? –less than, move to second half of list –greater than, move to first half of list –repeat until found or sub list size = 0

15 Binary Search list low item middle itemhigh item Is middle item what we are looking for? If not is it more or less than the target item? (Assume lower) list low middlehigh item item item and so forth…

16 Implement Binary Search

17 Trace When Key == 3 Trace When Key == 30 Variables of Interest?

clicker  Given an array with 1,000,000 elements in sorted order, how many elements do you expect to look at when searching (with binary search) for a value if: item presentitem not present A ,000 B C. 11,000,000 D. 1, ,000 E. 1,000 1,000 18