//This program implements the well known Selection Sort Algorithm // (like page 64 of Scanholm) // This is a handcoded version, // unlike timeSelectionSort.cpp that uses #include #include #include #include #include void swap( vector::iterator p, vector::iterator q) { int pv = *p; *p=*q; *q=pv; } void sort( vector::iterator begin, vector::iterator end) { vector::/******/ next; for (next = begin; next != end-1; next++) { vector::iterator min_pos = next; vector::iterator i; for (i = next + 1; i != end; i++) if (*i < *min_pos) min_pos = i; if ( min_pos != next) swap(next, min_pos); }//for next } int main() { const int seed = static_cast(time(0)); srand(seed);//set random number differently each run const int Biggest = 100000; const int Size = 5000; const int Sample = 100; double total_time =0.0; for(int s = 0; s data; for(int i = 0; i< Size; i++) { data.push_back(rand()%Biggest); } time_t time1 = time(0); sort(data.begin(), data.end()); time_t time2 = time(0); total_time+= difftime(time2, time1); } cout << "Selection Sort. Size =" << Size << ", mean time ="; cout << total_time /Sample; return 0; }