Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- / main.cpp : Defines the entry point for the console application.
- //
- #include \"stdafx.h\"
- #include <math.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- int const maxVelikostPole = 20;
- float poleFloat[maxVelikostPole];
- void naplPoleFloat( float pole[], int velikost )
- {
- srand( (unsigned int) time( NULL ) );
- for ( int i = 0; i < velikost; i++ ) {
- pole[i] = (float) rand() / ( (float) RAND_MAX / 20001 ) - 10000;
- }
- }
- void vypisPoleFloat( float pole[], int velikost )
- {
- for ( int i = 0; i < velikost; i++ ) {
- printf( \"%.2f\\n\", pole[i] );
- }
- }
- int kolik_ms(LPFILETIME pred, LPFILETIME po)
- {
- hyper pred64b = pred->dwHighDateTime;
- pred64b = (pred64b << 32) | pred->dwLowDateTime;
- hyper po64b = po->dwHighDateTime;
- po64b = (po64b << 32) | po->dwLowDateTime;
- return (int) ((po64b - pred64b) / 10000);
- }
- void selectSort( int od, int doo, float pole[] )
- {
- for ( int i = od; i < doo; i++ ) {
- int maxIndex = i;
- for ( int j = i + 1; j < doo; j++ ) {
- if ( pole[j] < pole[maxIndex] ) {
- maxIndex = j;
- }
- }
- int pom = pole[i];
- pole[i] = pole[maxIndex];
- pole[maxIndex] = pom;
- }
- }
- struct struktura{
- int od;
- int doo;
- };
- DWORD WINAPI vlakno(LPVOID pom)
- {
- struktura *tmp = (struktura *)pom;
- selectSort(tmp->od, tmp->doo,poleFloat);
- return 0;
- }
- void merge()
- {
- float *pole2 = new float [maxVelikostPole / 2];
- int index1 = 0;
- int index2 = maxVelikostPole / 4;
- int index3 = 0;
- int size1 = maxVelikostPole / 4;
- int size2 = maxVelikostPole / 2;
- while (index1 < size1 || index2 < size2)
- {
- if ((poleFloat[index1] < poleFloat[index2] && index1 < size1) || index2 >= size2)
- {
- pole2[index3] = poleFloat[index1];
- index1++;
- }
- else
- {
- pole2[index3] = poleFloat[index2];
- index2++;
- }
- index3++;
- }
- for ( int i = 0; i < maxVelikostPole / 2; i++ )
- {
- poleFloat[i] = pole2[i];
- }
- }
- void merge2()
- {
- float *pole3 = new float [maxVelikostPole / 2];
- int index1 = maxVelikostPole / 2;
- int index2 = (maxVelikostPole*3) /4;
- int index3 = 0;
- int size1 = (maxVelikostPole*3) / 4;
- int size2 = maxVelikostPole;
- while (index1 < size1 || index2 < size2)
- {
- if ((poleFloat[index1] < poleFloat[index2] && index1 < size1) || index2 >= size2)
- {
- pole3[index3] = poleFloat[index1];
- index1++;
- }
- else
- {
- pole3[index3] = poleFloat[index2];
- index2++;
- }
- index3++;
- }
- for ( int i = 0; i < maxVelikostPole / 2; i++ )
- {
- poleFloat[(maxVelikostPole / 2) + i] = pole3[i];
- }
- }
- void merge3()
- {
- float *pole4 = new float [maxVelikostPole ];
- int index1 = 0;
- int index2 = maxVelikostPole / 2;
- int index3 = 0;
- int size1 = maxVelikostPole / 2;
- int size2 = maxVelikostPole;
- while (index1 < size1 || index2 < size2)
- {
- if ((poleFloat[index1] < poleFloat[index2] && index1 < size1) || index2 >= size2)
- {
- pole4[index3] = poleFloat[index1];
- index1++;
- }
- else
- {
- pole4[index3] = poleFloat[index2];
- index2++;
- }
- index3++;
- }
- for ( int i = 0; i < maxVelikostPole; i++ )
- {
- poleFloat[i] = pole4[i];
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- naplPoleFloat(poleFloat,maxVelikostPole);
- FILETIME cas_pred, cas_po;
- struktura * cast1 = new struktura;
- cast1->od = 0;
- cast1->doo = maxVelikostPole /4;
- struktura * cast2 = new struktura;
- cast2->od = maxVelikostPole /4;
- cast2->doo = (maxVelikostPole * 2)/4;
- struktura * cast3 = new struktura;
- cast3->od = (maxVelikostPole * 2)/4;
- cast3->doo = (maxVelikostPole * 3)/4;
- struktura * cast4 = new struktura;
- cast4->od = (maxVelikostPole * 3)/4;
- cast4->doo = maxVelikostPole;
- GetSystemTimeAsFileTime (&cas_pred);
- HANDLE p1 = CreateThread(0, 0, vlakno, cast1, 0, 0);
- HANDLE p2 = CreateThread(0, 0, vlakno, cast2, 0, 0);
- HANDLE p3 = CreateThread(0, 0, vlakno, cast3, 0, 0);
- HANDLE p4 = CreateThread(0, 0, vlakno, cast4, 0, 0);
- WaitForSingleObject(p1, INFINITE);
- WaitForSingleObject(p2, INFINITE);
- WaitForSingleObject(p3, INFINITE);
- WaitForSingleObject(p4, INFINITE);
- GetSystemTimeAsFileTime (&cas_po);
- //printf( \"Cas trvani sortu: %d [ms]\\n\", kolik_ms( &cas_pred, &cas_po ) );
- merge();
- merge2();
- merge3();
- vypisPoleFloat(poleFloat,maxVelikostPole);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement