# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
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
COPYRIGHT 2003: Dr. David Scanlan, CSUS OBJECTIVES: Explain the need for arrays. Coding an array. Basic algorithms: Largest, Smallest, Sum, Standard Deviation,
CPS120: Introduction to Computer Science Searching and Sorting.
Match-and-Stop Search Will find FIRST match Use Boolean variable to denote whether a match has been found or not Found initially False If a match is found,
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Searching and Sorting Arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Chapter 8 ARRAYS Continued
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
Chapter 19: Searching and Sorting Algorithms
Searching and Sorting Chapter Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
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.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Chapter 6: Arrays: Lists and Tables
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
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.
Bubble Sort.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Controlling Program Flow with Looping Structures
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Recitation 13 Searching and Sorting.
Chapter 7 Arrays.
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
Introduction to Programming
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
24 Searching and Sorting.
CSCE 222 Discrete Structures for Computing
Presentation transcript:

# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010

# 2# 2 Excel Sorting Data 1. select 2. choose the type of sort in the Data tab

# 3# 3 Sorting Algorithms Selection Sort Bubble Sort There are numerous ways to perform a sort. We will consider two: In the examples in this lecture we will sort arrays in ascending order but we could also have sorted in descending order.

# 4# 4 CS 105 Spring 2010 Selection Sort To sort a list in ascending order: Step 1 Swap the last element of the list with the element that contains the largest value. Step 2 Repeat Step 1 on the list with the last element removed until there is only one element remaining. See visualization of algorithm here.here

# 5# 5 CS 105 Spring 2010 Sub SortArray_selection() Dim strNames(1024) As String Dim intCount As Integer intCount = 0 ' Number of strings ' Read down column A until we find an empty cell Do While Cells(intCount + 1, 1).Value <> "" strNames(intCount + 1) = Cells(intCount + 1, 1).Value intCount = intCount + 1 Loop ' Call the SelectionSort sub (see code on next slide) SelectionSort strNames, intCount ' Display sorted strings in column B Do While intCount > 0 Cells(intCount, 2).Value = strNames(intCount) intCount = intCount - 1 Loop End Sub

# 6# 6 CS 105 Spring 2010 Sub SelectionSort(strNames() As String, intCount As Integer) Dim intIndex As Integer, intPosition As Integer, intFound as Integer Dim strTemp As String ' Find the largest (alphabetically) string to be placed in the last ' position in the array and then repeat to find the next largest For intPosition = intCount To 2 Step -1 ' start with a guess intFound = 1 ' check to see if our guess is the largest and if not then store ' both value and position of the larger value For intIndex = 2 To intPosition If strNames(intIndex) > strNames(intFound) Then intFound = intIndex End If Next intIndex ' swap the largest value with the value at intPosition strTemp = strNames(intPosition) strNames(intPosition) = strNames(intFound) strNames(intFound) = strTemp Next intPosition End Sub

# 7# 7 CS 105 Spring 2010 Bubble Sort To sort a list in ascending order: Step 1 starting with the first two elements in the list swap (if necessary) to make the second element the largest. Repeat with second and third elements and then third and fourth … until last element of the list is the largest. Step 2 Repeat Step 1 on the list with the largest element removed until there is only one element remaining. See visualization of algorithm here.here

# 8# 8 CS 105 Spring 2010 Sub SortArray_bubble() Dim strNames(1024) As String Dim intCount As Integer intCount = 0 ' Number of strings ' Read down column A until we find an empty cell Do While Cells(intCount + 1, 1).Value <> "" strNames(intCount + 1) = Cells(intCount + 1, 1).Value intCount = intCount + 1 Loop ' Call the BubbleSort sub (see code on next slide) BubbleSort strNames, intCount ' Display sorted strings in column B Do While intCount > 0 Cells(intCount, 2).Value = strNames(intCount) intCount = intCount - 1 Loop End Sub

