1 - Distributed Object Computing (DOC) Group for DRE Systems

Download Report

Transcript 1 - Distributed Object Computing (DOC) Group for DRE Systems

1
19
Searching and
Sorting
 2008 Pearson Education, Inc. All rights reserved.
2
With sobs and tears he sorted out Those of the largest size…
— Lewis Carroll
Attempt the end, and never stand to doubt;
Nothing’s so hard, but search will find it out.
— Robert Herrick
‘Tis in my memory lock’d, And you yourself shall keep the
key of it.
— William Shakespeare
It is an immutable law in business that words are words,
explanations are explanations, promises are promises — but
only performance is reality
— Harold S. Green
 2008 Pearson Education, Inc. All rights reserved.
3
OBJECTIVES
In this chapter you will learn:
 To search for a given value in a vector using
binary search.
 To sort a vector using the recursive merge sort
algorithm.
 To determine the efficiency of searching and
sorting algorithms.
 2008 Pearson Education, Inc. All rights reserved.
4
19.1
19.2
19.3
19.4
Introduction
Searching Algorithms
19.2.1 Efficiency of Linear Search
19.2.2 Binary Search
Sorting Algorithms
19.3.1 Efficiency of Selection Sort
19.3.2 Efficiency of Insertion Sort
19.3.3 Merge Sort (A Recursive Implementation)
Wrap-Up
 2008 Pearson Education, Inc. All rights reserved.
5
19.1 Introduction
• Searching data
– Determine whether a value (the search key) is present in
the data
• If so, find its location
– Algorithms
• Linear search
– Simple
• Binary search
– Faster but more complex
 2008 Pearson Education, Inc. All rights reserved.
6
19.1 Introduction (Cont.)
• Sorting data
– Place data in order
• Typically ascending or descending
• Based on one or more sort keys
– Algorithms
• Insertion sort
• Selection sort
• Merge sort
– More efficient, but more complex
 2008 Pearson Education, Inc. All rights reserved.
7
19.1 Introduction (Cont.)
• Big O notation
– Estimates worst-case runtime for an algorithm
• How hard an algorithm must work to solve a problem
 2008 Pearson Education, Inc. All rights reserved.
8
Chapter
Algorithm
Location
Searching Algorithms
7
Linear search
Section 7.7
20
Binary search
Section 19.2.2
Recursive linear search
Exercise 19.8
Recursive binary search
Exercise 19.9
Binary tree search
Section 20.7
Linear search of a linked list
Exercise 20.21
binary_search standard
library function
Section 22.5.6
21
23
Fig. 19.1 | Searching and sorting algorithms in this text. (Part 1 of 2)
 2008 Pearson Education, Inc. All rights reserved.
9
Chapter
Algorithm
Location
Sorting Algorithms
7
Insertion sort
Section 7.8
8
Selection sort
Section 8.6
20
Recursive merge sort
Section 19.3.3
Bubble sort
Exercises 19.5 and 19.6
Bucket sort
Exercise 19.7
Recursive quicksort
Exercise 19.10
21
Binary tree sort
Section 20.7
23
sort standard library function
Section 22.5.6
Heap sort
Section 22.5.12
Fig. 19.1 | Searching and sorting algorithms in this text. (Part 2 of 2)
 2008 Pearson Education, Inc. All rights reserved.
10
19.2 Searching Algorithms
• Searching algorithms
– Find element that matches a given search key
• If such an element does exist
– Major difference between search algorithms
• Amount of effort they require to complete search
– Particularly dependent on number of data elements
– Can be described with Big O notation
 2008 Pearson Education, Inc. All rights reserved.
11
19.2.1 Efficiency of Linear Search
• Big O notation
– Measures runtime growth of an algorithm relative to
number of items processed
• Highlights dominant terms
• Ignores terms that become unimportant as n grows
• Ignores constant factors
 2008 Pearson Education, Inc. All rights reserved.
12
19.2.1 Efficiency of Linear Search (Cont.)
• Big O notation (Cont.)
– Constant runtime
• Number of operations performed by algorithm is constant
– Does not grow as number of items increases
• Represented in Big O notation as O(1)
– Pronounced “on the order of 1” or “order 1”
• Example
– Test if the first element of an n-vector is equal to the
second element
• Always takes one comparison, no matter how large
the vector
 2008 Pearson Education, Inc. All rights reserved.
