Unformatted Document Excerpt
Coursehero >>
Utah >>
BYU >>
CS 142
Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
and
SearChing
Chapter Chapter
12
Sorting goalS
to compare the selection sort and
merge sort algorithms
to study the linear search and binary
search algorithms
to appreciate that algorithms for the same task can differ widely in performance
to understand the big-oh notation
to be able to estimate and compare the performance
of algorithms
to write code to measure the running time of a program
Chapter ContentS
12.1 SelectionSort 2
12.2 ProfilingtheSelectionSort
Algorithm 5
12.3 AnAlyzingthePerformAnceof
theSelectionSortAlgorithm 6
12.4 mergeSort 8
12.6 SeArching 15
Programming Tip 12.1: library Functions for
Sorting and Binary Search 18
Special Topic 12.2: defining an ordering for
Sorting objects 18
Random Fact 12.1: Cataloging Your necktie
Collection 19
12.5 AnAlyzingthemergeSort
Algorithm 11
Special Topic 12.1: the Quicksort algorithm 13
1
2 Chapter 12 Sorting and Searchingone of the most common tasks in data processing is sorting.
For example, an array of employees often needs to be
displayed in alphabetical order or sorted by salary. in this
chapter, you will learn several sorting methods and
techniques for comparing their performance. these techniques are useful not just for sorting algorithms, but also
for analyzing other algorithms.
once an array of elements is sorted, one can rapidly locate
individual elements. You will study the binary search
algorithm that carries out this fast lookup.
12.1 Selection Sort
the selection sort
algorithm sorts an
array by repeatedly
finding the smallest
element of the
unsorted tail region
and moving it to
the front.
A sorting algorithm rearranges the elements of an array so that they are stored in
sorted order. In this section, we show you the first of several sorting algorithms,
called selection sort. Consider the following array a:
11 9 17 5 12
An obvious first step is to find the smallest element. In this case the smallest element
is 5, stored in a[3]. You should move the 5 to the beginning of the array. Of course,
there is already an element stored in a[0], namely 11. Therefore you cannot simply
move a[3] into a[0] without moving the 11 somewhere else. You dont yet know
where the 11 should end up, but you know for certain that it should not be in a[0].
Simply get it out of the way by swapping it with a[3]:
5
9 17 11 12
Now the first element is in the correct place. In the foregoing figure, the darker color
indicates the portion of the array that is already sorted.
Next take the minimum of the remaining entries a[1]...a[4]. That minimum value,
9, is already in the correct place. You dont need to do anything in this case, simply
extend the sorted area by one to the right:
5
9 17 11 12
Repeat the process. The minimum value of the unsorted region is 11, which needs to
be swapped with the first value of the unsorted region, 17:
5
9 11 17 12
Now the unsorted region is only two elements long; keep to the same successful
strategy. The minimum element is 12. Swap it with the first value, 17:
5
9 11 12 17
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.1
Selection Sort
3
That leaves you with an unprocessed region of length 1, but of course a region of
length 1 is always sorted. You are done.
If speed was not an issue for us, we could stop the discussion of sorting right here.
However, the selection sort algorithm shows disappointing performance when run
on large data sets, and it is worthwhile to study better sorting algorithms.
Here is the implementation of the selection sort algorithm:
ch12/selsort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
/**
Gets the position of the smallest element in an array range.
@param a the array
@param from the beginning of the range
@param to the end of the range
@return the position of the smallest element in
the range a[from]...a[to]
*/
int min_position(int a[], int from, int to)
{
int min_pos = from;
for (int i = from + 1; i <= to; i++)
{
if (a[i] < a[min_pos]) { min_pos = i; }
}
return min_pos;
}
/**
Swaps two integers.
@param x the first integer to swap
@param y the second integer to swap
*/
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
/**
Sorts an array using the selection sort algorithm.
@param a the array to sort
@param size the number of elements in a
*/
void selection_sort(int a[], int size)
{
int next; // The next position to be set to the minimum
for (next = 0; next < size - 1; next++)
{
// Find the position of the minimum
int min_pos = min_position(a, next, size - 1);
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
4 Chapter 12 Sorting and Searching
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
if (min_pos != next)
{
swap(a[min_pos], a[next]);
}
}
}
/**
Prints all elements in an array.
@param a the array to print
@param size the number of elements in a
*/
void print(int a[], int size)
{
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
srand(time(0));
const int SIZE = 20;
int values[SIZE];
for (int i = 0; i < SIZE; i++)
{
values[i] = rand() % 100;
}
print(values, SIZE);
selection_sort(values, SIZE);
print(values, SIZE);
return 0;
}
Programrun
60 47 70 39 6 12 96 93 83 53 36 29 50 97 94 95 38 17 8 26
6 8 12 17 26 29 36 38 39 47 50 53 60 70 83 93 94 95 96 97
Selfcheck
1. What steps does the selection sort algorithm go through to sort the array
6 5 4 3 2 1?
2. How can you change the selection sort algorithm so that it sorts the elements in
descending order (that is, with the largest element at the beginning of the array)?
3. Suppose we modified the selection sort algorithm to start at the end of the array,
working toward the beginning. In each step, the current position is swapped
with the minimum. What is the result of this modification?
4. Why do we need the temp variable in the swap function? What would happen if
you simply assigned a[i] to a[j] and a[j] to a[i]?
Practiceit Now you can try these exercises at the end of the chapter: R12.1, P12.1, P12.2.
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.2
profiling the Selection Sort algorithm
5
12.2 profiling the Selection Sort algorithm
To measure the performance of a program, one could simply run it and measure how
long it takes by using a stopwatch. However, most of our programs run very quickly,
and it is not easy to time them accurately in this way. Furthermore, when a program
does take a noticeable time to run, a certain amount of that time may simply be used
for loading the program from disk into memory (for which it should not be penalized) or for screen output (whose speed depends on the computer model, even for
computers with identical CPUs). Instead we use the time function. The call
int now = time(0);
sets now to the number of seconds that have elapsed since January 1, 1970. We dont
care about this value, but if we have two time measurements, then their difference
yields the elapsed time:
int before = time(0);
selection_sort(values, size);
int after = time(0);
cout << "Elapsed time = " << after - before
<< " seconds" << endl;
to measure the
running time of a
function, get the
current time
immediately before
and after the
function call.
By measuring the time just before and after the sorting, you dont count the time it
takes to initialize the array or the time during which the program waits for the user to
provide inputs. See ch12/seltime.cpp for the complete program. The table in Figure 1
shows the results of some sample runs.
These measurements were obtained on a Pentium processor with a clock speed of
1.67 GHz running Linux. On another computer, the actual numbers will differ, but
the relationship between the numbers will be the same. Figure 1 shows a plot of the
measurements.
As you can see, doubling the size of the data set more than doubles the time needed
to sort it.
25
Seconds
20
10,000
1
15
20,000
3
30,000
6
40,000
Time (seconds)
n
11
50,000
17
60,000
25
10
5
10
20
30
40
50
60
n (thousands)
figure1 time taken by Selection Sort
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
6 Chapter 12 Sorting and Searching
5. Approximately how many seconds would it take to sort a data set of 80,000
Selfcheck
values?
6. Look at the graph in Figure 1. What mathematical shape does it resemble?
Practiceit Now you can try these exercises at the end of the chapter: P12.3.
12.3 analyzing the performance of the
Selection Sort algorithm
Let us count the number of operations that a program must carry out to sort an array
using the selection sort algorithm. Actually, we dont know how many machine
operations are generated for each C++ instruction or which of those instructions are
more time-consuming than others, but we can make a simplification. Simply count
how often an element is visited. Each visit requires about the same amount of work
by other operations, such as incrementing subscripts and comparing values.
Let n be the size of the array. First, you must find the smallest of n numbers. To
achieve this, you must visit n elements. Then swap the elements, which takes two visits. (You may argue that there is a certain probability that you dont need to swap the
values. That is true, and one can refine the computation to reflect that observation. As
we will soon see, doing so would not affect the overall conclusion.) In the next step,
you need to visit only n 1 elements to find the minimum and then visit two of them
to swap them. In the following step, n 2 elements are visited to find the minimum.
The last run visits two elements to find the minimum and requires two visits to swap
the elements. Therefore, the total number of visits is
n + 2 + (n 1) + 2 +
+ 2 + 2 = n + (n 1) +
= 2+
=
+ 2 + (n 1) 2
+ (n 1) + n + (n 1) 2
n (n + 1)
1 + (n 1) 2
2
because
1+ 2 +
+ (n 1) + n =
n (n + 1)
2
After multiplying out and collecting terms of n, you find that the number of visits is
1 n2
2
+ 5n 3
2
This is a quadratic equation in n. That explains why the graph of Figure 1 looks
approximately like a parabola.
Now simplify the analysis further. When you plug in a large value for n (for example, 1,000 or 2,000), then 1 n 2 is 500,000 or 2,000,000. The lower term, 5 n 3 , doesnt
2
2
contribute much at all; it is just 2,497 or 4,997, a drop in the bucket compared to the
hundreds of thousands or even millions of comparisons specified by the 1 n 2 term.
2
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.3 analyzing the performance of the Selection Sort algorithm
7
Just ignore these lower-level terms. Next, ignore the constant factor 1 . You need
2
not be interested in the actual count of visits for a single n. You need to compare the
2
ratios of counts for different values of n . For example, you can say that sorting an
array of 2,000 numbers requires four times as many visits as sorting an array of 1,000
numbers:
(
(
1
2
1
2
2000 2
1000 2
)=4
)
The factor 1 cancels out in comparisons of this kind. We will simply say, The num2
ber of visits is of order n2. That way, we can easily see that the number of comparisons increases fourfold when the size of the array doubles: (2n)2 = 4n2.
To indicate that the number of visits is of order n2, computer scientists often use
big-Oh notation: The number of visits is O(n2). This is a convenient shorthand.
To turn an exact expression such as
1 n2
2
Computer scientists
use big-oh notation
to describe how fast
a function grows.
Selection sort is an
O(n2) algorithm.
doubling the
data set means a
fourfold increase in
processing time.
Selfcheck
+ 5n3
2
into big-Oh notation, simply locate the fastest-growing term, n2, and ignore its constant coefficient, 1 in this case, no matter how large or small it may be.
2
In general, the expression f(n) = O(g(n)) means that f grows no faster than g, or,
more formally, that for all n larger than some threshold, the ratio f (n) g(n) is less
than a constant value C. The function g is usually chosen to be very simple, such as n 2
in our example.
You observed before that the actual number of machine operations, and the actual
amount of time that the computer spends on them, is approximately proportional
to the number of element visits. Maybe there are about 10 machine operations
(increments, comparisons, memory loads, and stores) for every element visit. The
number of machine operations is then approximately 10 1 n 2. As before, we arent
2
interested in the coefficient, so we can say that the number of machine operations,
and hence the time spent on the sorting, is of the order of n2 or O(n2).
The sad fact remains that doubling the size of the array causes a fourfold increase
in the time required for sorting it. When the size of an array increases by a factor
of 100, the sorting time increases by a factor of 10,000. To sort an array of a million
entries (for example, to create a telephone directory), takes 10,000 times as long as
sorting 10,000 entries. If 10,000 entries can be sorted in about a second (as in our
example), then sorting one million entries requires almost three hours. You will see
in the next section how one can dramatically improve the performance of the sorting
process by choosing a more sophisticated algorithm.
7.
8.
9.
10.
If you increase the size of a data set tenfold, how much longer does it take to sort
it with the selection sort algorithm?
How large does n need to be so that 1 n 2 is bigger than 5 n 3 ?
2
2
Section 6.2.7 has two algorithms for removing an element for an array of length
n. How many array visits does each algorithm require on average?
Describe the number of array visits in Self Check 9 using the big-Oh notation.
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
8 Chapter 12 Sorting and Searching
11.
Consider this algorithm for sorting an array. Set k to the length of the array. Find
the maximum of the first k elements. Remove it, using the second algorithm of
Section 6.2.7. Decrement k and stop when it is 1. What is the algorithms running
time in big-Oh notation?
Practiceit Now you can try these exercises at the end of the chapter: R12.3, R12.6, R12.9.
12.4 Merge Sort
In this section, you will learn about the merge sort algorithm, a much more efficient
algorithm than selection sort. The basic idea behind merge sort is very simple. Suppose you have an array of 10 integers. Engage in a bit of wishful thinking and hope
that the first half of the array is already perfectly sorted, and the second half is too,
like this:
5
9 10 12 17
1
8 11 20 32
Now it is an easy matter to merge the two sorted arrays into a sorted array, simply by
taking a new element from either the first or the second subarray and choosing the
smaller of the elements each time:
5
1
8 11 20 32
1
9 10 12 17
1
8 11 20 32
1
5
5
9 10 12 17
1
8 11 20 32
1
5
8
5
9 10 12 17
1
8 11 20 32
1
5
8
9
5
9 10 12 17
1
8 11 20 32
1
5
8
9 10
5
9 10 12 17
1
8 11 20 32
1
5
8
9 10 11
5
9 10 12 17
1
8 11 20 32
1
5
8
9 10 11 12
5
9 10 12 17
1
8 11 20 32
1
5
8
9 10 11 12 17
5
9 10 12 17
1
8 11 20 32
1
5
8
9 10 11 12 17 20
5
the merge sort
algorithm sorts an
array by cutting the
array in half,
recursively sorting
each half, then
merging the sorted
halves.
9 10 12 17
5
9 10 12 17
1
8 11 20 32
1
5
8
9 10 11 12 17 20 32
In fact, you probably performed this merging before when you and a friend had to
sort a pile of papers. You and the friend split the pile in half, each of you sorted your
half, and then you merged the results together.
This is all well and good, but it doesnt seem to solve the problem for the computer.
It still has to sort the first and second halves, because it cant very well ask a few buddies to pitch in. As it turns out, though, if the computer keeps dividing the array into
smaller and smaller subarrays, sorting each half and merging them back together, it
carries out dramatically fewer steps than the selection sort requires.
Let us write a program that implements this idea. Because we will call the merge_
sort function multiple times to sort portions of the array, we will supply the range of
elements that we would like to have sorted:
void merge_sort(int a[], int from, int to)
{
if (from == to) { return; }
int mid = (from + to) / 2;
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.4
Merge Sort
9
// Sort the first and the second half
merge_sort(a, from, mid);
merge_sort(a, mid + 1, to);
merge(a, from, mid, to);
}
The merge function is somewhat long but quite straightforwardsee the following
code listing for details.
ch12/mergesort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
/**
Merges two adjacent ranges in an array.
@param a the array with the elements to merge
@param from the start of the first range
@param mid the end of the first range
@param to the end of the second range
*/
void merge(int a[], int from, int mid, int to)
{
int n = to - from + 1; // Size of the range to be merged
// Merge both halves into a temporary array b
// We allocate the array dynamically because its size is only
// known at run timesee Section 7.4
int* b = new int[n];
int i1 = from;
// Next element to consider in the first half
int i2 = mid + 1;
// Next element to consider in the second half
int j = 0; // Next open position in b
// As long as neither i1 nor i2 is past the end, move the smaller
// element into b
while (i1 <=
{
if (a[i1]
{
b[j] =
i1++;
}
else
{
b[j] =
i2++;
}
j++;
}
mid && i2 <= to)
< a[i2])
a[i1];
a[i2];
// Note that only one of the two while loops below is executed
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
10
Chapter 12 Sorting and Searching
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copy any remaining entries of the first half
while (i1 <= mid)
{
b[j] = a[i1];
i1++;
j++;
}
// Copy any remaining entries of the second half
while (i2 <= to)
{
b[j] = a[i2];
i2++;
j++;
}
// Copy back from the temporary array
for (j = 0; j < n; j++)
{
a[from + j] = b[j];
}
// The temporary array is no longer needed
delete[] b;
}
/**
Sorts the elements in a range of an array.
@param a the array with the elements to sort
@param from start of the range to sort
@param to end of the range to sort
*/
void merge_sort(int a[], int from, int to)
{
if (from == to) { return; }
int mid = (from + to) / 2;
// Sort the first half and the second half
merge_sort(a, from, mid);
merge_sort(a, mid + 1, to);
merge(a, from, mid, to);
}
/**
Prints all elements in an array.
@param a the array to print
@param size the number of elements in a
*/
void print(int a[], int size)
{
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
srand(time(0));
const int SIZE = 20;
int values[SIZE];
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.5
108
109
110
111
112
113
114
115
116
Selfcheck
12.
13.
14.
analyzing the Merge Sort algorithm
11
for (int i = 0; i < SIZE; i++)
{
values[i] = rand() % 100;
}
print(values, SIZE);
merge_sort(values, 0, SIZE - 1);
print(values, SIZE);
return 0;
}
Why does only one of the two while loops at the end of the merge function do any
work?
Manually run the merge sort algorithm on the array 8 7 6 5 4 3 2 1.
The merge sort algorithm processes an array by recursively processing two
halves. Describe a similar recursive algorithm for computing the sum of all
elements in an array.
Practiceit Now you can try these exercises at the end of the chapter: R12.15, P12.4, P12.9.
12.5 analyzing the Merge Sort algorithm
The merge sort algorithm looks much more complicated than the selection sort algorithm, and it appears that it may well take much longer to carry out these repeated
subdivisions. However, the timing results for merge sort look much better than those
for selection sort (see ch12/mergetime.cpp and the table in Figure 2). Sorting an array
with 60,000 elements takes less than one second on our test machine, whereas the
selection sort takes 25 seconds.
In order to get precise timing results, it is best to run the algorithm multiple times,
and then divide the total time by the number of runs. Figure 2 shows typical results
and a graph plotting the relationship. Note that the graph does not have a parabolic
shape. Instead, it appears as if the running time grows approximately linearly with
the size of the array.
n
Time (seconds)
10
Merge sort
5
10
20
30
40
50
60
0.012
1
0.025
3
30,000
15
Selection Sort
(seconds)
20,000
Selection sort
20
Merge Sort
(seconds)
10,000
25
0.038
6
40,000
0.052
11
50,000
0.066
17
60,000
0.081
25
n (thousands)
figure2 Merge Sort timing versus Selection Sort
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12
Chapter 12 Sorting and Searching
To understand why the merge sort algorithm is such a tremendous improvement,
let us estimate the number of array element visits. First, we tackle the merge process
that happens after the first and second halves have been sorted.
Each step in the merge process adds one more element to b. There are n elements in
b. That element may come from the first or second half of a, and in most cases the elements from the two halves must be compared to see which one to take. Count that as
3 visits per element (one for b and one each for the two halves of a), or 3n visits total.
Then you must copy back from b to a, yielding another 2n visits, for a total of 5n.
If you let T(n) denote the number of visits required to sort a range of n elements
through the merge sort process, then you obtain
n
n
T (n) = T + T + 5n
2
2
because sorting each half takes T (n 2) visits. Actually, if n is not even, then you have
one array of size (n 1) 2 and one of size (n + 1) 2 . Although it turns out that this
detail does not affect the outcome of the computation, you can assume for now that n
is a power of 2, say n = 2m. This way, all arrays can be evenly divided into two parts.
Unfortunately, the formula
n
T (n) = 2T + 5n
2
does not clearly tell you the relationship between n and T(n). To understand the relationship, evaluate T (n 2) , using the same formula:
n
n
n
T = 2T + 5
2
4
2
Therefore
n
T (n) = 2 2T + 5n + 5n
4
Do that again:
n
n
n
T = 2T + 5
4
8
4
hence
n
T (n) = 2 2 2T + 5n + 5n + 5n
8
This generalizes from 2, 4, 8, to arbitrary powers of 2:
n
T (n) = 2kT + 5nk
2k
Recall that you assume that n = 2m; hence, for k = m,
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.5
analyzing the Merge Sort algorithm
13
n
T (n) = 2m T
+ 5nm
2m
= nT (1) + 5nm
= n + 5n log 2 (n)
Because n = 2m, you have m = log2(n).
To establish the growth order, you drop the lower order term n and are left with 5n
log2(n). Drop the constant factor 5. It is also customary to drop the base of the logarithm because all logarithms are related by a constant factor. For example,
log 2 ( x) = log10 ( x) log10 ( 2) log10 ( x) 3.32193
Merge sort is an
O(n log(n)) algorithm.
the n log(n) function
grows much more
slowly than n2.
Hence we say that merge sort is an O(n log(n)) algorithm.
Is the O(n log(n)) merge sort algorithm better than an O(n2) selection sort algorithm? You bet it is. Recall that it took 1002 = 10,000 times as long to sort a million
records as it took to sort 10,000 records with the O(n2) algorithm. With the O(n
log(n)) algorithm, the ratio is
1, 000, 000 log (1, 000, 000 )
6
= 100 = 150
(10, 000)
4
10, 000 log
Suppose for the moment that merge sort takes the same time as selection sort to sort
an array of 10,000 integers, that is, 1 second on the test machine. (Actually, as you
have seen, it is much faster than that.) Then it would take about 150 seconds, or less
than three minutes, to sort 1,000,000 integers. Contrast that with selection sort,
which would take almost 3 hours for the same task. As you can see, even if it takes
you several hours to learn about a better algorithm, that can be time well spent.
In this chapter you have barely begun to scratch the surface of this interesting
topic. There are many sort algorithms, some with even better performance than the
merge sort algorithm, and the analysis of these algorithms can be quite challenging.
If you are a computer science major, you may revisit these important issues in later
computer science classes.
Selfcheck
15.
16.
Given the timing data for the merge sort algorithm in the table in Figure 2, how
long would it take to sort an array of 100,000 values?
If you double the size of an array, how much longer will the merge sort algorithm take to sort the new array?
Practiceit Now you can try these exercises at the end of the chapter: R12.7, R12.10, R12.11.
Special topic 12.1
theQuicksortAlgorithm
Quicksort is a commonly used algorithm that has the advantage over merge sort that no temporary arrays are required to sort and merge the partial results.
The quicksort algorithm, like merge sort, is based on the strategy of divide and conquer.
To sort a range a[from] . . . a[to] of the array a, first rearrange the elements in the range so
that no element in the range a[from] . . . a[p] is larger than any element in the range a[p +
1] . . . a[to]. This step is called partitioning the range.
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
14
Chapter 12 Sorting and Searching
For example, suppose we start with a range
5
3
2
6
4
1
3
7
Here is a partitioning of the range. Note that the partitions arent yet sorted.
3
3
2
1
4
6
5
7
Youll see later how to obtain such a partition. In the next step, sort each partition, by recursively applying the same algorithm on the two partitions. That sorts the entire range, because
the largest element in the first partition is at most as large as the smallest element in the second
partition.
1
2
3
3
4
5
6
7
Quicksort is implemented recursively as follows:
void sort(int a[], int from, int to)
{
if (from >= to) { return; }
int p = partition(a, from, to);
sort(a, from, p);
sort(a, + p 1, to);
}
Let us return to the problem of partitioning a range. Pick an element from the range and call
it the pivot. There are several variations of the quicksort algorithm. In the simplest one, well
pick the first element of the range, a[from], as the pivot.
Now form two regions a[from] . . . a[i], consisting of values at most as large as the pivot
and a[j] . . . a[to], consisting of values at least as large as the pivot. The region
a[i + 1] . . . a[j - 1] consists of values that havent been analyzed yet. (See Figure 3.) At the
beginning, both the left and right areas are empty; that is, i = from - 1 and j = to + 1.
pivot
[from]
pivot
Not yet analyzed
[i]
[j]
[to]
figure3 partitioning a range
Then keep incrementing i while a[i] < pivot and keep decrementing j while a[j]
ure 4 shows i and j when that process stops.
pivot
pivot
[from]
< pivot
[i]
> pivot. Fig-
pivot
> pivot
[j]
pivot
[to]
figure4 extending the partitions
Now swap the values in positions i and j, increasing both areas once more. Keep going while i
< j. Here is the code for the partition function:
int partition(int a[], int from, int to)
{
int pivot = a[from];
int i = from - 1;
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.6
Searching
15
int j = to + 1;
while (i < j)
{
i++; while (a[i] < pivot) { i++; }
j--; while (a[j] > pivot) { j--; }
if (i < j) { swap(a[i], a[j]); }
}
return j;
}
On average, the quicksort algorithm is an O(n log(n)) algorithm. Because it is simpler, it runs
faster than merge sort in most cases. There is just one unfortunate aspect to the quicksort algorithm. Its worst-case run-time behavior is O(n2). Moreover, if the pivot element is chosen as
the first element of the region, that worst-case behavior occurs when the input set is already
sorteda common situation in practice. By selecting the pivot element more cleverly, we can
make it extremely unlikely for the worst-case behavior to occur. Such tuned quicksort algorithms are commonly used because their performance is generally excellent. For example, the
C library contains a function qsort that implements the quicksort algorithm.
12.6 Searching
Searching for an element in an array is an extremely common task. As with sorting,
the right choice of algorithms can make a big difference.
12.6.1 linear Search
a linear search
examines all values
in an array until it
finds a match or
reaches the end.
Suppose you need to find the telephone number of your friend. If you have a telephone book, you can look up your friends name quickly, because the telephone book
is sorted alphabetically. However, now suppose you have a telephone number and
you must know to whom it belongs (without actually calling the number). You could
look through the telephone book, one number at a time, until you find the number.
This would obviously be a tremendous amount of work.
This thought experiment shows the difference between a search through an
unsorted data set and a search through a sorted data set.
If you want to find a number in an array of values in arbitrary order, you must look
through all elements until you have found a match or until you reach the end. This is
called a linear or sequential search.
Here is a function that performs a linear search through an array of integers a for a
given value (see ch12/lsearch.cpp). The function then returns the index of the match,
or 1 if the value does not occur in a.
int linear_search(int a[], int size, int value)
{
for (int i = 0; i < size; i++)
{
if (a[i] == value)
{
return i;
}
}
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
16
Chapter 12 Sorting and Searching
return -1;
}
a linear search
locates a value in
an array in
O(n) steps.
How long does a linear search take? If you assume that the value is present in the
array a, then the average search visits n 2 elements. If it is not present, then all n elements must be inspected to verify the absence. Either way, a linear search is an O(n)
algorithm.
12.6.2 Binary Search
Now consider searching for an item in an array that has been previously sorted. Of
course, you could still do a linear search, but it turns out you can do much better than
that.
Here is a typical example. The data set is:
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
14 43 76 100 115 290 400 511
and you want to see whether the value 123 is in the data set. The last point in the first
half of the data set, a[3], is 100. It is smaller than the value you are looking for; hence,
you should look in the second half of the data set for a match, that is, in the array
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
14 43 76 100 115 290 400 511
Now the last value of the first half of this array is 290; hence, the value must be located
in the array
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
14 43 76 100 115 290 400 511
The last value of the first half of this very short array is 115, which is smaller than the
value that you are searching, so you must look in the second half:
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
14 43 76 100 115 290 400 511
a binary search
locates a value in a
sorted array by
determining whether
the value occurs in
the first or second
half, then repeating
the search in one of
the halves.
It is trivial to see that you dont have a match, because 123 290. If you wanted to
insert 123 into the array, you would need to insert it just before a[5].
This search process is called a binary search, because the size of the search is cut in
half in each step. That cutting in half works only because you know that the array of
values is sorted.
The following function implements a binary search in a sorted array of integers
(see ch12/bsearch.cpp). It returns the position of the match if the search succeeds, or 1
if the value is not found in the array:
int binary_search(int a[], int from, int to, int value)
{
if (from > to)
{
return -1;
}
int mid = (from + to) / 2;
if (a[mid] == value)
{
return mid;
}
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
12.6
Searching
17
else if (a[mid] < value)
{
return binary_search(a, mid + 1, to, value);
}
else
{
return binary_search(a, from, mid - 1, value);
}
}
Now determine the number of element visits required to carry out a search. Use the
same technique as in the analysis of merge sort. Because you look at the middle element, which counts as one comparison, and then search either the left or the right
array, you have
n
T ( n) = T + 1
2
Using the same equation,
n
n
T = T +1
2
4
By plugging this result into the original equation, you get
n
T ( n) = T + 2
4
This generalizes to
n
T ( n) = T + k
2k
As in the analysis of merge sort, you make the simplifying assumption that n is a
power of 2, n = 2m, where m = log2(n). Then you obtain
T (n) = 1 + log 2 (n)
a binary search
locates a value in a
sorted array in
O(log(n)) steps.
Selfcheck
Therefore, binary search is an O(log(n)) algorithm.
This result makes intuitive sense. Suppose that n is 100. Then after each search, the
size of the search range is cut in half, to 50, 25, 12, 6, 3, and 1. After seven comparisons
we are done. This agrees with our formula, because log2(100) 6.64386, and indeed
the next larger power of 2 is 27 = 128.
Because a binary search is so much faster than a linear search, is it worthwhile to
sort an array first and then use a binary search? It depends. If you only search the
array once, then it is more efficient to pay for an O(n) linear search than for an O(n
log(n)) sort and O(log(n)) binary search. But if one makes a number of searches in the
same array, then sorting it is definitely worthwhile.
17.
18.
Suppose you need to look through 1,000,000 records to find a telephone number. How many records do you expect to search before finding the number?
What happens when you execute the binary search algorithm on an array that is
not sorted?
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
18
Chapter 12 Sorting and Searching
19.
20.
Suppose a value is repeated in the array that the binary search algorithm
searches. If you search for that value, which index is returned?
How can you modify the binary search algorithm to return the lowest index at
which a (possibly repeated) value occurs? (Hint: If the range has length 1, the
answer is easy. If a[mid] < value, then you know the lowest value must be in the
upper half.)
Practiceit Now you can try these exercises at the end of the chapter: R12.16, P12.12, P12.8.
programming tip 12.1
libraryfunctionsforSortingandBinarySearch
If you need to sort or search values in your own programs, there is no need to implement
your own algorithms. You can simply use functions in the C++ library. This note gives you a
brief overview of the library functions for sorting and binary search. For more information on
using library algorithms, see Big C++, 2nd ed., by Cay Horstmann and Tim Budd (John Wiley
& Sons, Inc., 2009).
You sort an array by calling the sort function with a pointer to the beginning and the end
of the array:
sort(a, a +
size);
Here size is the size of the array. For example,
int a[5] = { 60, 47, 70, 39, 6 };
sort(a, a + 5); // Now a contains
6, 39, 47, 60, 70
For a vector, the call looks slightly different:
sort(v.begin(), v.end());
The expressions v.begin() and v.end() are iterators that denote the beginning and ending positions of the vector. (As you will see in the next chapter, an iterator denotes a position in a
container.)
If you have a sorted array or vector, you can use the librarys binary_search function to test
whether it contains a given value. For example, the call
binary_search(a, a +
size, value)
returns true if the array a contains value. (Unlike our binary search function from Section 12.6,
the library function does not return the position where the value was found.)
To search a vector, you call
binary_search(v.begin(), v.end(), value)
To use the sort or binary_search functions, you must include the <algorithm> header.
Special topic 12.2
DefininganorderingforSortingobjects
When you use the sort library function, you must ensure that it is able to compare elements.
Suppose that you want to sort an array of Employee objects. The compiler will complain that it
does not know how to compare two employees.
There are several ways to overcome this problem. The simplest is to define the < operator
for Employee objects:
bool operator<(const Employee& a, const Employee& b)
{
return a.get_salary() < b.get_salary();
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
Chapter Summary
19
}
The curious name
operator< indicates that this function defines a comparison operator. (See
ch12/stlsort/stlsort.cpp for an example program.) For more information about defining your
own operators, see Big C++, 2nd ed., by Cay Horstmann and Tim Budd, Chapter 14 (John
Wiley & Sons, Inc., 2009).
This < operator compares employees by salary. If you call sort to sort an array of employees, they will be sorted by increasing salary.
Random Fact 12.1
people and companies use computers
to organize just about every aspect of
their lives. on the whole, computers
are tremendously good for collecting
and analyzing data. in fact, the power
offered by computers and their software makes them seductive solutions
for just about any organizational problem. it is easy to lose sight of the fact
that using a computer is not always the
best solution to a problem.
in 1983, the author John Bear wrote
about a person who had come up with
a novel use for the personal computers that had recently become available. that person cataloged his necktie
collection, putting descriptions of the
ties into a database and generating
reports that listed them by color, price,
or style. We can hope he had another
use to justify the purchase of a piece
of equipment worth several thousand
dollars, but that particular application
was so dear to his heart that he wanted
the world to know about it. perhaps not
surprisingly, few other computer users
Cataloging Your necktie Collection
shared that excitement, and you dont
find the shelves of your local software
store lined with necktie-cataloging
software.
the phenomenon of using technology for its own sake is quite widespread. in the internet bubble of
2000, hundreds of companies were
founded on the premise that the internet made it technologically possible to
order items such as groceries and pet
food from a home computer, and therefore the traditional stores would be
replaced by web stores. however, technological feasibility did not ensure economic success. trucking groceries and
pet food to households was expensive,
and few customers were willing to pay
a premium for the added convenience.
Many elementary schools spend
tremendous resources to bring computers and the internet into the classroom. indeed, it is easy to understand
why teachers, school administrators,
parents, politicians and equipment
vendors are in favor of computers in
classrooms. isnt computer literacy
absolutely essential for youngsters in
the new millennium? isnt it particularly important to give low-income
kids, whose parents may not be able
to afford a home computer, the opportunity to master computer skills?
however, the total cost of running
computers far exceeds the initial cost
of the equipment. Some schools have
had to make hard choicesshould they
lay off librarians and art instructors to
hire more computer technicians, or
should they let the equipment become
useless? it is easy to get caught up in
the technology hype without questioning whether the educational benefits
justify the expense.
as computer programmers, we like
to computerize everything. as computer professionals, though, we owe it
to our employers and clients to understand which problems they want to
solve and to deploy computers and
software only where they add more
value than cost.
Chapter SuMMarY
Describetheselectionsortalgorithm.
The selection sort algorithm sorts an array by repeatedly finding the smallest
element of the unsorted tail region and moving it to the front.
measuretherunningtimeofafunction.
To measure the running time of a function, get the current time immediately
before and after the function call.
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
20
Chapter 12 Sorting and Searching
Usethebig-ohnotationtodescribetherunningtimeofanalgorithm.
Computer scientists use big-Oh notation to describe how fast a function grows.
Selection sort is an O(n2) algorithm. Doubling the data set means a fourfold
increase in processing time.
Describethemergesortalgorithm.
The merge sort algorithm sorts an array by cutting the array in half, recursively
sorting each half, then merging the sorted halves.
contrasttherunningtimesofthemergesortandselectionsortalgorithms.
Merge sort is an O(n log(n)) algorithm. The n log(n) function grows much more
slowly than n2.
Describethelinearsearchandbinarysearchalgorithmsandtheirrunningtimes.
A linear search examines all values in an array until it finds a match or reaches
the end.
A linear search locates a value in an array in O(n) steps.
A binary search locates a value in a sorted array by determining whether
the value occurs in the first or second half, then repeating the search in one of
the halves.
A binary search locates a value in an array in O(log(n)) steps.
revieW exerCiSeS
r12.1 Checking against off-by-one errors. When writing the selection sort algorithm of
Section 12.1, a programmer must make the usual choices of < against <=, size against
size - 1, and next against next + 1. This is fertile ground for off-by-one errors. Make
code walkthroughs of the algorithm with arrays of length 0, 1, 2, and 3 and check
carefully that all index values are correct.
r12.2 What is the difference between searching and sorting?
r12.3 For the following expressions, what is the order of the growth of each?
a.n2 + 2n + 1
b.n10 + 9n9 + 20n8 + 145n7
c. (n + 1)4
d.(n2 + n)2
e. n + 0.001n3
f. n3 - 1000n2 + 109
g.n + log(n)
h.n2 + n log(n)
i. 2n + n2
j.
(n3 + 2n)
(n 2 + 0.75)
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
review exercises
21
r12.4 You determined that the actual number of visits in the selection sort algorithm is
T ( n) = 1 n 2 + 5 n 3
2
2
You then characterized this function as having O(n2) growth. Compute the actual
ratios
T ( 2, 000 ) T (1, 000 )
T ( 5, 000 ) T (1, 000 )
T (10, 000 ) T (1, 000 )
and compare them with
f ( 2, 000 ) f (1, 000 )
f ( 5, 000 ) f (1, 000 )
f (10, 000 ) f (1, 000 )
where f (n) = n2.
r12.5 Suppose algorithm A takes five seconds to handle a data set of 1,000 records. If the
algorithm A is an O(n) algorithm, how long will it take to handle a data set of 2,000
records? Of 10,000 records?
r12.6 Suppose an algorithm takes five seconds to handle a data set of 1,000 records. Fill in
the following table, which shows the approximate growth of the execution times
depending on the complexity of the algorithm.
For example, because 30002 / 10002 = 9, the O(n2) algorithm would take 9 times as
long, or 45 seconds, to handle a data set of 3,000 records.
O (n)
1,000
O (n2)
O (n3)
O (n log(n))
O (2n )
5
5
5
5
5
2,000
3,000
45
10,000
r12.7 Sort the following growth rates from slowest growth to fastest growth.
O(n)
O(n log(n))
O(n3 )
O( 2n )
O(nn )
O( n )
O(log(n))
O(n n )
O(n 2 log(n))
O(nlog(n) )
r12.8 What is the order of complexity of the standard algorithm to find the minimum
value of an array? Of finding both the minimum and the maximum?
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
22
Chapter 12 Sorting and Searching
r12.9 What is the order of complexity of the following function?
int count(int a[], int size, int c)
{
int count = 0;
for (int i = 0; i < size; i++)
{
if (a[i] == c) { count++; }
}
return count;
}
r12.10 Your task is to remove all duplicates from an array. For example, if the array has the
values
4
7
11
4
9
5
11
7
3
5
then the array should be changed to
4
7
11
9
5
3
Here is a simple algorithm. Look at a[i]. Count how many times it occurs in a.
If the count is larger than 1, remove it. What is the order of complexity of this
algorithm?
r12.11 Modify the merge sort algorithm to remove duplicates in the merging step to obtain
an algorithm that removes duplicates from an array. Note that the resulting array
does not have the same ordering as the original one. What is the efficiency of this
algorithm?
r12.12 Develop an O(n log(n)) algorithm for removing duplicates from an array if the
resulting array must have the same ordering as the original array. When a value
occurs multiple times, all but its first occurrence should be removed.
r12.13 Consider the following sorting algorithm. To sort an array a, make a second array b
of the same size. Then insert elements from a into b, keeping b in sorted order. For
each element, call the binary search function of Exercise P12.6 to determine where it
needs to be inserted. To insert an element into the middle of an array, you need to
move all elements above the insert location up.
Is this an efficient algorithm? Estimate the number of element visits in the sorting
process. Assume that on average half of the elements of b need to be moved to insert
a new element.
r12.14 Make a walkthrough of selection sort with the following data sets:
a. 4
b. 7
7
6
11
8
4
7
9
5
5
9
11
0
7
11
3
10
5
5
8
r12.15 Make a walkthrough of merge sort with the following data sets:
a. 5
b. 9
11
0
7
11
3
10
5
5
4
8
7
7
11
6
4
8
9
7
5
r12.16 Make a walkthrough of the following:
a.Linear search for 7 in
b.Binary search for 8 in
c. Binary search for 8 in
7
7
7
1
2
1
3
2
2
3
3
3
4
4
5
7
7
7
11
8
10
13
11
13
13
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
programming exercises
23
prograMMing exerCiSeS
P12.1 Modify the selection sort algorithm to sort an array of strings by increasing length.
P12.2 Modify the selection sort algorithm to sort a vector of integers.
P12.3 Write a program that automatically generates the table of sample runs of the selec-
tion sort times as in Figure 1. The program should ask for the smallest and largest
value of n and the number of measurements, then make all sample runs and display
the table.
P12.4 Modify the merge sort algorithm to sort a vector of employees by salary.
P12.5 Write a telephone lookup program. Read a data set of 1,000 names and telephone
numbers from a file that contains the numbers in random order. Handle lookups by
name and also reverse lookups by phone number. Use a binary search for both
lookups.
P12.6 Consider the binary search function in Section 12.6.2. If no match is found, the
function returns 1. Modify the function so that it returns a bool value indicating
whether a match was found. Add a reference parameter pos, which is set to the
location of the match if the search was successful. If a was not found, set pos to the
index of the next larger value instead, or to the array size if a is larger than all the
elements of the array.
P12.7 Use the modification of the binary search function from Exercise P12.6 to sort an
array. Make a second array of the same size as the array to be sorted. For each
element in the first array, call binary search on the second array to find out where the
new element should be inserted. Then move all elements above the insertion point
up by one slot and insert the new element. Thus, the second array is always kept
sorted. Implement this algorithm and measure its performance.
P12.8 Implement the binary_search function of Section 12.6.2 without recursion. Hint:
While from
searched.
< to, update either from or to, depending on which range should be
P12.9 Implement the merge_sort function without recursion, where the size of the array is a
power of 2. First merge adjacent regions of size 1, then adjacent regions of size 2,
then adjacent regions of size 4, and so on.
P12.10 Implement the merge_sort function without recursion, where the size of the array is
an arbitrary number. Hint: Keep merging adjacent areas whose size is a power of 2,
and pay special attention to the last area in the array.
P12.11 Write a program that sorts a vector of Employee objects by the employee names and
prints the results. Use the sort function from the C++ library.
P12.12 Write a program that keeps an appointment book. Make a class Appointment that
stores a description of the appointment, the appointment day, the starting time, and
the ending time. Your program should keep the appointments in a sorted vector.
Users can add appointments and print out all appointments for a given day. When a
new appointment is added, use binary search to find where it should be inserted in
the vector. Do not add it if it conflicts with another appointment.
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
24
Chapter 12 Sorting and Searching
anSWerS to SelF-CheCk QueStionS
1. 1 | 5 4 3 2 6, 1 2 | 4 3 5 6, 1 2 3 4 5 6
2. In each step, find the maximum of the remaining elements and swap it with the cur-
rent element (or see Self Check 3).
3. The modified algorithm sorts the array in descending order.
4. Dropping the temp variable would not work. Then a[i] and a[j] would end up being
the same value.
5. Four times as long as 40,000 values, or about 50 seconds.
6. A parabola.
7. It takes about 100 times longer.
8. If n is 4, then 1 n 2 is 8 and 5 n 3 is 7.
2
2
9. The first algorithm requires one visit, to store the new element. The second algorithm requires T(p) = 2 (n p 1) visits, where p is the location at which the element is removed. We dont know where that element is, but if elements are removed
at random locations, on average, half of the removals will be above the middle and
half below, so we can assume an average p of n / 2 and T(n) = 2 (n n 2 1) = n 2.
10. The first algorithm is O(1), the second O(n).
11. Let n be the length of the array. In the kth step, we need k visits to find the minimum.
To remove it, we need an average of k 2 visits (see Self Check 9). One additional
visit is required to add t to the end. Thus, the kth step requires 2k 1 visits. Because
k goes from n to 2, the total number of visits is
2n 1 + 2(n 1) 1 + ... + 2 3 1 + 2 2 1
= 2(n + (n 1) + ... + 3 + 2 + 1 1) (n 1)
= n(n + 1) 2 n + 1 (because 1 + 2 + 3 + ... + (n 1) + n = n(n + 1)2)
= n2 3
Therefore, the total number of visits is O(n2).
12. When the preceding while loop ends, the loop condition must be false, that is, i1 > mid
(in which case the first loop isnt executed), or i2 > to (in which case the second loop
isnt executed).
13. First sort 8 7 6 5. Recursively, first sort 8 7. Recursively, first sort 8. Its sorted. Sort
7. Its sorted. Merge them: 7 8. Do the same with 6 5 to get 5 6. Merge them to 5 6 7
8. Do the same with 4 3 2 1: Sort 4 3 by sorting 4 and 3 and merging them to 3 4. Sort
2 1 by sorting 2 and 1 and merging them to 1 2. Merge 3 4 and 1 2 to 1 2 3 4. Finally,
merge 5 6 7 8 and 1 2 3 4 to 1 2 3 4 5 6 7 8.
14. If the array size is 1, return its only element as the sum. Otherwise, recursively compute the sum of the first and second subarray and return the sum of these two values.
15. Approximately 100,000 log(100,000) / 50,000 log(50,000) = 2 5 / 4.7 = 2.13 times
the time required for 50,000 values. Thats 2.13 97 milliseconds or approximately
207 milliseconds.
2n log( 2n)
(1 + log( 2))
16.
=2
For n > 2, this is a value < 3.
n log(n)
log(n)
17. On average, youd make 500,000 comparisons.
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
answers to Self-Check Questions 25
18. The algorithm may falsely report that an element is not present. For example,
consider the task of finding 9 in the array 17 5 9 8 1 32 20 12. Because a[3] < 9, the
algorithm will look at the second half of the array 1 32 20 12, which does not contain
the value 9.
19. Any of the indexes at which the element exists may be returned. For example, if the
array is 1 4 5 5 5 7 8 8, and you search for 5, then a[3] is visited first, and a position of
3 is returned.
20. int binary_search(int a[], int from, int to, int value)
{
if (from == to) // Range has length 1
{
if (a[from] == value) { return from; }
else { return -1; }
}
int mid = (from + to) / 2;
if (a[mid] < value) // Value must be in the upper half
{
return binary_search(a, mid + 1, to, value);
}
else // Keep on searching in the lower half
{
return binary_search(a, from, mid, value);
}
}
C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved.
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more.
Course Hero has millions of course specific materials providing students with the best way to expand
their education.
Below is a small sample set of documents:
BYU - CS - 142
8/31/2011This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.CS679:AdvancedNLPObjectivesforToday1.2.3.4.5.Quickcourseinfo.OverviewofTextMiningDiscussyourapplicationsofTextMiningElementsofTextMiningIntro
BYU - CS - 142
8/31/2011This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.CS679:AdvancedNLPObjectivesforToday1.2.3.4.5.Quickcourseinfo.OverviewofTextMiningDiscussyourapplicationsofTextMiningElementsofTextMiningIntro
BYU - CS - 142
Derived Classesclass Questioncfw_public:Question();void set_text(string question_text);void set_answer(string correct_response);bool check_answer(string response) const;void display() const;private:string text;string answer;;class ChoiceQuest
BYU - MATH - 110
Math 110 All SectionsFall 2011Exam 2September 29 - October 5All questions are multiple choice. There is no time limit. No notes are allowed.Please do not write on the exam.11. For the polynomial f (x) = 3(x2 +1)(x 9)3 nd the zeros and determineif
BYU - MATH - 110
MATH 110 - Exam 4 - Fall 2010 - All SectionsNo books, notes, or calculators allowed.Do NOT write on this exam.There is no time limit.1.Which one of the conics is represented by the equation 2 x 2 + 6 x ! 3 y 2 = 6( x ! 1) + 9(a) circle2.( 2,2)(
BYU - MATH - 110
Math 110Exam IVFall 2009All Sections including Salt Lake Center1. The equation 4x2 + y 2 8x + 4y + 4 = 0 is what type of conic section:(d) circle(a) parabola(b) ellipse(c) hyperbola(e) none of the above2. What is the center of the ellipse given
BYU - MATH - 110
MATH 110 - Exam 3 - Fall 2010 - All SectionsNo books, notes, or calculators allowed.Do NOT write on this exam.There is no time limit.1. Find the domain of the composite function f g where f (x) =(a)1x x = ,x = 72(c)x x = 2, x =(b)11x x = ,x
BYU - MATH - 110
MATH 110 - Exam 3 - Winter 2011 - All SectionsNo books, notes, or calculators allowed.Do NOT write on this exam.There is no time limit.1. Find the domain of the composite function f g where f (x) =(a)1x x = , x = 32(b)x x = 1, x = 12(c)x x =
BYU - MATH - 110
Math 110January 1221, 2011Test 1All Sections including SL CenterDo not write on this exam.yThe graph of y = f (x) is given:1321112x32Choose the graph for each of the following functions. An answer may be used more than once.1. y = f (|x|
BYU - MATH - 110
Math 110September 818, 2010Test 1All Sections including SL CenterDo not write on this exam.yThe graph of y = f (x) is given:1321112x32Choose the graph for each of the following functions. An answer may be used more than once.1. y = f (|x
BYU - MATH - 110
MATH 110 - Exam 2 - Fall 2010 - All SectionsNo books, notes, or calculators allowed.Do NOT write on this exam.There is no time limit.1. Which of the following functions could have the graph shown below?(a) f (x) = (x + 3)4 + 4(b) f (x) = (x + 3)4 +
BYU - MATH - 110
Math 110September 717, 2011Test 1All Sections including SL CenterDo not write on this exam.yThe graph of y = f (x) is given:1321112x32Choose the graph for each of the following functions. An answer may be used more than once.1. y = f (x)
BYU - MATH - 110
Math 110September 818, 2010Test 1All Sections including SL CenterDo not write on this exam.yThe graph of y = f (x) is given:1321112x32Choose the graph for each of the following functions. An answer may be used more than once.1. y = f (|x
BYU - MATH - 110
Math 110 Placement Exam SolutionsFRACTION ARITHMETIC111. Find the sum. +231321+=+=Solution Convert to a common denominator. Then add numerators.23663+25=66Google Help: addition of fractions2. Find the dierence.12233412Solution Co
BYU - MATH - 110
Math 110September 919, 2009Test 1All Sections including SL CenterDo not write on this exam.yThe graph of y = f (x) is given:1321112x32Choose the graph for each of the following functions. An answer may be used more than once.1. y = f (|x
BYU - MATH - 110
Math 110 Placement ExamA prepared student for Math 110 should be familiar with the material in this exam and be able tosolve the problems without a calculator and without working backwards from the answers.FRACTION ARITHMETIC1. Find the sum.(a)351
BYU - MATH - 110
Math 110 All Sections Exam 4April 1st -7thWinter 20101. Which one of the conics is represented by the equationa) hyperbola b) parabola c) ellipsed) circlee) none of the above2. Find the asymptotes of the hyperbolaa)b)c)d)3. Find the center of
BYU - MATH - 110
Math 110April 1617, 1921, 2010Final ExamAll Sections including SL Center1. What is the range of the function dened by the equation y = x2 + 6x + 3?(a) (, 3](b) (, 6](c) (, 9](d) (, 12] (e) (, 15] (f) (, 18]2. If b and c are real numbers so that t
BYU - MATH - 110
Math 110April 16, 1821, 2011Final ExamAll Sections including SL CenterDo not write on this exam.1. What is the range of the function dened by the equation y = 2x2 + 4x?(a) [4, )(b) (4, )(c) [2, )(d) (2, )(e) (, 4) (f) (, 2)2. If b and c are rea
BYU - MATH - 110
Math 110Dec 1418, 2009Final ExamAll Sections including SL CenterDo not write on this exam.1. What is the range of the function dened by the equation y = x2 + 4x?(a) [4, )(b) (4, )(c) [2, )(d) (2, )(e) (, 4] (f) (, 2)2. If b and c are real numbe
BYU - MATH - 110
Math 110Dec 1317, 2010Final ExamAll Sections including SL CenterDo not write on this exam.1. What is the domain of the function dened by the equation y =(x + 1)2?x3 2x2 + x(a) (, 1) (1, 0) (0, 1) (1, )(b) (, 0) (0, 1) (1, )(c) (, 1) (1, 0) (0,
BYU - MATH - 110
Math 110 Exam 3: Winter 20104 Mar 2010 - 10 Mar 20101. Find the domain of the f g where f ( x) =(b) cfw_ x| x3, x(c) cfw_ x| x0, x2/32. Find the inverse of f ( x) =(c) f ( x) =14x4x+x+243, x(f) cfw_ x| x00, x(e) cfw_ x| x0(b) f 1 (
BYU - MATH - 111
Math 111 Test 31. Find the exact value oftan13.3(a) 0(b)2(c)637(e) 64(f) 3(d) 2. Find the exact value of cos1 (cos 2 )(a) 2(b) (c) 02(e) 3(f) None of above(d) 3. Solve the equation for x. 50 sin1 (x) = 25 .(a) 0(b) 1(c) 11
BYU - MATH - 111
Math 111 Test 1The graph of y = f (x) is given in Figure 1:Figure 1: Graph of f (x)Match the graphs (a)-(h) to the functions (numbered 1-8). An answer may be used more thanonce.1. y = f (x) + 32. y = 1 f (x)23. y = f ( 1 x)24. y = f (x 1)5. y =
BYU - MATH - 111
Math 111 Test 21. The measure of an angle is 130 . Find the measure of the angle in radians.(a)(b)(c)(d)(e)1318131813181318342. The length of a arc is 6 and the radius of the circle is 10, nd the measure of the centralangle.(a)3535
BYU - MATH - 111
Math 111Winter 2011Test 3%1. Find the exact value of cos $% &'(.A. 0B.*C.+*D.,*E.-*F. Does not exist'0*2. Find the exact value of sin$% &sin & 1 (.A. 3*1*B. 1C.*D.13*E.13. Solve the following equation for x: 24 tan$%() = 4
BYU - MATH - 111
Math 111Test 21. The measure of an angle in radians isA.B.. Find the measure of the angle in degrees.C.E. 35D.2. s denotes the length of the arc of a circle of radius r subtended by a central angle . If the arclength s is 6 ft, and the radius r
BYU - MATH - 111
Math 111Test 1The graph of2is given:1-4-3-2-101234-1-2Choose the graph for each of the following functions. An answer may be used more than once.1.5.2.6.A.3.7.4.8.B.221-4-3-2-1101234-4-3-2-10C.D.-2-1012
BYU - MATH - 111
Math 111PretestyThe graph of y = f (x) is given:1321112x32Choose the graph for each of the following functions. An answer may be used more than once.1. y = f (|x|)2. y = f (x)3. y = 2f (2x)4. y = |f (x)|5. y = f (x + 1)6. y = f (x) 17
BYU - MATH - 111
MATH 111 - Final - Summer 2011 - Section 1No books, notes, or calculators allowed.There is no time limit.1. In a right triangle, A = 53 and b = 6. Find the length of the hypotenuse c.6(a) sin(53 )(b) 6 sin(53 )6(c) cos(53 )(d) 6 cos(53 )6(e) ta
BYU - MATH - 111
Math 111Final Exam1. In the given right triangle = 7 and = 40. Find the hypotenuse c.(a) 7 sec 50(b) 7 cos 40(d) 7 sin 50c(c) 7 csc 40(e) 7 sec 40b40 = 7(Drawingnottoscale)(f) 7 sin 402. Use fundamental identities and/or the complementary an
BYU - MATH - 111
MATH 111 - Exam 2 - Summer 2011 - Section 1No books, notes, or calculators allowed.Do NOT write on this exam.There is no time limit.1. The measure of an angle is 75 . Find the measure of the angle in radians.(a)512(b) 512(c)34(d)5125(e) 1
BYU - MATH - 111
Math 111Final Exam1. In the given right triangleand. Find the length of leg a.c(a)(b)b=9(c)62(d)(e)(f)(Drawing not to scale)2. Use fundamental identities and/or the complementary angle theorem to find the value of thefollowing expression:
BYU - MATH - 111
MATH 111 - Exam 1 - Summer 2011 - Section 1No books, notes, or calculators allowed.Do NOT write on this exam.There is no time limit.For problems 1-4 choose the equation that yields the given graph.1. (a) (x 1)2 + y 2 = 1(b) (x 1)2 + (y 2)2 = 1(c) (
BYU - MATH - 111
MATH 111 - Exam 3 - Summer 2011 - Section 1No books, notes, or calculators allowed.There is no time limit.1. Find the exact value of tan1 (1).(a)4(b)34(c)54(d)74(e)94(f)114(f)109(f)232. Find the exact value of cos1 (cos( 10 ).9
Rutgers - POLI SCI - 381
Rutgers UniversityDepartment of Political ScienceComparative Politics: Post-communist Democracies (790:381)Kelly ClancyFall 2010Kelly.A.Clancy@gmail.comRoom: SC 214Office Hours: Thursdays 3-5, Hickman Hall 401Course Overview:This class will exami
St. Johns - MGMT - 600
Case Study on: GlaxoSmithKlineCaseOBJECTIVE:GSKTOSUPPLYANTIRETROVIRALSANDANTI-MALARIALSATSUSTAINABLE PREFERENTIAL PRICES FOR ELIGIBLECUSTOMERS IN THE LEAST DEVELOPED COUNTRIES ANDALL SUB-SAHARAN AFRICAN COUNTRIES.Eligible customers include th
St. Johns - MGMT - 600
Case Study on: GlaxoSmithKlineCaseOBJECTIVE:GSKTOSUPPLYANTIRETROVIRALS AND ANTI-MALARIALS ATSUSTAINABLE PREFERENTIAL PRICES FOR ELIGIBLECUSTOMERS IN THE LEAST DEVELOPED COUNTRIESAND ALL SUB-SAHARAN AFRICAN COUNTRIES.Eligible customers include th
St. Johns - MGMT - 600
CATEGORYPARAMETERENERGYABBREVATIONENERGYSCOREEXERCISESEDENTRYMILDOCCASIONALREGULARE1E2E3E44321DIET1 MEAL2 MEALS3 MEALSE4E2E3432CAFFEINE1 CUP2 CUPS3 CUPS4 CUPSE1E2E3E44321ALCOHOLANY AMOUNTNO ALCOHOLE4E114TOB
St. Johns - MGMT - 600
Country case study-South Africa-Presented by Jigar.R.Rajpura.1FAST facts about the country:Government: Democratic.Only country in the world with 3 capitals.Ranked 48th in GLOBALISATION INDEX of 68.23HealthcareENVIRONMENT4Healthcare system in
St. Johns - MGMT - 600
Presented by Jigar.R.Rajpura.1Government: Democratic.Only country in the world with 3 capitals.Ranked 48th in GLOBALISATION INDEX of 68.23452-tier system of healthcare prevalent.PRIVATE SECTOR:Highly specialized & hi-tech.Caters to middle-high
St. Johns - MGMT - 600
FACING THE CHALLENGE ONE YEAR ONA progress report on our contribution to improving healthcare in the developing world02F ACIN G T H E C HALLENG E O N E Y EA R O NCONTENTS4&56&7891011Preferential Pricing ArrangementsResearch and DevelopmentCom
St. Johns - MGMT - 600
FACING THE CHALLENGE ONE YEAR ONA progress report on our contribution to improving healthcare in the developing world02FACING THE CHALLENGE ONE YEAR ONCONTENTS4&56&7891011Preferential Pricing ArrangementsResearch and DevelopmentCommunity Inve
St. Johns - MGMT - 600
Name: Jigar.RajpuraX-01824544Instructor: Dr.PalCourse:PAS256Summer-II 2008, Final Exam A case control study of the association between polymorphisms of the endothelial nitric oxidesynthase and glycoprotein IIIa genes and upper gastrointestinal bleed
St. Johns - MGMT - 600
FINAL EXAMINATION PROPOSALI am a business representative of a small scale pharmaceutical company producing coatedtablets locally, I would be putting forth a proposal to a MNC for permitting me to produce one ofits product as and under a LOAN LICENSE fo
St. Johns - MGMT - 600
FINAL EXAMINATION PROPOSALI am a business representative of a small scale pharmaceutical company producing coatedtablets locally, I would be putting forth a proposal to a MNC for permitting me to produce one ofits product as and under a LOAN LICENSE fo
St. Johns - MGMT - 600
ANALYSIS OF MARKET POTENTIAL OF MAHARASHTRA STATEFOR IRON, CALCIUM AND CEFADROXYL PRIOR MARKETLAUNCH A SYSTEMATIC SURVEY.Authors: Shah Jimit, Joshi Omkar, Mantri Rajiv, Shah Jinish, Madgulkar Ashwini, DatePravin, Bhandari Shashikant V*.* Author for c
St. Johns - MGMT - 600
JIGAR.R.RAJPURA.X-01824544.FINALTERM PAPER.PAS 217PROBLEM STATEMENT:TO ESTIMATE THE MARKET POTENTIAL OF NEW-YORK STATE FOR HERBALBASED ENERGY SUPPLEMENT PRIOR TO THE MARKET LAUNCHHYPOTHESIS:NULL HYPOTHESIS: There is no possible market for the sale o
St. Johns - MGMT - 600
PROCUREMENT OF ANTIRETROVIRAL MEDICINES IN SOUTH AFRICAThis short case study is based on an Oxfam GB briefing paper entitled Assessing theImpact of TRIPs-Plus Patent Rules in the Proposed US-SACU Free Trade Agreement(2005) by Jonathan Berger and Achal
St. Johns - MGMT - 600
Glaxosmithkline IN South AfricaOBJECTIVE: SUPPLY ANTIRETROVIRALS ANDANTI-MALARIALS AT SUSTAINABLE PREFERENTIALPRICES TO ELIGIBLE CUSTOMERS IN THE LEASTDEVELOPED COUNTRIES AND ALL SUB-SAHARANAFRICAN COUNTRIES.Eligible customers include the public sec
St. Johns - MGMT - 600
Global issues in pharmaceutical marketing:Marketing literally means promoting. It is the process of identifying the consumers' wants andneeds and making the product to satisfy these. The term includes advertising, distribution andselling of a product o
St. Johns - MGMT - 600
Global issues in pharmaceutical marketing:Marketing literally means promoting. It is the process of identifying the consumers' wants andneeds and making the product to satisfy these. The term includes advertising, distribution andselling of a product o
St. Johns - MGMT - 600
globalint.qxd06/08/0315:36Page 1IntroductionTHE STRATEGIC PLANNING CYCLEDefining a brand is not something that is generally left to chance. A brandis a construct and not a living and breathing organism, as some wouldhave us believe, and as much of
St. Johns - MGMT - 600
PHOENIX pharmaceuticals ltd.JIGAR.R.RajpuraBusiness representativeA mythical bird that never dies, the Phoenixflies far ahead to the front, always scanning thelandscape and distant space. It represents ourcapacity for vision, for collecting sensory
St. Johns - MGMT - 600
RESEARCH PROPOSAL:1.) Defining the market problem:ANALYSIS OF MARKET POTENTIAL OF NEW YORK STATE FOR VITAMINBASED ENERGY SUPPLEMENT PRIOR MARKET LAUNCHSUMMARY:In the recent pharma market there is a boom of vitamin supplements, both herbal andorganic
St. Johns - MGMT - 600
IMPORTANT NOTICE:The information in this PDF file is subject to Business Monitor Internationals full copyrightand entitlements as defined and protected by international law. The contents of the file are for thesole use of the addressee. All content in
St. Johns - MGMT - 600
SURVEY TO FIND THE ACCESSIBILITY AND THE AWARNESS OFMINORITY WOMEN TOWARDS THE HEALTHCARE SYSTEMWomen forms about half of U.S. population. Womens health needs changes as they age.Over the period of time they reflect changing health needs which can be s
St. Johns - MGMT - 600
SURVEY TO FIND THE ACCESSIBILITY AND THE AWARNESS OFMINORITY WOMEN TOWARDS THE HEALTHCARE SYSTEMWomen forms about half of U.S. population. Womens health needs changes as they age.Over the period of time they reflect changing health needs which can be s