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
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
108
109
110
111
112
113
114
115
|
// Fig. 5.25: fig05_25.cpp
// Multipurpose sorting program using function pointers.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
// prototypes
void bubble( int [], const int, bool (*)( int, int ) );
void swap( int * const, int * const );
bool ascending( int, int );
bool descending( int, int );
int main()
{
const int arraySize = 10;
int order;
int counter;
int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
cout << "Enter 1 to sort in ascending order,\n"
<< "Enter 2 to sort in descending order: ";
cin >> order;
cout << "\nData items in original order\n";
// output original array
for ( counter = 0; counter < arraySize; counter++ )
cout << setw( 4 ) << a[ counter ];
// sort array in ascending order; pass function ascending
// as an argument to specify ascending sorting order
if ( order == 1 ) {
bubble( a, arraySize, ascending );
cout << "\nData items in ascending order\n";
}
// sort array in descending order; pass function descending
// as an argument to specify descending sorting order
else {
bubble( a, arraySize, descending );
cout << "\nData items in descending order\n";
}
// output sorted array
for ( counter = 0; counter < arraySize; counter++ )
cout << setw( 4 ) << a[ counter ];
cout << endl;
return 0; // indicates successful termination
} // end main
// multipurpose bubble sort; parameter compare is a pointer to
// the comparison function that determines sorting order
void bubble( int work[], const int size,
bool (*compare)( int, int ) )
{
// loop to control passes
for ( int pass = 1; pass < size; pass++ )
// loop to control number of comparisons per pass
for ( int count = 0; count < size - 1; count++ )
// if adjacent elements are out of order, swap them
if ( (*compare)( work[ count ], work[ count + 1 ] ) )
swap( &work[ count ], &work[ count + 1 ] );
} // end function bubble
// swap values at memory locations to which
// element1Ptr and element2Ptr point
void swap( int * const element1Ptr, int * const element2Ptr )
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
} // end function swap
// determine whether elements are out of order
// for an ascending order sort
bool ascending( int a, int b )
{
return b < a; // swap if b is less than a
} // end function ascending
// determine whether elements are out of order
// for a descending order sort
bool descending( int a, int b )
{
return b > a; // swap if b is greater than a
} // end function descending
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
|