c++ - Towers of Hanoi algorithm without printing anything to terminal window -


the typical towers of hanoi problem solver following:

void hanoi(int disknumber , int start, int temp, int finish) {     if(disknumber == 1)     {         cout<< " move disk " << disknumber<<" " << start <<" "<< finish<<endl;     }     else     {         hanoi(disknumber-1,start,temp,finish);         cout<<"move disk " << start <<" "<<finish<<endl;         hanoi(disknumber - 1,temp,start,finish);     } } 

but want calculating time algorithm runs. thus:

int main {     //hanoi:     cout<<"hanoi tower problem:"<<endl;     //3 disks:     clock_t htimer3 = clock();     hanoi(3, 1,2,3);     cout<<"cpu time n = 3 is: "     <<clock() - htimer3/clocks_per_sec<<endl;     //6 disks:     clock_t htimer6 = clock();     hanoi(6, 1,2,3);     cout<<"cpu time n = 6 is: "     <<clock() - htimer6/clocks_per_sec<<endl;     //9 disks:     clock_t htimer9 = clock();     hanoi(9, 1,2,3);     cout<<"cpu time n = 9 is: "     <<clock() - htimer9/clocks_per_sec<<endl;     //12 disks:     clock_t htimer12 = clock();     hanoi(12, 1,2,3);     cout<<"cpu time n = 12 is: "     <<clock() - htimer12/clocks_per_sec<<endl;     //15 disks:     clock_t htimer15 = clock();     hanoi(15, 1,2,3);     cout<<"cpu time n = 15 is: "     <<clock() - htimer15/clocks_per_sec<<endl;     //end of hanoi tower problem     return 0; } 

the problem here example if set disknumber = 15, code's gonna run 32767 times fill out terminal window , i'll lose generated lines comes before (i have calculate other algorithms bubble sort quick sort etc. i'm gonna use numbers draw chart later represent big o, i.e: big o of towers of hanoi algorithm 2^n) . solve problem modified code:

void hanoi(int disksize, int start, int finish, int temp) {   if(disksize == 1)     {         return;     }     else     {         hanoi(disksize - 1, start, temp, finish);         hanoi(disksize - 1, temp, finish, start);     } } 

my main question: modified code, take same running time if original algorithm? if not, should do? suggestions?

yes, time complexity of modified code same previous 1 since cout takes constant time. running time not affected (granularity in order of nanoseconds) considering stream you're writing to.

i recomment redirecting output file. example:

./executable > filename 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -