Jan 28, 2002 Reading assignment: Deitel and Deitel Chapter 4.8, 20.2.1 -or- Linear Search Web Page and Binary Search Web Page, and Wang Section 4.6 First a little more on bubble sort: - How many times is the compare/swap executed? In the worst case, n-1 times on the first pass, n-2 on the second pass, n-3 on the third, ... 2 on the (n-2)nd pass and 1 on the (n-1)st pass. Adding these up, using a summation formula usually taught in Calc I, we get n(n-1)/2 = (n2 -n)/2. For large values of n, if we ignore constant multipliers, the dominant term is n2. We say that this algorithm is O(n2) (say "order n2" or "Big oh of n2") Searching for elements in an array - When an array is small or unsorted, you can use a linear search Just step through the array one element at a time until you find the one you're looking for. Linear search is O(n). Read Fig. 4.19 in Deitel and Deitel for an implementation - If an array is sorted, you can use the binary search algorithm. Compare the middle element in the array with what you're looking for. If it's the same - yeay, you found it! If it's larger, then you know to look in the lower half, otherwise you know to look in the upper half. Now compare the middle element in the correct half with the element you're looking for. If it's the same - yeay, you found it! If it's larger, then you know to look in the lower half of the half, otherwise you know to look in the upper half of the half. Keep splitting the list in half like this until you find it. - Binary search is O(log2(n)) Vectors - So far we have been using arrays to store lists of contiguous data all of the same type. It is essential that you understand arrays, including their idiosyncracies such as they are always passed by reference, and you need to keep track of the number of elements and pass that number to any functions accessing the array (unless it's an array of characters, which are null-terminated), etc. - In practice, we often use the standard library class called "vector". It acts much like an array, but contains many useful member functions, keeps track of the size of the vector, and manages memory when the size of the vector changes. The latter is a huge savings of programming time and will reduce your number of bugs. - #include <vector> - using std::vector; - Vectors can be of any type. Examples of declaration: vector <int> iv(20); // declares a vector of 20 ints called arr vector <float> fv(32,0) // declares fv to be a vector of 32 floats // all initialized to 0 vector <string *> sv; // declares sv to be an empty vector of // pointers to string objects - To access particular elements, use iv.at(i); or iv[i]; - The size member functions gives the number of elements in the vector vec_size = iv.size(); - Warning: arrays are strange because they are automatically passed by reference. Vectors are *not* strange, so they are automatically passed by value. This is rarely what you want (a copy is made in pass by value and you don't want to copy your entire vector!). So remember to pass your vectors by reference or by pointer. - To insert an element at the end of a vector, use push_back iv.push_back(num); - To delete an element at the back of the vector, use pop_back iv.pop_back(); - The member function begin returns a pointer to the first element, end returns a pointer just past the end - The insert member function allows you to insert at various places iv.insert(iv.end(), num); // same as push_back iv.insert(iv.begin(), num); iv.insert(iv.begin()+i, num); - To delete an element from various locations, use erase iv.erase(iv.begin()+i) - There are many more member functions besides these
Binary search program from class:
Bubble sort program from class using vectors: