Incorrect result
Apr 5, 2021 at 6:38am UTC
Before I wrote this program with Stooge Sort and means of time was normal.
Now I use Cocktail Sort but time is 0 in CodeBlocks (there I have .cpp and .h files)
Also I have result in online IDE
https://ideone.com/GgHXvZ
But why does It not work in CodeBlocks?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include "cmath"
#include <chrono>
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std::chrono;
int RandomNumber2() { return (std::rand() % 100); }
void FillVector(std::vector<int >& a) {
std::srand(unsigned (std::time(0)));
std::generate(a.begin(), a.end(), RandomNumber2);
std::cout << "\n\nVector: " ;
for (std::vector<int >::iterator it = a.begin(); it != a.end(); it++) std::cout << *it << " " ;
}
void CocktailSort(std::vector<int >& a, int n) {
bool flag = true ;
int start = 0, end = n-1;
while (flag){
flag = false ;
for (int i = start; i<end; i++){
if (a[i] > a[i+1]){
std::swap(a[i], a[i+1]);
flag = true ;
}
}
if (!flag){
break ;
}
flag = false ;
end--;
for (int i = end - 1; i >= start; i--){
if (a[i] > a[i+1]){
std::swap(a[i], a[i+1]);
flag = true ;
}
}
start++;
}
}
double measureTime(std::vector<int >& a, int n) {
steady_clock::time_point t1 = steady_clock::now();
CocktailSort(a, n);
steady_clock::time_point t2 = steady_clock::now();
duration<double > time_span = duration_cast<duration<double >>(t2 - t1);
double mTime = time_span.count();
std::cout << mTime << std::endl;
return mTime;
}
void Experiment(std::vector<int > &a, int n) {
FillVector(a);
CocktailSort(a, n);
std::cout << "\n\na. Sorted data: " ;
for (int i{ 0 }; i < n; i++) std::cout << a[i] << ' ' ;
double * times = new double [1000];
for (int i=100; i < 1000; i += 100) {
std::cout << "\n[" << i << "]" << " " ;
for (int j{1}; j < 10; j+=1) {
times[j] = measureTime(a, i);
}
}
}
int main() {
int n{1000};
std::vector<int > a(n);
Experiment(a, n);
return 0;
}
Last edited on Apr 5, 2021 at 6:39am UTC
Apr 5, 2021 at 6:51am UTC
What are you trying to measure?
1 2
FillVector(a);
CocktailSort(a, n);
You fill with random data, and you sort it.
times[j] = measureTime(a, i);
Every experiment you time is sorting already sorted data. Depending on the algorithm, this is very quick.
Here's what I saw.
[100] 1.043e-06
8.56e-07
1.021e-06
8.84e-07
5.84e-07
5.65e-07
5.57e-07
5.57e-07
5.55e-07
...
[900] 8.036e-06
7.756e-06
7.727e-06
8.111e-06
7.792e-06
7.904e-06
1.7007e-05
1.7423e-05
1.7041e-05
Depending on the granularity of your clock, things less than a microsecond may as well be instantaneous.
If you want meaningful times then
1. Make sure you're sorting random data.
2. Ideally make it the same random data each time (use a fixed seed)
3. Pick a much bigger value for n.
Apr 5, 2021 at 7:14am UTC
Every experiment you time is sorting already sorted data
Thanks!
Topic archived. No new replies allowed.