This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ector If the vector has 10 elements, this algorithm requires up to nine
comparisons. If the vector has 1000 elements, this algorithm requires up to 999
comparisons As n grows larger, the n part of the expression "dominates" Big O is designed to highlight these dominant terms and ignore
terms that become unimportant as n grows an algorithm that requires a total of n1 comparisons is O(n).
Sharif University of Technology Advanced Programming:
Sessions 26 and 27: Data Structures & Algorithm 79 Big O notation An O(n) algorithm is referred to as having a linear runtime.
O(n) is often pronounced "on the order of n" or more
simply "order n.“ Sharif University of Technology Advanced Programming:
Sessions 26 and 27: Data Structures & Algorithm 80 Big O notation An algorithm that tests whether any element of a vector is
duplicated elsewhere in the vector The first element must be compared with every other element in the
vector. The second element must be compared with every other element
except the first.
… This algorithm will end up making (n 1) + (n 2) + ... + 2 + 1 or
n2/2 + n/2 comparisons As n increases, the n2 term dominates Sharif University of Technology Advanced Programming:
Sessions 26 and 27: Data Structures & Algorithm 81 Big O notation It is O(n2), which is referred to as quadratic runtime and
pronounced on the order of nsquared or more simply order
nsquared. Big O is concerned with how an algorithm's runtime grows
in
in relation to the number of items processed Big O ignores the constant Sharif University of Technology Advanced Programming:
Sessions 26 and 27: Data Structures & Algorithm 82 Linear Search Linear search is the easiest search algorithm to implement
Sometimes the simplest algorithms perform poorly
The linear search algorithm runs in O(n) time
The worst case in this algorithm is that every element must
be checked to determine whether the search key exists in
the vector If the size of the vector is doubled, the number of
comparisons that the algorithm must perform is also
doubled Sharif University of Technology Advanced Programming:
Sessions 26 and 27: Data Structures & Algorithm 83 Binary Search More efficient than the linear search algorithm Requires that the vector first be sorted Worthwhile when the vector, once sorted, will be searched
a great many times
Target=13 middle 1:
2:
3:
4:
5:
6:
7:
8:
9: 1
5
6
7 not found,
7
new search
8
10
12
13 Sharif University of Technology middle 6:
7:
8:
9: 8
10
12
13 not found,
new search middle 9: 13 found, return index 9 Advanced Programming:
Sessions 26 and 27: Data Structures & Algorithm 84 1
2 // Class that contains a vector of random integers and a function
3 // that uses binary search to find an integer.
4 #include <vector>
5 using std::vector;
6
7 class BinarySearch
8{
9
public:
10
BinarySearch( int ); // constructor initializes vector
11
int binarySearch( int ) const; // perform a binary search on vector
12
void displayElements() const; // display vector elements
13 private:
14
int size; // vector size
15
vector< int > data; // vector of ints
16
void displaySubElements( int, int ) const; // display range of values
17 }; // end class BinarySearch Sharif University of Technology Introduction to Programming:
Sessions 26 and 27: Data Structures & Algorithm 85 class BinarySearch
1
{
2 // BinarySearch class memberfunction definition.
3 #include <iostream>
public:
4 using std::cout;
BinarySearch( int );...
View Full
Document
 Fall '13
 Arash

Click to edit the document details