Manipulating lists of data

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
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
Nov 21, Fall 2006IAT 8001 Binary Search Sorting. Nov 21, Fall 2006IAT 8002 Search  Often want to search for an item in a list  In an unsorted list,
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.
Insertion Sort & Shellsort By: Andy Le CS146 – Dr. Sin Min Lee Spring 2004.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
(c) , University of Washington
Searching and Sorting Topics Sequential Search on an Unordered File
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
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.
Forensics and CS Philip Chan. CSI: Crime Scene Investigation high tech forensics tools DNA profiling Use.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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
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.
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.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching Topics Sequential Search Binary Search.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
Copyright Prentice Hall Modified by Sana odeh, NYU
CSCI 104 Sorting Algorithms
May 17th – Comparison Sorts
Introduction to Search Algorithms
Simple Sorting Algorithms
Sorting by Tammy Bailey
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
Chapter 7 Sorting Spring 14
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
Description Given a linear collection of items x1, x2, x3,….,xn
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.
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Unit IV : Searching and sorting Techniques
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting 1-D Arrays
UMBC CMSC 104 – Section 01, Fall 2016
Search,Sort,Recursion.
Searching and Sorting Topics Sequential Search on an Unordered File
Manipulating lists of data
Sub-Quadratic Sorting Algorithms
Searching and Sorting Arrays
Search,Sort,Recursion.
Simple Sorting Algorithms
Ch. 2: Getting Started.
CSC 143 Java Sorting.
Quicksort.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Simple Sorting Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
Algorithms.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

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

Common Problems There are some very common problems that computers are asked to solve: Searching through a lot of records for a specific record. Who uses this ? Airlines Companies that take phone orders Credit Card Companies ... almost any company

Searching Does this search have to be fast ? How can we make the search faster ? By keeping the records in some order By using an efficient search algorithm Search algorithms Sequential search Binary search

Sequential Search on an Unordered File 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 get the next record When do we know that there wasn’t a record in the file that matched.

Sequential Search of an Ordered File Get the search criterion from the user Get the first record from the file While the record is less than the criterion get the next record If the record matches the criterion then success else there is no match in the file. When do we know that there wasn’t a record in the file that matched ?

Sequential Search of Ordered vs. Unordered List If the order was ascending alphabetical on customer’s last names, how would the search for John Adams on the ordered list compare to the search on the unordered list if John Adams was in the list ? if John Adams was not in the list ?

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

More Searching Overall, we don’t really see much improvement if we’re using the sequential search. Maybe we need a better search algorithm. How else could we search an ordered file ?

Binary Search If we have an ordered list and we know how many things are in the list (i.e. # of records in a file), we can use a different strategy. Binary Search gets it’s name, because we are always going to divide things into two parts.

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 8 < 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 - that’s awesome !!!

Lg n We say that 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 algorithms that run faster than lg n time.

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 be really efficient, 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.

Bubble Sort void BubbleSort (int a[ ] , int size) { int i, j, temp; for (i = 0; i < size; i++) for (j = 0; j < size - 1; j++) if (a[j] > a[j+1]) temp = a[j]; a[j] = a[j + 1]; a[j+1] = temp; }

Insertion Sort Insertion sort is slower than quick sort, but not as slow as bubble sort, 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 <

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