int FindMin(IntArray arr, int start, int end)
{
int min = arr[start];
int where = start;
for (int j = start + 1; j <= end; j++)
{
if (arr[j] < min)
{
min = arr[j];
where = j;
}
}
return where;
}
int Find(IntArray arr, int match, int end)
{
for (int j = 0; j <= end; j++)
if (arr[j] == match)
return j;
return -1;
}
int BinSearch (SortedArray arr, int match, int start, int end)
{
if (start == end)
return start;
else
{
int middle = (start + end) / 2;
if (match < arr[middle])
return BinSearch(arr, match, start, middle-1);
else if (match > arr[middle])
return BinSearch(arr, match, middle+1, end);
else
return middle;
}
}
| Recursive Functions must have: 1) an exit case (trivial case) 2) one or more recursive cases, in which the problem is reduced to a simpler problem |
//
INTARRAY.H
// An array of integers, with boundschecking and careful memory management.
#include <iostream.h>
class IntArray
{
friend ostream& operator<< (ostream& os, const IntArray& a);
public:
IntArray(); // Construct a 10element IntArray of zeros
IntArray(int n); // Construct a zero IntArray of size n
IntArray(const IntArray& a); // Copy constructor
~IntArray() {delete data;}; // inline function to delete the array of data
Length() {return (upper + 1);}; // inline function which returns actual length of array
int& operator[] (int I) const;
IntArray& operator= (const IntArray& a);
private:
int upper; // the upper bound on array indices
int* data; // ptr to the array elements (indices 0 .. upper)
};
|
The copy constructor for an object is invoked: 1) an object is defined to have the value of another object of the same type 2) an object is passed to a function by value 3) an object is returned by a function |
IntArray ZeroTail(IntArray arr, int end) // 2 here
{
IntArray newArray = arr; // 1 here
for (int j = end; j < Length(arr); j++)
NewArray[j] = 0;
return newArray; // 3 here
}
|
The copy constructor for a type TypeName is declared with a reference argument: TypeName (const TypeName&); |
| If you design a class with pointers in its member data, it is always a good idea to provide the class with its own copy constructor, assignment operator, and destructor. |
| time vs space | size of data |
| initial arrangement of data | language used |
| generated machine code | speed of computer |
for (OuterCounter = 1; OuterCounter <= N; OuterCounter++)
for (InnerCounter = 1; InnerCounter <= OuterCounter; InnerCounter++)
Control = 1;
while (Control <= N)
{
: O(log(N))
Control := 2 * Control
}
| Factorial | O(N) |
| Reverse of a String | O(N * O(TextConcatenate) |
| Permuations of a set | O(N!) |
| Recursive binary search | O(log(N)) |
| Recursive merge/sort | O(N log(N)) |
| Create | O(1) | O(1) |
| Update | O(1) | O(N) |
| Search | O(N) | O(Log(N)) |
| Delete | O(N) | O(N) |
| Report | O(N Log(N)) | O(N) |
void QuickSort (IntArray& arr, int start, int end)
{
if (start < end)
{
int split = Partition(arr, start, end);
QuickSort(arr, start, split);
QuickSort(arr, split+1, end);
}
}
int Partition (IntArray& arr, int start, int end)
{
int top = end + 1;
int bottom = start -1;
int pivot = arr[start];
while (top > bottom)
{
do
{ bottom++; }
while (arr[bottom] < pivot);
do
{ top--; }
while (arr[top] > pivot);
Swap(arr[top], arr[bottom]);
}
Swap(arr[top], arr[bottom]);
return top;
}
IF Size(List) > 1
Split(List, first half, second half)
MergeSort(first half of List)
MergeSort(second half of List)
Merge(two halves of List)
| Sorting Algorithm | Run Time - List in Random Order | Run Time - List in Ascending Order |
| Selection | 3.0 | 3.1 |
| Insertion | 2.4 | 0.0 |
| Shell | 0.8 | 0.4 |
| Merge | 4.0 | 3.9 |
| Quick | 0.6 | 0.3 |
| Heap | 1.4 | 1.6 |