⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timequadsorts.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -