1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.

Slides:



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

CSE Lecture 3 – Algorithms I
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
 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 and Sorting Arrays
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Problem Solving and Algorithms
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.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
February 4, 2005 Searching and Sorting Arrays. Searching.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
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.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
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.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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 & 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 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
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
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Arrays
Introduction to Search Algorithms
Topic 14 Searching and Simple Sorts
Introduction to Programming
CSC215 Lecture Algorithms.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
Topic 14 Searching and Simple Sorts
Search,Sort,Recursion.
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341

2 Today: Searching and Sorting Arrays

3 Searching Looking for a particular value in an array Linear Search –Start at the beginning (or end) and iteratively check every element Until you find it OR until you reach the end Binary Search –Start in the middle of a sorted array Eliminates ½ of the remaining search space on each iteration

4 Linear Search int test[5]={10, 42, 8, 100, -3}; int result; result = searchArray(test, 5, 100); if (result == -1) cout << “100 Not Found” << endl; else cout << “100 Found” << endl; //Returns Subscript of element or -1 if not found int searchArray(int arr[], int numElem, int target) { for(int x = 0; x < numElem; x++) { if (arr[x] == target) return x; } return (-1); }

5 Searching Also Searching –Finding the largest or smallest element in an array //Returns Subscript of largest element int //Largest value placed in val searchArray(int arr[], int numElem, int& val) { val = arr[0]; int subs = 0; for(int x = 1; x < numElem; x++) { if (arr[x] > val) { val = arr[x]; subs = x; } return subs; }

6 Binary Search

7 Array must be sorted Only searching ½ of the array in each iteration of the search int main() { int test[13]={1,5,7,8,10,11,14,16,22, 35,44,57,66}; int result = binarySearch(test,13,16); if(result == -1) cout<<“Number not found”<<endl; else cout<<“Number found”<<endl; return 0; }

8 Binary Search - Intuition [0][12][6] 16 value We ask: Is Value in the top half of the array, or the bottom half? Determine this based on comparing value to middle element

9 Binary Search - Implementation int binarySearch(int arr[], int numElem, int val) { int first = 0, last = numElem – 1, middle; while(first <= last) { middle=first+(last-first)/2; if (arr[middle]==val) return middle; else if(arr[middle]>val) last = middle – 1; else first = middle + 1; } return -1; } find the middle index If val is in bottom half If val is in top half

10 Binary Search - Intuition [0][12][6] 16 val middle = first+(last-first)/2 = 6 arr[middle] = 14 Is val >, < or == 14 > first last so : first = middle + 1 first = 7 Iter 1

11 Binary Search - Intuition [0][12][6] 16 val middle = first+(last-first)/2 = 9 arr[middle] = 35 Is val >, < or == 35 first last Iter 2 first = 7 last = 12 [9][7] < so : last = middle - 1 last = 8

12 Binary Search - Intuition [0][12][6] 16 val middle = first+(last-first)/2 = 7 arr[middle] = 16 Is val >, < or == 16 first last Iter 3 first = 7 last = 8 [9][7] = so : return middle return 7 [8]

13 Sorting

14 Motivation Searching sorted data is faster than searching unsorted data –What if the phonebook wasn’t in alphabetical order? Sorted data is usually easier to deal with Can sort ascending or descending

15 Bubble Sort Intuition 2135 swap if out of order …and so on until you make one pass without swapping

16 Bubble Sort – Swap Function void swap(int& x, int& y) { int temp = x; x = y; y = temp; }

17 Bubble Sort void bubbleSort(int arr[], int numElem) { bool swapped; do { swapped = false; for (int x = 0; x < (numElem-1); x++) { if (arr[x] > arr[x+1]) { swap(arr[x], arr[x+1]); swapped = true; } } while (swapped); }

18 Bubble Sort - Example swapped = false arr [0][1] [5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; } true 72 x=0 72

19 Bubble Sort - Example swapped = true arr [2][1] [5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; } 73 x=1

20 false – do nothing Bubble Sort - Example swapped = true arr [2][3] [5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; } x=2

21 false – do nothing Bubble Sort - Example swapped = true arr [4][3][5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; } x=3

22 Bubble Sort - Example swapped = true arr [4][5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; } x=4 19 Finished w/ 1 st Iteration of do-while loop...

23 Bubble Sort - Example swapped = false arr [1][5][0][2][3][4] 18 true Finished w/ 2nd Iteration of do-while loop...

24 Bubble Sort - Example swapped = false arr [1][5][0][2][3][4] true Finished w/ 3 rd Iteration of do-while loop... 17

25 Bubble Sort - Example swapped = false arr [1][5][0][2][3][4] true Finished w/ 4 th Iteration of do-while loop... 31

26 Bubble Sort - Example swapped = false arr [1][5][0][2][3][4] true Finished w/ 5 th Iteration of do-while loop... but still must go through one more time! 12

27 Bubble Sort - Example swapped = false arr [1][5][0][2][3][4] DONE!

28 Questions??? ?