InsertSort

#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;

}


Wyszukiwarka

Podobne podstrony:
Instrukcja?archiwizacji dla Systemu InsERT GT w Windows XP
Zero Clearance Inserts
5 Insert for or since
Algorytmy ściąga, Insertion sort:
ALS - 009-005 - Program Sortowanie INSERTION SORT, Informatyka - uczelnia, WWSI i WAT, wwsi, SEM II,
3 Insert lately, already, yet
bd 02 creat insert
L 5211 Dress with lace insert
InsertSort1
M 5211 Dress with lace insert
4 Insert for or since
Fungitell Insert PL
Zagadnienia 09-10 Origo et insertio musculi puncta ossea
1 Insert for or since
Arduin Dungeon 4 Death Heart Inserts
Hellfrost GM Screen Inserts
INSERT GT-zadania krótkie, A Egzamin zawodowy TECHNIK EKONOMISTA!
Practical grammar Insert the appropriate modal verb