13
19.2.1 Efficiency of Linear Search (Cont.)
• Big O notation (Cont.)
– Linear runtime
• Number of operations performed by algorithm grows linearly
with number of items
• Represented in Big O notation as O(n)
– Pronounced “on the order of n” or “order n”
• Example
– Test if the first element of an n-vector is equal to any
other element
• Takes n - 1 comparisons
• n term dominates, -1 is ignored
 2008 Pearson Education, Inc. All rights reserved.
14
19.2.1 Efficiency of Linear Search (Cont.)
• Big O notation (Cont.)
– Quadratic runtime
• Number of operations performed by algorithm grows as the
square of the number of items
• Represented in Big O notation as O(n2)
– Pronounced “on the order of n2” or “order n2”
• Example
– Test if any element of an n-vector is equal to any other
element
• Takes n2/2 – n/2 comparisons
• n2 term dominates, constant 1/2 is ignored, -n/2 is
ignored
 2008 Pearson Education, Inc. All rights reserved.
15
19.2.1 Efficiency of Linear Search (Cont.)
• Efficiency of linear search
– Linear search runs in O(n) time
• Worst case: every element must be checked
• If size of the vector doubles, number of comparisons also
doubles
 2008 Pearson Education, Inc. All rights reserved.
16
Performance Tip 19.1
Sometimes the simplest algorithms perform
poorly. Their virtue is that they are easy to
program, test and debug. Sometimes more
complex algorithms are required to realize
maximum performance.
 2008 Pearson Education, Inc. All rights reserved.
17
19.2.2 Binary Search
• Binary search algorithm
– Requires that the vector first be sorted
• Can be performed by Standard Library function sort
– Takes two random-access iterators
– Sorts elements in ascending order
– First iteration (assuming sorted ascending order)
• Test the middle element in the vector
– If it matches search key, the algorithm ends
– If it is greater than search key, continue with only the
first half of the vector
– If it is less than search key, continue with only the second
half of the vector
 2008 Pearson Education, Inc. All rights reserved.
18
19.2.2 Binary Search (Cont.)
• Binary search algorithm (Cont.)
– Subsequent iterations
• Test the middle element in the remaining subvector
– If it matches search key, the algorithm ends
– If not, eliminate half of the subvector and continue
– Terminates when
• Element matching search key is found
• Current subvector is reduced to zero size
– Can conclude that search key is not in vector
 2008 Pearson Education, Inc. All rights reserved.
1
// Fig 19.2: BinarySearch.h
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;
19
Outline
BinarySearch.h
6
7
class BinarySearch
8
{
9
public:
(1 of 1)
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
 2008 Pearson Education,
Inc. All rights reserved.
1
2
3
// Fig 19.3: BinarySearch.cpp
// BinarySearch class member-function definition.
#include <iostream>
4
5
using std::cout;
using std::endl;
20
Outline
6
BinarySearch.cpp
7 #include <cstdlib> // prototypes for functions srand and rand
8 using std::rand;
9 using std::srand;
10
(1 of 3)
11 #include <ctime> // prototype for function time
12 using std::time;
13
14
15
16
17
18
#include <algorithm> // prototype for sort function
#include "BinarySearch.h" // class BinarySearch definition
// constructor initializes vector with random ints and sorts the vector
BinarySearch::BinarySearch( int vectorSize )
19 {
20
21
22
23
24
25
26
27
size = ( vectorSize > 0 ? vectorSize : 10 ); // validate vectorSize
srand( time( 0 ) ); // seed using current time
// fill vector with random ints in range 10-99
for ( int i = 0; i < size; i++ )
Initialize the vector with
random ints from 10-99
data.push_back( 10 + rand() % 90 ); // 10-99
std::sort( data.begin(), data.end() ); // sort the data
28 } // end BinarySearch constructor
Sort the elements in vector
data in ascending order
 2008 Pearson Education,
Inc. All rights reserved.
29
21
30 // perform a binary search on the data
31 int BinarySearch::binarySearch( int searchElement ) const
32 {
33
34
int low = 0; // low end of the search area
int high = size - 1; // high end of the search area
35
36
37
int middle = ( low + high + 1 ) / 2; // middle element
int location = -1; // return value; -1 if not found
38
do // loop to search for element
39
{
40
// print remaining elements of vector to be searched
41
42
displaySubElements( low, high );
43
44
// output spaces for alignment
for ( int i = 0; I < middle; i++ )
45
cout << "
Outline
Calculate the low end index, high
end index and middle index of the
BinarySearch.cpp
portion of the vector being searched
(2 of 3)
Initialize the location of the found
element to -1, indicating that the
search key has not (yet) been found
";
46
Test if the middle element is
equal to searchElement
47
48
49
cout << " * " << endl; // indicate current middle
50
if ( searchElement == data[ middle ] )
51
52
53
location = middle; // location is the current middle
else if ( searchElement < data[ middle ] ) // middle is too high
high = middle - 1; // eliminate the higher half
54
55
56
else // middle element is too low
low = middle + 1; // eliminate the lower half
57
58
// if the element is found at the middle
Eliminate half of the remaining
values in the vector
middle = ( low + high + 1 ) / 2; // recalculate the middle
} while ( ( low <= high ) && ( location == -1 ) );
Loop until the subvector is of zero size or the search key is
 2008 Pearson Education,
located Inc. All rights reserved.
59
60
22
return location; // return location of search key
61 } // end function binarySearch
Outline
62
63 // display values in vector
64 void BinarySearch::displayElements() const
BinarySearch.cpp
65 {
66
displaySubElements( 0, size - 1 );
(3 of 3)
67 } // end function displayElements
68
69 // display certain values in vector
70 void BinarySearch::displaySubElements( int low, int high ) const
71 {
72
73
for ( int i = 0; i < low; i++ ) // output spaces for alignment
cout << "
";
74
75
76
for ( int i = low; i <= high; i++ ) // output elements left in vector
cout << data[ i ] << " ";
77
78
cout << endl;
79 } // end function displaySubElements
 2008 Pearson Education,
Inc. All rights reserved.
1
// Fig 19.4: Fig19_04.cpp
2
// BinarySearch test program.
3
4
#include <iostream>
using std::cin;
5
using std::cout;
6 using std::endl;
7
8 #include "BinarySearch.h" // class BinarySearch definition
9
10 int main()
11 {
12
23
Outline
Fig20_04.cpp
(1 of 3)
int searchInt; // search key
13
14
int position; // location of search key in vector
15
16
17
18
19
20
21
22
23
// create vector and output it
BinarySearch searchVector ( 15 );
searchVector.displayElements();
24
25
26
27
28
29
// repeatedly input an integer; -1 terminates the program
while ( searchInt != -1 )
{
// use binary search to try to find integer
position = searchVector.binarySearch( searchInt );
// get input from user
cout << "\nPlease enter an integer value (-1 to quit): ";
cin >> searchInt; // read an int from user
cout << endl;
Perform a binary
search on the data
 2008 Pearson Education,
Inc. All rights reserved.
30
// return value of -1 indicates integer was not found
31
if ( position == -1 )
32
33
34
cout << "The integer " << searchInt << " was not found.\n";
24
Outline
else
cout << "The integer " << searchInt
<< " was found in position " << position << ".\n";
35
Fig19_04.cpp
36
(2 of 3)
37
// get input from user
38
cout << "\n\nPlease enter an integer value (-1 to quit): ";
39
cin >> searchInt; // read an int from user
40
cout << endl;
41
} // end while
42
43
return 0;
44 } // end main
26 31 33 38 47 49 49 67 73 74 82 89 90 91 95
Please enter an integer value (-1 to quit): 38
26 31 33 38 47 49 49 67 73 74 82 89 90 91 95
*
26 31 33 38 47 49 49
*
26 31 33 38 47 49 49 67 73 74 82 89 90 91 95
(Continued at top of next slide... )
 2008 Pearson Education,
Inc. All rights reserved.
(...continued from bottom of previous slide )
25
Please enter an integer value (-1 to quit): 38
Outline
26 31 33 38 47 49 49 67 73 74 82 89 90 91 95
*
26 31 33 38 47 49 49
*
The integer 38 was found in position 3.
Fig19_04.cpp
Please enter an integer value (-1 to quit): 91
(3 of 3)
26 31 33 38 47 49 49 67 73 74 82 89 90 91 95
*
73 74 82 89 90 91 95
*
90 91 95
*
The integer 91 was found in position 13.
Please enter an integer value (-1 to quit): 25
26 31 33 38 47 49 49 67 73 74 82 89 90 91 95
*
26 31 33 38 47 49 49
*
26 31 33
*
26
*
The integer 25 was not found.
Please enter an integer value (-1 to quit): -1
 2008 Pearson Education,
Inc. All rights reserved.
26
19.2.2 Binary Search (Cont.)
• Efficiency of binary search
– Logarithmic runtime
• Number of operations performed by algorithm grows
logarithmically as number of items increases
• Represented in Big O notation as O(log n)
– Pronounced “on the order of log n” or “order log n”
• Example
– Binary searching a sorted vector of 1023 elements takes
at most 10 comparisons ( 10 = log 2 ( 1023 + 1 ) )
• Repeatedly dividing 1023 by 2 and rounding down
results in 0 after 10 iterations
 2008 Pearson Education, Inc. All rights reserved.
27
19.3 Sorting Algorithms
• Sorting algorithms
– Placing data into some particular order
• Such as ascending or descending
– One of the most important computing applications
– End result, a sorted vector, will be the same no matter
which algorithm is used
• Choice of algorithm affects only runtime and memory use
 2008 Pearson Education, Inc. All rights reserved.
28
19.3.1 Efficiency of Selection Sort
• Selection sort
– At ith iteration
• Swaps the ith smallest element with element i
– After ith iteration
• Smallest i elements are sorted in increasing order in first i
positions
– Requires a total of (n2 – n)/2 comparisons
• Iterates n - 1 times
– In ith iteration, locating ith smallest element requires n –
i comparisons
• Has Big O of O(n2)
 2008 Pearson Education, Inc. All rights reserved.
29
19.3.2 Efficiency of Insertion Sort
• Insertion sort
– At ith iteration
• Insert (i + 1)th element into correct position with respect to
first i elements
– After ith iteration
• First i elements are sorted
– Requires a worst-case of n2 inner-loop iterations
• Outer loop iterates n - 1 times
– Inner loop requires n – 1iterations in worst case
• For determining Big O, nested statements mean multiply the
number of iterations
• Has Big O of O(n2)
 2008 Pearson Education, Inc. All rights reserved.
19.3.3 Merge Sort (A Recursive
Implementation)
30
• Merge sort
– Sorts vector by
• Splitting it into two equal-size subvectors
– If vector size is odd, one subvector will be one element
larger than the other
• Sorting each subvector
• Merging them into one larger, sorted vector
– Repeatedly compare smallest elements in the two
subvectors
– The smaller element is removed and placed into the
larger, combined vector
 2008 Pearson Education, Inc. All rights reserved.
19.3.3 Merge Sort (A Recursive
Implementation) (Cont.)
31
• Merge sort (Cont.)
– Our recursive implementation
• Base case
– A vector with one element is already sorted
• Recursion step
– Split the vector (of ≥ 2 elements) into two equal halves
• If vector size is odd, one subvector will be one
element larger than the other
– Recursively sort each subvector
– Merge them into one larger, sorted vector
 2008 Pearson Education, Inc. All rights reserved.
19.3.3 Merge Sort (A Recursive
Implementation) (Cont.)
32
• Merge sort (Cont.)
– Sample merging step
• Smaller, sorted vectors
– A: 4 10 34 56
– B: 5 30 51 52
77
93
• Compare smallest element in A to smallest element in B
– 4 (A) is less than 5 (B)
• 4 becomes first element in merged vector
– 5 (B) is less than 10 (A)
• 5 becomes second element in merged vector
– 10 (A) is less than 30 (B)
• 10 becomes third element in merged vector
– Etc.
 2008 Pearson Education, Inc. All rights reserved.
1
// Fig 19.5: MergeSort.h
2
// Class that creates a vector filled with random integers.
3
// Provides a function to sort the vector with merge sort.
4
#include <vector>
5
using std::vector;
33
Outline
MergeSort.h
6
7
// MergeSort class definition
8
class MergeSort
9
{
(1 of 1)
10 public:
11
MergeSort( int ); // constructor initializes vector
12
void sort(); // sort vector using merge sort
13
void displayElements() const; // display vector elements
14 private:
15
int size; // vector size
16
vector< int > data; // vector of ints
17
void sortSubVector( int, int ); // sort subvector
18
void merge( int, int, int, int ); // merge two sorted vectors
19
void displaySubVector( int, int ) const; // display subvector
20 }; // end class SelectionSort
 2008 Pearson Education,
Inc. All rights reserved.
1
// Fig 19.6: MergeSort.cpp
2
3
// Class MergeSort member-function definition.
#include <iostream>
4
using std::cout;
5
6
using std::endl;
7
8
#include <vector>
using std::vector;
9
10 #include <cstdlib> // prototypes for functions srand and rand
34
Outline
MergeSort.cpp
(1 of 5)
11 using std::rand;
12 using std::srand;
13
14 #include <ctime> // prototype for function time
15 using std::time;
16
17 #include "MergeSort.h" // class MergeSort definition
18
19 // constructor fill vector with random integers
20 MergeSort::MergeSort( int vectorSize )
21 {
22
23
size = ( vectorSize > 0 ? vectorSize : 10 ); // validate vectorSize
srand( time( 0 ) ); // seed random number generator using current time
24
25
26
// fill vector with random ints in range 10-99
for ( int i = 0; i < size; i++ )
27
data.push_back( 10 + rand() % 90 );
28 } // end MergeSort constructor
 2008 Pearson Education,
Inc. All rights reserved.
29
30 // split vector, sort subvectors and merge subvectors into sorted vector
31 void MergeSort::sort()
32 {
33
35
Outline
sortSubVector( 0, size - 1 ); // recursively sort entire vector
34 } // end function sort
MergeSort.cpp
Call function sortSubVector
with 0 and size – 1 as the
(2 of 5)
beginning and ending indices
35
36 // recursive function to sort subvectors
37 void MergeSort::sortSubVector( int low, int high )
38 {
39
40
// test base case; size of vector equals 1
if ( ( high - low ) >= 1 ) // if not base case
41
{
Test the base case
int middle1 = ( low + high ) / 2; // calculate middle of vector
int middle2 = middle1 + 1; // calculate next element over
42
43
44
45
46
// output split step
cout << "split:
";
47
48
displaySubVector( low, high );
cout << endl << "
";
49
50
51
displaySubVector( low, middle1 );
cout << endl << "
";
displaySubVector( middle2, high );
52
cout << endl << endl;
53
54
// split vector in half; sort each half (recursive calls)
55
56
sortSubVector( low, middle1 ); // first half of vector
sortSubVector( middle2, high ); // second half of vector
Split the vector in two
Recursively call function
sortSubVector on
the two subvectors
57
 2008 Pearson Education,
Inc. All rights reserved.
58
// merge two sorted vectors after split calls return
59
merge( low, middle1, middle2, high );
36
60
} // end if
Combine the two sorted vectors
61 } // end function sortSubVector
into one larger, sorted vector
62
63 // merge two sorted subvectors into one sorted subvector
64 void MergeSort::merge( int left, int middle1, int middle2, int right )
65 {
66
int leftIndex = left; // index into left subvector
67
int rightIndex = middle2; // index into right subvector
68
69
70
71
72
int combinedIndex = left; // index into temporary working vector
vector< int > combined( size ); // working vector
73
74
75
76
77
78
displaySubVector( left, middle1 );
cout << endl << "
";
displaySubVector( middle2, right );
cout << endl;
79
80
while ( leftIndex <= middle1 && rightIndex <= right )
{
81
82
83
84
85
86
87
Outline
MergeSort.cpp
(3 of 5)
// output two subvectors before merging
cout << "merge:
";
// merge vectors until reaching end of either
// place smaller of two current elements into result
// and move to next space in vector
if ( data[ leftIndex ] <= data[ rightIndex ] )
combined[ combinedIndex++ ] = data[ leftIndex++ ];
else
combined[ combinedIndex++ ] = data[ rightIndex++ ];
} // end while
Loop until the end of either
subvector is reached
Test which element at
the beginning of the
vectors is smaller
Place the smaller element
in the combined vector
 2008 Pearson Education,
Inc. All rights reserved.
88
89
if ( leftIndex == middle2 ) // if at end of left vector
90
91
{
92
93
combined[ combinedIndex++ ] = data[ rightIndex++ ];
} // end if
94
95
96
else // at end of right vector
{
while ( leftIndex <= middle1 ) // copy in rest of left vector
97
while ( rightIndex <= right ) // copy in rest of right vector
combined[ combinedIndex++ ] = data[ leftIndex++ ];
98
99
100
} // end else
101
102
103
104
for ( int i = left; i <= right; i++ )
data[ i ] = combined[ i ];
37
Outline
Fill the combined vector with
the remaining elements of
the rightMergeSort.cpp
vector or…
(4 of 5)
…else fill the combined
vector with the remaining
elements of the left vector
// copy values back into original vector
// output merged vector
Copy the combined vector
into the original vector
105
cout << "
";
106
displaySubVector( left, right );
107
cout << endl << endl;
108 } // end function merge
109
110 // display elements in vector
111 void MergeSort::displayElements() const
112 {
113
displaySubVector( 0, size - 1 );
114 } // end function displayElements
 2008 Pearson Education,
Inc. All rights reserved.
115
38
116 // display certain values in vector
117 void MergeSort::displaySubVector( int low, int high ) const
Outline
118 {
119
// output spaces for alignment
120
for ( int i = 0; i < low; i++ )
121
cout << "
";
(5 of 5)
122
123
// output elements left in vector
124
for ( int i = low; i <= high; i++ )
125
MergeSort.cpp
cout << " " << data[ i ];
126 } // end function displaySubVector
 2008 Pearson Education,
Inc. All rights reserved.
1
// Fig 19.7: Fig19_07.cpp
2
// MergeSort test program.
3
#include <iostream>
4
using std::cout;
5
using std::endl;
#include "MergeSort.h" // class MergeSort definition
(1 of 3)
8
9
Outline
Fig19_07.cpp
6
7
39
int main()
10 {
11
// create object to perform merge sort
12
MergeSort sortVector( 10 );
13
14
cout << "Unsorted vector:" << endl;
15
sortVector.displayElements(); // print unsorted vector
16
cout << endl << endl;
17
18
sortVector.sort(); // sort vector
19
20
cout << "Sorted vector:" << endl;
21
sortVector.displayElements(); // print sorted vector
22
cout << endl;
23
return 0;
24 } // end main
 2008 Pearson Education,
Inc. All rights reserved.
40
Unsorted vector:
30 47 22 67 79 18 60 78 26 54
split:
30 47 22 67 79 18 60 78 26 54
30 47 22 67 79
18 60 78 26 54
split:
30 47 22 67 79
30 47 22
67 79
split:
30 47 22
30 47
22
split:
30 47
30
47
merge:
30
Outline
Fig19_07.cpp
(2 of 3)
47
30 47
merge:
30 47
22
22 30 47
split:
67 79
67
79
merge:
67
79
67 79
merge:
22 30 47
67 79
22 30 47 67 79
(continued at top of next slide... )
 2008 Pearson Education,
Inc. All rights reserved.
41
(...continued from bottom of previous slide)
split:
18 60 78 26 54
18 60 78
26 54
split:
18 60 78
18 60
78
split:
18 60
18
60
merge:
18
Outline
Fig19_07.cpp
(3 of 3)
60
18 60
merge:
18 60
78
18 60 78
split:
26 54
26
54
merge:
26
54
26 54
merge:
18 60 78
26 54
18 26 54 60 78
merge:
22 30 47 67 79
18 26 54 60 78
18 22 26 30 47 54 60 67 78 79
Sorted vector:
18 22 26 30 47 54 60 67 78 79
 2008 Pearson Education,
Inc. All rights reserved.
19.3.3 Merge Sort (A Recursive
Implementation) (Cont.)
42
• Efficiency of merge sort
– n log n runtime
• Halving vectors means log 2 n levels to reach base case
– Doubling size of vector requires one more level
– Quadrupling size of vector requires two more levels
• O(n) comparisons are required at each level
– Calling sortSubVector with a size-n vector results in
• Two sortSubVector calls with size-n/2 subvectors
• A merge operation with n – 1 (order n) comparisons
– So, always order n total comparisons at each level
• Represented in Big O notation as O(n log n)
– Pronounced “on the order of n log n” or “order n log n”
 2008 Pearson Education, Inc. All rights reserved.
43
Algorithm
Location
Searching Algorithms
Linear search
Section 7.7
Binary search
Section 20.2.2
Recursive linear
Exercise 20.8
search
Recursive binary Exercise 20.9
search
Sorting Algorithms
Insertion sort
Section 7.8
Selection sort
Section 8.6
Merge sort
Section 20.3.3
Bubble sort
Exercises 16.3
and 16.4
Quick sort
Exercise 20.10
Big O
O(n)
O(log n)
O(n)
O(log n)
O(n2)
O(n2)
O(n log n)
O(n2)
Worst case: O(n2)
Average case: O(n log n)
Fig. 19.8 | Searching and sorting algorithms with Big O values.
 2008 Pearson Education, Inc. All rights reserved.
44
n
Approximate
decimal value
O(log n)
O(n)
O(n log n)
O(n2)
210
1000
10
210
10 ·210
220
220
1,000,000
20
220
20 ·220
240
230
1,000,000,000
30
230
30 ·230
260
Fig. 19.9 | Approximate number of comparisons for common Big O notations.
 2008 Pearson Education, Inc. All rights reserved.