# 9# 9 CS 105 Spring 2010 Sub BubbleSort(strNames() As String, intCount As Integer) Dim intIndex As Integer, intPosition As Integer Dim strTemp As String ' Find the largest (alphabetically) string to be ' placed in the last position in the array ' and then repeat to find the next largest For intPosition = intCount To 2 Step -1 ' compare two and swap (bubble up) the largest string ' if necessary For intIndex = 2 To intPosition If strNames(intIndex) < strNames(intIndex - 1) Then strTemp = strNames(intIndex) strNames(intIndex) = strNames(intIndex - 1) strNames(intIndex - 1) = strTemp End If Next intIndex Next intPosition End Sub

# 10 Best Sorting Algorithm? Time Counting number of comparisons (, =, …) Counting the number of swaps There are many ways to compare sorting algorithms Determining the best algorithm based on time is flawed since it is dependent on the particular hardware on which the test is performed. Counting the number of comparisons and the number of swaps is independent of the hardware.

# 11 CS 105 Spring 2010 An Improved Bubble Sort We would like to reduce the number of comparisons. One way to do this is that at each step we keep track of whether any swaps were made. If no swaps were made we know that the list had to be sorted. That is, we can stop our sort early! We will use a Boolean “flag” variable to keep track of any swaps that are made for each step.

# 12 CS 105 Spring 2010 Sub BubbleSort(strNames() As String, intCount As Integer) Dim intIndex As Integer, intPosition As Integer Dim strTemp As String Dim blnSwap As Boolean For intPosition = intCount To 2 Step -1 blnSwap = False For intIndex = 2 To intPosition If strNames(intIndex) < strNames(intIndex - 1) Then strTemp = strNames(intIndex) strNames(intIndex) = strNames(intIndex - 1) strNames(intIndex - 1) = strTemp blnSwap = True End If Next intIndex If blnSwap = False Then Exit Sub End If Next intPosition End Sub

# 13 Searching Algorithms Binary Search Sorting makes searching easier. There are numerous ways to perform a search. We will consider just one:

# 14 Searching a List Binary Search requires that the list be sorted. In this example the list is sorted in ascending order. To search for a target value in a list we Step 1 compare the target with the middle element in the list and if we find a match then we are done. Step 2 If the target is less(greater) than the middle value then we discard the right(left) half of the list. Step 3 Repeat Step 1--- applied to only the half of the list remaining from Step 2 until a match is found or the list is empty. CS 105 Spring 2010

# 15 CS 105 Spring 2010 Sub SearchArray_binarysearch() Dim strNames(1024) As String Dim strTarget As String Dim intCount As Integer, intIndex As Integer intCount = 0 ' Read values from column A into strNames Do While Cells(intCount + 1, 1).Value <> "" strNames(intCount + 1) = Cells(intCount + 1, 1).Value intCount = intCount + 1 Loop ' Sort the values in the strNames array BubbleSort strNames, intCount ' Ask the user for a name to search for strTarget = InputBox("Enter a name") ' Call BinarySearch to search the array strNames for strTarget intIndex = BinarySearch(strNames, intCount, strTarget) ' If intIndex = -1 then no match found If intIndex = -1 Then MsgBox "No match found for " & strTarget Else MsgBox "Match found at index = " & intIndex End If End Sub

# 16 CS 105 Spring 2010 Function BinarySearch(strNames() As String, intCount As Integer, strTarget As String) As Integer Dim intMidpt As Integer Dim intFirst As Integer, intLast As Integer BinarySearch = -1 ' BinarySearch = -1 means no match found intFirst = 1 intLast = intCount ' While array not empty (intFirst <= intLast) and no match (BinarySearch = -1) Do While (intFirst <= intLast) And (BinarySearch = -1) intMidpt = (intFirst + intLast) / 2 If strTarget > strNames(intMidpt) Then intFirst = intMidpt + 1 ElseIf strTarget < strNames(intMidpt) Then intLast = intMidpt - 1 Else BinarySearch = intMidpt End If Loop End Function