here is algorithm
#include<iostream>
#include<Windows.h>
#include<time.h>
using namespace std;
int main(){
int a[10]={12,3,5,2,7,80,10,1,16,30};
long ts,te;
srand(::GetTickCount());
ts=clock();
for (int i=0;i<10;i++){
for (int j=9;j>i;j--){
if (a[j]<a[j-1]){
int t=a[j];a[j]=a[j-1];a[j-1]=t;
}
}
}
te=clock();
cout<<" time elapsed "<<te-ts<<endl;
return 0;
}
but i am surprise ,because it gives me zero as output,i am measuring time elapsed from begining of code to finishing,and why?my computer is not so called supercomputer and what is wrong in this code fragment?
Unless you used punch-cards to write your program, you shouldn’t be surprised that sorting 10 numbers takes less than one tick. If you want a more accurate profile of your code, use milliseconds, that should give you a better idea.
The inner-most instruction in the loops run 100 times – that’s nothing compared to even a low-end processor nowadays.
EDIT: I tested the code with 100000 numbers, that’s 10^10 iterations inside the for loop, and it only took 3 seconds.