Manipulating lists of data

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
Sorting Large Files Part One:  Why even bother?  And a simple solution.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Algorithm Efficiency and Sorting
Selection Sort, Insertion Sort, Bubble, & Shellsort
Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log.
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.
Section 8.4 Insertion Sort CS Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.
(c) , University of Washington
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Searching and Sorting Topics Sequential Search on an Unordered File
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
CSC 211 Data Structures Lecture 13
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.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
3 – SIMPLE SORTING ALGORITHMS
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
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.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Copyright © Curt Hill Sorting Ordering an array.
Searching Topics Sequential Search Binary Search.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
CMPT 120 Topic: Searching – Part 2
May 17th – Comparison Sorts
Simple Sorting Algorithms
Sorting by Tammy Bailey
Algorithms and Data Structures
Teach A level Computing: Algorithms and Data Structures
Disk Storage, Basic File Structures, and Buffer Management
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Unit IV : Searching and sorting Techniques
Complexity Present sorting methods. Binary search. Other measures.
Searching and Sorting Topics Sequential Search on an Unordered File
ITEC 2620M Introduction to Data Structures
Data Structures and Algorithms
Chapter 8 Search and Sort
Searching and Sorting Topics Sequential Search on an Unordered File
Manipulating lists of data
8/04/2009 Many thanks to David Sun for some of the included slides!
Searching and Sorting 1-D Arrays
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
Sub-Quadratic Sorting Algorithms
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.
Searching and Sorting Arrays
Algorithms and Data Structures
Analysis of Algorithms
Ch. 2: Getting Started.
CSC 143 Java Sorting.
CENG 351 Data Management and File Structures
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Manipulating lists of data Searching & Sorting I Manipulating lists of data for quick retrieval

Searching and Sorting “Which are the top three college football teams?” “Who was the baseball player with the longest unbroken string of games?” “Which Presidential Candidate has the best chance of winning?” “What are your top five recreational activities?”

Common Problems Computers are often used to do two things Search: find a particular item or type of item. Sort: put items in their proper order. Who searches and sorts? Airlines: find your reservation, ticket, seat Catalog retailers/e-tailers: who are you? Have you ordered before? What did you order? Did you pay? What is the best selling item? Credit Card Companies: what did you buy? ... almost any company

Olsen’s Law of Computer Performance There are only three types of computers in the world: Too small Too slow All of the above.

Searching Does a search have to be fast ? (Yes!) How can we make the search faster ? By keeping the records in some order Using an efficient search algorithm Search algorithms Sequential search Binary search

Sequential Search on an Unordered File Searches records in sequence. Get the search criterion from the user Get the first record from the file While the record doesn’t match the criterion && there are still more records in the file do get the next record When do we know: The file contained a matching record? The file did not contain a matching record?

Sequential Search of an Ordered File Search the file in sequence when the file is in a known order. Get the search criterion from the user Get the first record from the file While the record is less than the criterion do get the next record If the record matches the criterion then success else there is no match in the file. When do we know: The file contained a matching record? The file did not contain a matching record?

Sequential Search of Ordered vs. Unordered List Customer list sorted in alphabetical order by last names. How would the search for John Adams on the ordered list compare to the search on the unordered list? Ordered List if John Adams was in the list ? if John Adams was not in the list ? Unordered List

Ordered vs Unordered (continued) How about George Washington ? unordered in the file not in the file ordered James Madison ?

More Searching Overall, ordered files yield no consistent improvement if we’re using the sequential search. Improvement depends on place in order. “Adams” --- big difference “Washington” --- little difference How else could we search an ordered file?

Binary Search Search algorithm for ordered lists of known length. Binary Search --- named because each step divides the region of uncertainty in half.

How Binary Search Works Always look at the center value. Each time you get to get to discard half of the remaining list. Is this fast ?

How fast is Binary Search ? Worst case : 11 Items in the list took 4 tries How about a list with 32 items ? 1st try - list has 16 items 2nd try - list has 8 items 3rd try - list has 4 items 4th try - list has 2 items 5th try - list has 1 item

More examples List has 512 items List has 250 items 1st try - 256 items 2nd try - 128 items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item List has 250 items 1st try - 125 items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item

What’s the pattern ? List of 11 took 4 tries List of 32 took 5 tries 32 = 25 and 512 = 29 9 < 11 < 16 23 < 11 < 24 128 < 250 < 256 27 < 250 < 28

The fastest ! How long (worst case) will it take to find an item in a list 30,000 items long ? 210 = 1024 213 = 8192 211 = 2048 214 = 16384 212 = 4096 215 = 32768 So it will take 15 tries. It only takes 15 tries to find what we want out of 30,000 items.

Twenty Questions Ask 20 “yes” or “no” questions. How many items can you distinguish? 220 items ~ 10(20/lg2(10)) ~ 10(20/3.32) ~ 10(6.02)

Lg n Resolution ~ 2n where n is the number of questions. “The binary search algorithm runs in lg n time.” lg n means the log to the base 2 of some value of n 8 = 23 lg 8 = 3 16 = 24 lg 16 = 4 There are no search algorithms that run faster than lg n time. Sequential search runs in linear time meaning execution time is proportional to n.

Courses at UMBC Algorithms - CMSC 441 Cryptology - CMSC 443 Studies algorithms and their speed Cryptology - CMSC 443 The study of making & breaking codes - write programs that can break the code like Pdeo eo pda yknnayp wjosan

Searching and Sorting (continued) We have a very fast search algorithm - Binary search But, the list has to be sorted, before we can search it with binary search. To sort the list efficiently, we also need a fast sort algorithm.

Some Sort Algorithms Bubble Sort Heap Sort Selection Sort Merge Sort Insertion Sort Quick Sort In an effort to find a very fast sorting algorithm, we have many known sorting algorithms. Bubble sort is the slowest, running in n2 time. Too slow !

Speed of Sorting Algorithms Most Sorting algorithms run in n lg n time for the worst case. Quick Sort runs a little faster for the average case. So it is usually the sort that’s used. The algorithm for Quick Sort is quite complicated. It is shown in your book. There is a pre-written function called qsort in the C standard library.

Insertion Sort Insertion sort is almost as fast, and it is easy to understand Insertion sort works the same way as arranging your hand when playing cards. Out of the pile of unsorted cards that were dealt to you, you pick up a card and place it in your hand in the correct position relative to the cards you’re already holding.

Arranging Your Hand 7 5 7

Arranging Your Hand 5 7 5 6 7 5 6 7 K 5 6 7 8 K

Insertion Sort Unsorted - shaded 7 K 1 7 5 v 7 5 7 > 2 5 7 3 < Look at 2nd item - 5 Compare 5 to 7 5 is smaller, so move 5 to temp, leaving an empty slot in position 2 Move 7 into the empty slot, leaving position 1 open Move 5 into the open position 1 7 5 v 7 5 7 > 2 5 7 3 <

Insertion Sort Look at next item - 6 5 7 6 K 1 5 7 v 5 7 6 5 7 > 2 Compare to 1st - 5 6 is larger, so leave 5 Compare to next - 7, 6 is smaller, so move 6 to temp, leaving an empty slot Move 7 into the empty slot, leaving position 2 open Move 6 to the open 2nd position 1 5 7 v 5 7 6 5 7 > 2 5 6 7 3 <

Insertion Sort Look at next item - King 5 6 7 K Compare to 1st - 5 King is larger, so leave 5 where it is Compare to next - 6, King is larger, so leave 6 where it is Compare to next - 7 King is larger, so leave 7 where it is

Insertion Sort 5 6 7 K 8 1 5 6 7 K 8 v 5 6 7 K 8 5 6 7 K > 2 5 6 7 3 <

In-Place/Out-of-Place Sorts Insertion sort is an “in-place” sort because it can sort a list in the space occupied by the list. “Out-of-place” sorts require a separate workspace about as big as the list. Do you want to retain the unsorted list?