#include <cstdlib>
#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;
void InsertSort(double *, int);
void Disp(double *, int, char *);
double Generuj(float a, float b)
{ double w = (a + (b - a)*(double)rand()/RAND_MAX); return floor(w*100+0.5)/100; }
const int MaxObieg = 5;
int main(int argc, char *argv[ ]) // InsertSort
{
clock_t T1, T2;
double Time, oda, dob;
int nData;
if (argc <= 1 ) { nData = 22000; oda = 2.2; dob = 88.8; }
else { nData= atoi(argv[1]); oda = atof(argv[2]); dob = atof(argv[3]); }
double *A = new double[nData]; // dynamiczna tablica
for (int k = 0; k < MaxObieg; k++) {
for (int i = 0; i < nData; i++) A[i] = Generuj(oda, dob); //generowanie danych
if (k==0) { cout << "Fragment Danych ( z " << nData << "):"; Disp(A, 5, ""); }
T1 = clock();
InsertSort(A, nData); // sortowanie
T2 = clock();
Time = (T2 - T1)/(double)CLOCKS_PER_SEC;
cout << "Obieg Nr "<< k << " czas = " << Time << endl;
}
Disp(A, 5, "Po Sortowaniu:" );
cout << "Koniec InsertSort- Wcisnij Enter"; cin.get();
delete [ ] A;
return 0;
}
void InsertSort(double *X, int size)
{
int i, j;
double v;
for(i=1; i < size; i++) {
v = X[i]; j = i-1;
while (v < X[j] && j >= 0) { X[j+1] = X[j]; j--; }
X[j+1] = v;
}
}
void Disp(double *X, int size, char *text)
{ int i;
cout << text << endl;
for (i = 0; i < size; i++) { cout << X[i] << " ";
if ((i+1)%10 == 0) cout << endl; }
if ((i+1)%10 != 0) cout << endl;
}