timequadsorts.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 65 行

CPP
65
字号
#include <iostream>#include <string>using namespace std;#include "ctimer.h"#include "tvector.h"#include "sortall.h"#include "randgen.h"#include "prompt.h"// compare running times of quadratic sortsvoid Shuffle(tvector<int> & a, int count)// precondition: a has space for count elements// postcondition: a contains 0..count-1 randomly shuffled    {    RandGen gen;    // for random # generator    int randIndex,k;        // fill with valuesl 0..count-1    for(k=0; k < count; k++)    {   a[k] = k;    }    // choose random index from k..count-1 and interchange with k    for(k=0; k < count - 1; k++)    {   randIndex = gen.RandInt(k,count-1);   // random index        Swap(a,k,randIndex);                  // swap in sortall.h    }}int main(){    int size,minSize,maxSize,incr; // sort minSize, minSize+incr, ... maxSize    CTimer timer;    cout << "min and max size of vector: ";    cin >> minSize >> maxSize;    incr =  PromptRange("increment in vector size",1,1000);        cout << endl << "n\tinsert\tselect\tbubble" << endl << endl;    for(size=minSize; size <= maxSize; size += incr)    {   tvector<int> copy(size), original(size);        cout << size << "\t";        Shuffle(original,size);                copy = original;     // sort using insertion sort        timer.Start();        InsertSort(copy,copy.size());        timer.Stop();        cout << timer.ElapsedTime() << "\t";                copy = original;     // sort using selection sort        timer.Start();        SelectSort(copy,copy.size());        timer.Stop();        cout << timer.ElapsedTime() << "\t";                copy = original;     // sort using bubble sort        timer.Start();        BubbleSort(copy,copy.size());        timer.Stop();        cout << timer.ElapsedTime() << endl;    }    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?