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,

Slides:



Advertisements
Similar presentations
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Advertisements

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.
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
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.
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
Searching and Sorting Linear Search Binary Search Selection Sort
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Simple Sorting Algorithms
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
 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 and Sorting Arrays
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
VB CSCI130 Instructor: Dr. Lynn Ziegler, Slides originally designed by Dr. Imad Rahal, updated by Lynn Ziegler LAYEROrder High-order P.L.: Visual Basic1.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 16: Searching, Sorting, and the vector Type.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
1 Arrays 2: Sorting and Searching Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
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.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Two Forms Please use speaker notes for additional information!
Bubble Sort.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
ALGORITHMS.
VB CSCI130 Instructor: Dr. Imad Rahal LAYEROrder Application SW: Excel & Access2 High-order P.L.: Visual Basic1 Low-order P.L.: Assembly3 System Software:
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Insertion Sort while some elements unsorted: Using linear search, find the location in the sorted portion where the 1 st element of the unsorted portion.
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.
Data Structures Arrays and Lists Part 2 More List Operations.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 9: Sorting and Searching Arrays
Lecture 14 Searching and Sorting Richard Gesick.
Alg2_1c Extra Material for Alg2_1
3.1 Algorithms (and real programming examples).
Chapter 7 Arrays.
Algorithm design and Analysis
Sorting Given a[0], a[1], ..., a[n-1] reorder entries so that
Computer Science 111 Fundamentals of Computer Programming I
And now for something completely different . . .
Chapter 8 Arrays Objectives
Lecture 11 Searching and Sorting Richard Gesick.
Lecture Set 9 Arrays, Collections and Repetition
Chapter 8 Arrays Objectives
COMP108 Algorithmic Foundations Searching
COMP108 Algorithmic Foundations Searching
Applications of Arrays
Presentation transcript:

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, we set it to True Dim Found As Boolean Found = False POS = 0 SearchValue = Inputbox… Do While (Found=false and POS<(Array size))‏ POS = POS +1 If Array(POS) = SearchValue Then Found=True End If Loop If Found= True then Print success at location POS Else Print Failure End If

Match-and-Stop Search Read an array of 75 runner names and array of 75 runner times from a file and search for a specific name input through an inputbox If found, display rank and time Else, display not found What would happen if we didn’t use POS<(Array size) ? What would happen if the else is included in the loop and not outside

Sequential Search (2) Exhaustive search (must check all array elements)‏ E.g. find all values less than a given value, maximum, or minimum Must scan whole array We use a For Loop For Pos = 1 to (CTR)‏ If condition is true then do action

Bubble sort is one of the simple algorithms to sort data Goes through a list several times (passes)‏ On every pass, bubbles largest number to end of array compares a number with its right neighbor If the number is larger, it is swapped/exchanged with its neighbor Larger numbers are said to “bubble down” to their proper places in the list After every pass, we place a number in its proper position so the number of comparisons made is decreased by 1 Bubble Sort

Algorithm: sort N numbers Input numbers to be sorted into an array of size N Make N-1 passes through the array Compare each number with its right neighbor and swap them if they are out of order Print sorted arrays Note that on pass 1, we make N-1 comparisons Pass 2, we make N-2 comparisons … Pass x, we make N-x comparisons How do we swap two values X and Y?

Swapping in Bubble Sort List(I) = List(I+1)‏ List(I+1) = List(I)‏ ???? Lost List(I)! We need to save it in a temporary variable before storing List(I+1) in it Temp = List(I)‏ List(I) = List(I+1)‏ List(I+1) = Temp

Bubble Sort Example Private Sub Sortbutton_Click()‏ Dim MyNumbers(1 to 100) As Integer Dim Pos As Integer, Pass As Integer, CTR As Integer, Temp As Integer, MyNumber As Integer, Comp As Integer Open App.Path & "\Numbers.txt" for Input As #1 CTR = 0 Do Until EOF(1)‏ CTR = CTR + 1 Input #1,MyNumbers(Count)‏ Loop Close #1 ‘ print all array numbers (before sorting) on same line Results.Print "The unsorted list is:" For Pos = 1 To CTR picResult.Print MyNumbers(Pos), Next Pos picResult.Print ‘to go to a new line

Bubble Sort For Pass = 1 To CTR-1 For Pos= 1 To (CTR – Pass)‏ If MyNumbers(Pos) > MyNumbers(Pos +1) Then Temp = MyNumbers(Pos)‏ MyNumbers(Pos) = MyNumbers(Pos +1)‏ MyNumbers(Pos +1) = Temp End If Next Pos Next Pass Results.Print "The sorted list becomes:" For Pos = 1 To CTR Results.Print MyNumbers(Pos); Next Pos End Sub