// This program implements the Bubble sort algorithm (page 76 of Skansholm) // using a vector to contain the data and vector iterators to access the elements #include #include #include #include #include void swap( vector::iterator p, vector::iterator q) { int pv = *p; *p=*q; *q=pv; } void sort( vector::/******/ begin, vector::iterator end) { int swaps; do{ swaps=0; for(vector::iterator p = begin+1; p != end; p++) { if(*(p-1)>*p) { swaps++; swap(p-1, p); } } }while(swaps > 0); } 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 << "Bubble Sort. Size =" << Size << ", mean time ="; cout << total_time /Sample; return 0; }