Searching a vector We can search for one occurrence, return true/false or the index of occurrence Search the vector starting from the beginning Stop searching.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
HST 952 Computing for Biomedical Scientists Lecture 9.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Chapter 16: Searching, Sorting, and the vector Type.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Analysis of Algorithms
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 211 Data Structures Lecture 13
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
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.
Data Structure Introduction.
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.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
CSCI 51 Introduction to Programming March 12, 2009.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
A Computer Science Tapestry 10.1 From practice to theory and back again In theory there is no difference between theory and practice, but not in practice.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Announcements Midterm 2 is on TODAY, December 19 th at 19:40. Extra Recitation TODAY at 15:40-18:30 in FENS L062 Midterm Places: if (LastName
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
A Computer Science Tapestry 8.1 Vectors l Vectors are homogeneous collections with random access ä Store the same type/class of object, e.g., int, string,
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
Vectors of structs We can define vectors of structs struct student { unsigned int id; string name, lastname; double gpa; }; vector class(11); // a vector.
A Computer Science Tapestry 8.1 Vectors l Vectors are homogeneous collections with random access  Store the same type/class of object, e.g., int, string,
Data Structures I (CPCS-204)
Introduction to Search Algorithms
Announcements HW6 due this Wednesday
Announcements Final Exam on August 17th Wednesday at 16:00.
Arrays … The Sequel Applications and Extensions
Announcements Midterm2 grades were announced
Chapter 8 Search and Sort
Announcements HW6 due this Wednesday
24 Searching and Sorting.
Vectors Vectors are homogeneous collections with random access
Vectors Vectors are homogeneous collections with random access
Vectors Vectors are homogeneous collections with random access
Vectors Vectors are homogeneous collections with random access
Presentation transcript:

Searching a vector We can search for one occurrence, return true/false or the index of occurrence Search the vector starting from the beginning Stop searching when match is found We can search and count the number of occurrences and return count Search entire vector Similar to one occurrence search, but do not stop after first occurrence We can search for many occurrences, but return occurrences in another vector rather than count In all these cases, we search the vector sequentially starting from the beginning This type of search is called “sequential search”

Counting search int countmatches(const vector & a, const string& s) // post: returns # occurrences of s in a { int count = 0; int k; for(k=0; k < a.size(); k++) { if (a[k] == s) { count++; } return count; } How can we change this code to return the index of the first occurrence? see next slide

One occurrence search int firstmatch(const vector & a, const string& s) // post: returns the index of occurrence of s in a, -1 // otherwise { int k; for(k=0; k < a.size(); k++) { if (a[k] == s) { return k; } return -1; } Does not search the entire array if one match is found good for efficiency purposes How could you modify this to return true/false?

Collecting search Collect the occurrences in another vector void collect(const vector & a, vector & matches) // pre: matches is empty // post: matches contains all elements of a with // first letter 'A' { int k; for (k=0; k < a.size(); k++) { if (a[k].substr(0,1) == "A") { matches.push_back(a[k]); }

Binary search Alternative to sequential search for sorted vectors If a vector is sorted we can use the sorted property to eliminate half of the vector elements with one comparison What number (between 1 and 100) do we guess first in number guessing game? Idea of creating program to do binary search Check the middle element If it has the searched value, then you’re done! If not, eliminate half of the elements of the vector search the rest using the same idea continue until match is found or there is no match how could you understand that there is no match? let’s develop the algorithm on an example we need two index values, low and high, for the search space

Binary Search (search for 62) low=0 mid=7 high= low=0 mid=3 high=6 low=4 high=6 mid=5 => FOUND

Binary Search (search for 60) low = 0 mid=7 high = low=0 mid=3 high=6 low=4 high=6 mid=5 low=4 high=4 mid=4 low=5 high=4 => NO MATCH FOUND – STOP

Binary search code int bsearch(const vector & list, const string& key) // pre: list.size() == # elements in list // post: returns index of key in list, -1 if key not found { int low = 0; // leftmost possible entry int high = list.size()-1; // rightmost possible entry int mid; // middle of current range while (low <= high) { mid = (low + high)/2; if (list[mid] == key) // found key, exit search { return mid; } else if (list[mid] < key) // key in upper half { low = mid + 1; } else // key in lower half { high = mid - 1; } return -1; // not in list }

Comparing Sequential and Binary Search Given a list of N elements: Binary search makes on the order of log N operation O(log N) Linear (sequential) search takes on the order of N operations O(N)

Sorting One of the fundamental operations in Computer Science Given a randomly ordered array, sort it ascending descending Many algorithms exists some in Chapter 11 we will discuss two of them – Selection Sort (11.1.1) and Insertion Sort (11.1.2) Analysis in 11.4

Selection Sort N is the number of elements in vector/array Find smallest element, move into 0 th vector/array location examine all N elements 0.. N-1 Find next smallest element, move into 1 st location 0 th location is already the minimum examine N-1 elements 1.. N-1 Find next smallest element, move into 2 nd location 0 th and 1 st locations are already the minimum two elements examine N-2 elements 2.. N-1 Generalize for k th element, 0 <= k <= N-2 - find the minimum between k th and last element (element with index N-1) of array - swap the k th element with the minimum one

Selection Sort: The Code void SelectSort(vector & a) // pre: a contains a.size() elements // post: elements of a are sorted in non-decreasing order { int j, k, temp, minIndex, numElts = a.size(); for(k=0; k < numElts - 1; k++) { minIndex = k; // minimal element index for(j=k+1; j < numElts; j++) { if (a[j] < a[minIndex]) { minIndex = j; // new min, store index } temp = a[k]; // swap min and k-th elements a[k] = a[minIndex]; a[minIndex] = temp; }

insertion and deletion It’s easy to insert at the end of a vector, use push_back() However, if the vector is sorted and if we want to keep it sorted, then we can’t just add to the end We have to find an appropriate position to insert the element and do some shifts. If we need to delete an element from a sorted vector, how can we “close-up” the hole created by the deletion? Shift elements left by one index, decrease size We decrease size using pop_back() pop_back() changes size, not capacity

Inserting an element into a sorted array Insert NewNum which is e.g. 23 Is the array “capacity” sufficient for an extra element? What would you do to insert newNum in the right spot?

Insertion into sorted array NewNum =

Insert into sorted vector void insert(vector & a, int newnum) // NOT const vector // pre: a[0] <= … <= a[a.size()-1], a is sorted // post: newnum inserted into a, a still sorted { int count = a.size(); //size before insertion a.push_back(newnum); //increase size – newnum is inserted at // the end but the inserted value is not important int loc = count; // start searching insertion loc from end while (loc > 0 && a[loc-1] > newnum) { a[loc] = a[loc-1]; loc--; // shift right until the proper insertion cell } a[loc] = newnum; //actual insertion } See vectorproc.cpp (not in book)

What about deletion? Remove the element at a given position ( pos ) void remove(vector & a, int pos) // post: original a[pos] removed, size decreased { int lastIndex = a.size()-1; a[pos] = a[lastIndex]; a.pop_back(); } What about if vector is sorted, what changes? What’s the purpose of the pop_back() call?

Deletion from sorted vector Ex: Delete element at position Size is 9 Size is now 8 First shift all elements on the right of 3 rd element one cell to the left pop back the last element of vector

Deletion from sorted vector void remove(vector & a, int pos) // pre: a is sorted // post: original a[pos] removed, a is still sorted { int lastIndex = a.size()-1; int k; for(k=pos; k < lastIndex; k++) { a[k] = a[k+1]; } //shift all elements on the right of pos one cell left a.pop_back(); //remove the last element of the array } Does pop_back() actually remove an element? no, it just decreases the size so that the last element becomes unreachable Capacity remains the same See vectorproc.cpp (not in book)

Insertion Sort Insert 1 st element before or after 0 th first 2 sorted Insert 2 nd element (element with index 2) in proper location first 3 sorted Generalize insert k th element (element with index k) within first k elements first k+1 sorted run this for all k between 1.. N-1

Insertion Sort – The Code void InsertSort(vector & a) // precondition: a contains a.size() elements // postcondition: elements of a are sorted in non- decreasing order { int k,loc, numElts = a.size(); for(k=1; k < numElts; k++) { string hold = a[k]; // insert this element loc = k; // location for insertion // shift elements to make room for hold (i.e. a[k]) while (0 < loc && hold < a[loc-1]) { a[loc] = a[loc-1]; loc--; } a[loc] = hold; }

Which one faster? No exact answer! It depends on the vector to be sorted already ordered, totally disordered, random Let’s see how many iterations do we have in Selection Sort Outer loop  k: 0.. N-2 Inner loop  j: k+1.. N-1 (N-1) + (N-2) + (N-3) = N(N-1)/2 = (N 2 – N)/2 Worst case, best case, average case??? Complexity is O(N 2 ) order of N 2 Big-oh notation used to describe algorithmic complexities. This is not a precise amount of operations and comparisons. Minor terms and coefficients are not taken into consideration

Which one faster? Let’s analyze Insertion Sort Outer loop  k: 1.. N-1 N-1 iterations for the outer loop What about inner loop? worst case, best case differ worst case: k times, so total is …+(N-1) = N(N-1)/2, complexity is O(N 2 ) best case: inner loop does not iterate, complexity is O(N), but best case complexity analysis is not done too often what are the best and worst cases? average case: inner loop iterates k/2 times, order is still O(N 2 ) Complexities of both Selection and Insertion Sort are O(N 2 ) Which one would you prefer to use? Let’s run timesorts.cpp (modified from book) – needs several Tapestry.h and.cpp files in the project folder to run (comparer.h, ctimer.h, ctimer.cpp, prompt.h, prompt.cpp, randgen.h, randgen.cpp, sortall.h, sortall.cpp – red ones to be added to the project).

Built-in Arrays C++ native array type Two versions fixed size arrays array size is fixed and must be specified with a constant expression at the declaration we will see this type now array pointers array size is dynamically allocated we will not see it in this course use of both types are the same except definition vector versus built-in arrays vector is a class based on built-in arrays vector has member functions and operators, built-in arrays do NOT vector is more flexible, but slower

Built-in Array declaration As we said, we will discuss fixed size built-in arrays, not the pointer version with dynamic allocation size must be able to be determined at compile time constant, literal or an expression that involves constants and literals only const int CLASSSIZE = 100; // constant string names[CLASSIZE]; // array of 100 strings double grades[CLASSIZE*5]; // array of 500 doubles int list[200]; // array of 200 integers The following array declaration is INVALID int size; cout "Enter how many students ? "; cin >> size; string names[size]; // array size cannot be a variable

Built-in array initialization at declaration You may specify a list of initial values at declaration. See following example: string dayNames [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday"}; dayNames is an array with 7 elements of type string 0 th element is “Sunday”, 1 st is “Monday”,... not necessary to specify size (7), since the number of elements make the size clear but you can specify the size, if you wish string dayNames [7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday"};

Assignment rules in arrays vectors with the same element type can be assigned to each other by = LHS vector becomes the same as the RHS vector size and capacity also become the same Built-in arrays cannot be assigned to each other by = int coins[] ={1,5,10,25}; int temp[4]; temp = coins; // illegal temp[1] = coins[2]; // legal – array element assignment How can we assign coins to temp? element by element for (i=0; i<4; i++) temp[i] = coins[i];

Passing built-in arrays as parameters A built-in array can be passed only as reference parameter or const-reference parameter cannot be passed as value parameter But, we do not use ampersand character, &, at parameter declaration and we do not specify the array size in array parameter however array size is generally passed as another integer parameter since we do not have a size() member function for built-in arrays void Change(int list[], int numElts); void Print(const int list[], int numElts); reference parameter const-reference parameter

Built-in array demo See fixlist.cpp (slightly modified from the version in book) Why did we use const in Print ? to avoid accidental changes in array list Why did we pass numElts as parameter for the number of elements in the array? because we don’t know the total number of elements in the array while writing the functions

Example – Fibonacci numbers Used in many areas of Mathematics and Computer Science F 0 = 1 F 1 = 1 F n = F n-1 + F n You can see many examples of Fibonacci numbers in nature E.g. Increase of number of branches of trees in time See for more exampleshttp:// const int MAX_SIZE = 100; int list[MAX_SIZE]; int k; list[0] = list[1] = 1; for (k=2; k < MAX_SIZE, k++) { list[k] = list[k-1]+list[k-2]; }

Use of strings as arrays Characters in a string can be referred as an array using [ ] string s="cs201";... s[0] = 'n'; // makes 0 th character of s 'n'... for (k=0; k<s.length(); k++) cout << s[k] << " "; In general, s[k] means s.at(k)

The Matrix To represent two dimensional arrays Given rows and columns: We define a matrix as a vector of vectors vector > mat(rows, vector (cols)); vector > mat(3, vector (5)); mymatrix[2][3] = 100; First index is for row, second is for column

Possible Matrix definitions Possible matrix declarations 4 different declarations vector > matrix_name; empty matrix (zero rows, zero columns) vector > matrix_name(rows); matrix with rows many empty vector ’s vector > matrix_name(rows, vector (cols)); matrix with rows*cols elements all initialized to 0 vector > matrix_name(rows, vector (cols, init_value)); matrix with rows*cols elements all initialized to init_value

To get the size of rows and columns mymatrix.size() number of rows in matrix mymatrix[0].size() number of columns in matrix Example: Let’s run matdemo.cpp