CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

CMSC 2021 Searching and Sorting. CMSC 2022 Review of Searching Linear (sequential) search Linear search on an ordered list Binary search.
Garfield AP Computer Science
Chapter 9: Searching, Sorting, and Algorithm Analysis
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.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Algorithm Efficiency and Sorting
Selection Sort, Insertion Sort, Bubble, & Shellsort
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.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
CSC 211 Data Structures Lecture 12
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Searching and Sorting Topics Sequential Search on an Unordered File
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Sorting List is rearranged into sorted order How is the sorted order determined? – The ItemType is responsible for determining the key to be used in comparison.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
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.
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.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching and Sorting Searching algorithms with simple arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Algorithms
CSCI 104 Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Sorting by Tammy Bailey
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
Algorithms and Data Structures
Chapter 7 Sorting Spring 14
Design and Analysis of Algorithms
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Searching and Sorting Topics Sequential Search on an Unordered File
ITEC 2620M Introduction to Data Structures
Chapter 3: The Efficiency of Algorithms
Searching and Sorting Topics Sequential Search on an Unordered File
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Sort Techniques.
Lecture 11 Searching and Sorting Richard Gesick.
Manipulating lists of data
8/04/2009 Many thanks to David Sun for some of the included slides!
UMBC CMSC 104 – Section 01, Fall 2016
Search,Sort,Recursion.
Searching and Sorting Topics Sequential Search on an Unordered File
Manipulating lists of data
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
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.
Searching and Sorting Arrays
Algorithms and Data Structures
Search,Sort,Recursion.
Principles of Computing – UFCFA3-30-1
CHAPTER 9 SORTING & SEARCHING.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File Binary Search Bubble Sort Insertion Sort Reading Sections

CMSC 104, Version 8/062L24Searching&Sorting.ppt Common Problems There are some very common problems that we use computers to solve: o Searching through a lot of records for a specific record or set of records o Placing records in order, which we call sorting There are numerous algorithms to perform searches and sorts. We will briefly explore a few common ones.

CMSC 104, Version 8/063L24Searching&Sorting.ppt Searching A question you should always ask when selecting a search algorithm is “How fast does the search have to be?” The reason is that, in general, the faster the algorithm is, the more complex it is. Bottom line: you don’t always need to use or should use the fastest algorithm. Let’s explore the following search algorithms, keeping speed in mind. o Sequential (linear) search o Binary search

CMSC 104, Version 8/064L24Searching&Sorting.ppt Sequential Search on an Unordered File Basic algorithm: Get the search criterion (key) Get the first record from the file While ( (record != key) and (still more records) ) Get the next record End_while When do we know that there wasn’t a record in the file that matched the key?

CMSC 104, Version 8/065L24Searching&Sorting.ppt Sequential Search on an Ordered File Basic algorithm: Get the search criterion (key) Get the first record from the file While ( (record < key) and (still more records) ) Get the next record End_while If ( record = key ) Then success Else there is no match in the file End_else When do we know that there wasn’t a record in the file that matched the key?

CMSC 104, Version 8/066L24Searching&Sorting.ppt Sequential Search of Ordered vs.. Unordered List Let’s do a comparison. If the order was ascending alphabetical on customer’s last names, how would the search for John Adams on the ordered list compare with the search on the unordered list? o Unordered list –if John Adams was in the list? –if John Adams was not in the list? o Ordered list –if John Adams was in the list? –if John Adams was not in the list?

CMSC 104, Version 8/067L24Searching&Sorting.ppt Ordered vs. Unordered (con’t) How about George Washington? o Unordered – if George Washington was in the list? – If George Washington was not in the list? o Ordered – if George Washington was in the list? – If George Washington was not in the list? How about James Madison?

CMSC 104, Version 8/068L24Searching&Sorting.ppt Ordered vs.. Unordered (con’t) Observation: the search is faster on an ordered list only when the item being searched for is not in the list. Also, keep in mind that the list has to first be placed in order for the ordered search. Conclusion: the efficiency of these algorithms is roughly the same. So, if we need a faster search, we need a completely different algorithm. How else could we search an ordered file?

CMSC 104, Version 8/069L24Searching&Sorting.ppt Binary Search If we have an ordered list and we know how many things are in the list (i.e., number of records in a file), we can use a different strategy. The binary search gets its name because the algorithm continually divides the list into two parts.

CMSC 104, Version 8/0610L24Searching&Sorting.ppt How a Binary Search Works Always look at the center value. Each time you get to discard half of the remaining list. Is this fast ?

CMSC 104, Version 8/0611L24Searching&Sorting.ppt How Fast is a Binary Search? Worst case: 11 items in the list took 4 tries How about the worst case for a list with 32 items ? o 1st try - list has 16 items o 2nd try - list has 8 items o 3rd try - list has 4 items o 4th try - list has 2 items o 5th try - list has 1 item

CMSC 104, Version 8/0612L24Searching&Sorting.ppt How Fast is a Binary Search? (con’t) List has 250 items 1st try 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 List has 512 items 1st try items 2nd try 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

CMSC 104, Version 8/0613L24Searching&Sorting.ppt What’s the Pattern? List of 11 took 4 tries List of 32 took 5 tries List of 250 took 8 tries List of 512 took 9 tries 32 = 2 5 and 512 = < 11 < < 11 < < 250 < < 250 < 2 8

CMSC 104, Version 8/0614L24Searching&Sorting.ppt A Very Fast Algorithm! How long (worst case) will it take to find an item in a list 30,000 items long? 2 10 = = = = = = So, it will take only 15 tries!

CMSC 104, Version 8/0615L24Searching&Sorting.ppt Lg n Efficiency We say that the binary search algorithm runs in log 2 n time. (Also written as lg n) Lg n means the log to the base 2 of some value of n. 8 = 2 3 lg 8 = 316 = 2 4 lg 16 = 4 There are no algorithms that run faster than lg n time.

CMSC 104, Version 8/0616L24Searching&Sorting.ppt Sorting So, the binary search is a very fast search algorithm. 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.

CMSC 104, Version 8/0617L24Searching&Sorting.ppt Common Sort Algorithms Bubble SortHeap Sort Selection SortMerge Sort Insertion SortQuick Sort There are many known sorting algorithms. Bubble sort is the slowest, running in n 2 time. Quick sort is the fastest, running in n lg n time. As with searching, the faster the sorting algorithm, the more complex it tends to be. We will examine two sorting algorithms: o Bubble sort o Insertion sort

CMSC 104, Version 8/0618L24Searching&Sorting.ppt Bubble Sort - Let’s Do One! CPGATOBCPGATOB

CMSC 104, Version 8/0619L24Searching&Sorting.ppt Bubble Sort Code void bubbleSort (int a[ ], int size) { int i, j, temp; for ( i = 0; i < size; i++ ) /* controls passes through the list */ { for ( j = 0; j < size - 1; j++ ) /* performs adjacent comparisons */ { if ( a[ j ] > a[ j+1 ] ) /* determines if a swap should occur */ { temp = a[ j ]; /* swap is performed */ a[ j ] = a[ j + 1 ]; a[ j+1 ] = temp; }

CMSC 104, Version 8/0620L24Searching&Sorting.ppt 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. o 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.

CMSC 104, Version 8/0621L24Searching&Sorting.ppt Arranging Your Hand 7 57

CMSC 104, Version 8/0622L24Searching&Sorting.ppt Arranging Your Hand K 5678K

CMSC 104, Version 8/0623L24Searching&Sorting.ppt Insertion Sort Unsorted - shaded 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 K 5 7 v > < 1 2 3

CMSC 104, Version 8/0624L24Searching&Sorting.ppt Insertion Sort (con’t) Look at next item - 6. Compare to 1st is larger, so leave 5. Compare to next 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 K5 7 v > <

CMSC 104, Version 8/0625L24Searching&Sorting.ppt Insertion Sort (con’t) Look at next item - King. 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. 7 K5 6

CMSC 104, Version 8/0626L24Searching&Sorting.ppt Insertion Sort (con’t) K 5 7 v > < K8 K K 8 K

CMSC 104, Version 8/0627L24Searching&Sorting.ppt Courses at UMBC Data Structures - CMSC 341 o Some mathematical analysis of various algorithms, including sorting and searching Design and Analysis of Algorithms - CMSC 441 o Detailed mathematical analysis of various algorithms Cryptology - CMSC 443 o The study of making and breaking codes