CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.

Slides:



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

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Introduction to Computer Science Theory
Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Analysis of Algorithms CS Data Structures Section 2.6.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01.
Sorting Algorithms Insertion and Radix Sort. Insertion Sort One by one, each as yet unsorted array element is inserted into its proper place with respect.
1 CSE1301 Computer Programming: Lecture 30 Linked Lists (Part 2)
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
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.
Computer Science Searching & Sorting.
10/14/2015cosc237/recursion1 Recursion A method of defining a concept which refers to the concept itself A method of solving a problem by reducing it to.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
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.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSC 211 Data Structures Lecture 13
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
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.
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 & 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.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching Topics Sequential Search Binary Search.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Sorting and Searching Bubble Sort Linear Search Binary Search.
Chapter 16: Searching, Sorting, and the vector Type.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Recitation 13 Searching and Sorting.
CSE 1342 Programming Concepts
searching Concept: Linear search Binary search
Searching and Sorting Arrays
CSCE 222 Discrete Structures for Computing
Presentation transcript:

CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03

Overview Search a 1D array Sort a 1D array 2D arrays

Linear Search Goal: Find the location of a particular element in an array Process: Start at the beginning, compare each element in the array to the value being searched for Remember: Array’s aren’t self aware of their size.

Source Code int linSearch(const int arr[], int key, int arrSize) { for (int j = 0; j < arrSize; j++) if(arr[j] == key) return j; return -1; }

Binary Search Input: Sorted Array Goal: Find the location of an element in an array if it exists Process: Compare key with middle element in the array. – If key < middle, element must be in lower half of array – if key > middle, element must be in upper half of array – else key found

Sorting Classic problem in CS and very well studied Probably hundreds of sorting algorithms out there. Typically, sorted data is “easier” to work with than unsorted data Insertion Sort: – Process: Take 2 nd element and swap with 1 st if it is smaller. Take 3 rd element, insert into proper position with respect to 1 st and 2 nd. and so on…

2D Arrays 20 x 20 grid of integers access each element using 2 subscripts – cout << data[1][2]; – cin >> data[2][10]; Don’t make the mistake of – cout << data[1, 2]; int data[20][20];

2D array as matrix Write a program to read 2 matrices (of compatible size) out of a file and add them together.

?