CompSci 424.1 Searching & Sorting. CompSci 424.2 Searching & Sorting The Plan  Searching  Sorting  Java Context.

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.
Garfield AP Computer Science
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
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
C++ Plus Data Structures
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
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.
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.
CSE 373 Data Structures Lecture 15
Searching and Sorting Topics Sequential Search on an Unordered File
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
1 Lecture 16: Lists and vectors Binary search, Sorting.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting with Heaps Observation: Removal of the largest item from a heap can be performed in O(log n) time Another observation: Nodes are removed in order.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
CompSci 100e Program Design and Analysis II April 26, 2011 Prof. Rodger CompSci 100e, Spring20111.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Data Structure Introduction.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
3 – SIMPLE SORTING ALGORITHMS
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
CompSci 100E 30.1 Other N log N Sorts  Binary Tree Sort  Basic Recipe o Insert into binary search tree (BST) o Do Inorder Traversal  Complexity o Create:
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.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Copyright © Curt Hill Sorting Ordering an array.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Static block can be used to check conditions before execution of main begin, Suppose we have developed an application which runs only on Windows operating.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Chapter 16: Searching, Sorting, and the vector Type.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
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.
Introduction to Search Algorithms
CSC 222: Object-Oriented Programming
Sorting by Tammy Bailey
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
Teach A level Computing: Algorithms and Data Structures
Searching.
Topic 24 sorting and searching arrays
CSE 326: Data Structures Sorting
CSE 332: Data Abstractions Sorting I
Presentation transcript:

CompSci Searching & Sorting

CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context

CompSci Searching & Sorting Search (Retrieval)  “Looking it up”  One of most fundamental operations  Without computer  Indexes  Tables of content  Card Catalogue  Reference books  Fundamental part of many computer algorithms

CompSci Searching & Sorting Linear (Sequential) Search  Plod through material, one item at a time  Always works  Can be slow  Sometimes the only way  Phone Book Example   Whose number is it?  (How could this be done faster?)

CompSci Searching & Sorting Binary Search Often can do better than linear search:  Phone Book again (Predates Computer!)  Find midpoint  Decide before or after (or direct hit)  Discard half of uncertainty  Repeat until there  Fast! (Don’t even need computer!)  What does it require (why not use all the time)?  How man extra steps if double sized book?

CompSci Searching & Sorting Hashing A way of storing info so we can go directly there to retrieve  Mail boxes in a mail room (know exactly where number 33 is.)  Hashing is a way of transforming some part of info to allow such straight-forward storage  What to use for students in classroom  Age? Last name? SSN?

CompSci Searching & Sorting Hashing  Use extra space to allow for faster operation  Collision Handling  What to do if two different items map to the same bin?  Many different solutions…

CompSci Searching & Sorting Search Performance  Linear Search ( brute force, plodding )  Proportional to amount ~N  Binary Search ( telephone book )  Proportional to log of amount ~log(N)  Hashing ( go directly to …)  Independent of amount! ~constant

CompSci Searching & Sorting Sorting (Motivation) Fundamental part of many algorithms and procedures  Required before other operations possible  E.g., binary search  Often a user requirement for manual use  E.g., phone book, dictionary, directory, index…  Get lower Postal Rates if sorted by Zip Code  Implicit requirement for “orderly” operation

CompSci Searching & Sorting Selection Sort  N items in an array named Data [ 2 | 4 | 7 | 3 | 1 | 8 | 5 ]  Find smallest of elements 0 thru N-1 of Data  Interchange this with 1st element of array Data [ _ | _ | _ | _ | _ | _ | _ ]  Find smallest of elements 1 thru N-1 of Data  Interchange this with 2nd element of array Data [ _ | _ | _ | _ | _ | _ | _ ] ...  Find smallest of elements k-1 thru N-1 of Data  Interchange this with kth element of array Data [ _ | _ | _ | _ | _ | _ | _ ] [ _ | _ | _ | _ | _ | _ | _ ] [ _ | _ | _ | _ | _ | _ | _ ]  Done when k-1 = N-1 [ _ | _ | _ | _ | _ | _ | _ ]

CompSci Searching & Sorting Selection Sort  N items in an array named Data [ 2 | 4 | 7 | 3 | 1 | 8 | 5 ]  Find smallest of elements 0 thru N-1 of Data  Interchange this with 1st element of array Data [ 1 | 4 | 7 | 3 | 2 | 8 | 5 ]  Find smallest of elements 1 thru N-1 of Data  Interchange this with 2nd element of array Data [ 1 | 2 | 7 | 3 | 4 | 8 | 5 ] ...  Find smallest of elements k-1 thru N-1 of Data  Interchange this with kth element of array Data [ 1 | 2 | 3 | 7 | 4 | 8 | 5 ] [ 1 | 2 | 3 | 4 | 7 | 8 | 5 ] [ 1 | 2 | 3 | 4 | 5 | 8 | 7 ]  Done when k-1 = N-1 [ 1 | 2 | 3 | 4 | 5 | 7 | 8 ]

CompSci Searching & Sorting Selection Sort Performance (N 2 )  Assume there are N items to be sorted  Notice that with each pass we have to make N comparisons  (actually N/2 on average)  Notice that we have to make N passes  (actually N-1)  Therefore requires N x N comparisons  (actually N*(N-1)/2 )  Performance proportional to N 2 or ~N 2

CompSci Searching & Sorting Other Simple Sorts (N 2 )  2 More simple sorts like Selection Sort  Insertion Sort  Bubble Sort  All 3 have common properties  Easy to write  Fairly slow for large amounts of data

CompSci Searching & Sorting Industrial Quality Sorts  Can do much better than simple sorts  Selection Sort is often used  Divide and conquer strategy  Partitions data into two parts  Partitions each of these parts into subparts  Etc.  Performance greatly improved over previous  Can handle any real job

CompSci Searching & Sorting Other Fast Sorts  Merge Sort  Stable  Requires extra memory  Binary Tree Sort  Heap Sort  Shell Sort  Bucket Sort  Can be extremely fast under special circumstances  (Analogy to Hashing)

CompSci Searching & Sorting Sort Performance  Slowest: ~N 2  Selection Sort  Insertion Sort, Bubble Sort  Very Fast: ~N log N  QuickSort, Binary Tree Sort  Merge Sort, Heap Sort  Quite Fast  Shell Sort  Fastest ( limited situations): ~N  Bucket Sort

CompSci Searching & Sorting Java Context (writing your own?) Don’t need to write your own -- Java includes:  For Collections static void sort(List list)  stable static int binarySearch(List list, Object key)  For Arrays (?? = int, double, …, and Object) static void sort(?? [ ] a)  Uses quicksort (not stable) static int binarySearch( ?? [ ] a, ?? key)

CompSci Searching & Sorting Practice 1. In a class you design, create an array of ints, initialise with some numeric data and print it out. 2. Utilize the sort method found in the Arrays class. Sort your array and print it out again. 3. Write your own version of selection sort and add it to your class. Compare to the sort of the Arrays class.