Hello:
How can I improve on my function template to also do insertion sort on strings? I'm afraid I'm very new to templates, any hints appreciated.
Template looks like so:
template <class T>
void insSort(T arr[], int size)
{
T key = arr[0];
int j = 0;
for(int i = 1; i < size; ++i)
{
key = arr[i];
j = i - 1;
while((j >= 0) && (arr[j] > key))
{
arr[j + 1] = arr[j];
j -= 1;
}
arr[j + 1] = key;
}
}
I have an array of names like so:
Name nameArray[] = { Name ( "Bob", "Davidson" ),
Name ( "Harley", "Davidson" ),
Name ( "Kobe", "Bryant" ),
Name ( "Arnold", "Schwarzenegger" ),
Name ( "Keyser", "Soze" ),
Name ( "Irwin", "Fletcher" ),
Name ( "Sean", "Connery" ),
Name ( "George", "Lazenby" ),
Name ( "Roger", "Moore" ),
Name ( "Timothy", "Dalton" ),
Name ( "Pierce", "Brosnan" ) };
The name class looks like this:
#include <string>
using namespace std;
class Name {
public:
string first;
string last;
public:
Name ( string f, string l ) {
first = f; last = l;
};
};