/********************************************************************** A simple sort program. A real program should use C++ standard library algorithm 'sort'. C.A. Bertulani, May/19/2000 **********************************************************************/ #include #include #include #include typedef int (*CFT) (const void*, const void*); /********************************************************************** Sort the "n" elements of vector "base" into increasing order using the comparison fucntion pointed t by "cmp". The elements are of size "sz. Shell sortKnuth, Vol3, p. 84) ***********************************************************************/ void ssort (void* base, size_t n, size_t sz, CFT cmp) { for (unsigned int gap=n/2; 0(base); //necessary cast char* pj =b+j*sz; // &base[j] char*pjg =b+(j+gap)*sz; //&base[j+gap] if (cmp(pjg,pj)<0) { // swap base[j] and base[j+gap]; for (unsigned int k=0; k(p)->name, static_cast(q)->name); } int cmp2 (const void* p, const void* q) // compare acct numbers { return static_cast(p)->acct - static_cast(q)->acct; } /********************************************************************** Order physicists as you wish ***********************************************************************/ int main() { int k = 8; void ssort (void* , size_t , size_t, CFT); cout << "Physicist in alphabetical order:\n"; ssort(physicist,k,sizeof(User),cmp1); print_id(physicist,k); cout<<'\n'; cout<<"Physicist in order of account number:\n"; ssort (physicist,k,sizeof(User),cmp2); print_id(physicist,k); return 0; } /***********************************************************************/