Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Zadanie u Olivky
- 1.)Vytv. pole floatov do 20 prvkov, v intervale <-100.00, 100.00>
- pomocou vlakien
- predavat parametry do vlakna
- 2.)Triedit pomocou dvoch vlakien 1/2 pola, insertion sort
- +merat cas
- tisk pola
- 3.)Otocit poradie prvkov v poli (v kazdej polke) a znovu zotriedit
- +merat cas
- tisk pola
- 4.)Prvky z 3.) spojit do jedneho pola do zoradeneho pola
- tisk pola
- 5.)Znova bod 1.) a skusime triedit cele pole
- 100 000 prvkov v poli
- +merat cas
- tisk
- #include <iostream>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- const int N = 14;
- signed int P[N];
- int R[2];
- int R2[2];
- int T[N];
- void insertSort(int pole[], int size[2])
- {
- for (int i=size[0]; i<size[1]-1; i++)
- {
- int j = i+1;
- int a = pole[j];
- while (j>size[0] && a<pole[j-1])
- {
- pole[j] = pole[j-1];
- j--;
- }
- pole[j] = a;
- }
- for(int k=size[0]; k<size[1]; k++)
- {
- cout << P[k] << " ";
- }
- cout<<endl;
- }
- 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;
- // konverze 100ns -> 1ms
- return ( int ) ( ( po64b - pred64b ) / 10000 );
- }
- DWORD WINAPI vlakna( LPVOID c)
- {
- int * d = ( int * ) c;
- cout <<d[0]<<" "<<d[1] <<endl ;
- insertSort(P,d);
- return 0;
- }
- DWORD WINAPI vlakna1( LPVOID )
- {
- int k=0,i=0,j=N/2;
- cout<< "spojeni poli";
- while(k<=N-1)
- {
- if((i<N/2)&& P[i]<=P[j])
- {
- T[k]=P[i];
- i++;
- k++;
- }
- else if(j<N)
- {
- T[k]=P[j];
- j++;
- k++;
- }
- }
- for(int k=0; k<N; k++)
- {
- cout << T[k] << " ";
- }
- cout<<endl;
- return 0;
- }
- void main()
- {
- srand( ( int ) time( NULL ) );
- for (int i=0; i<N; i++)
- {
- P[i] = rand() % 70 - 30;
- }
- cout << "Aplikovani tridiciho algoritmu."<< endl;
- /*for (int i=0; i<N; i++)
- {
- cout << P[i] << endl;
- }*/
- int R[2]={0,N/2};
- FILETIME cas_pred, cas_po;
- GetSystemTimeAsFileTime( &cas_pred );
- HANDLE vlakno = CreateThread( 0, 0,vlakna,R, 0, 0 );
- R2[0]=N/2;
- R2[1]=N;
- HANDLE vlakno2 = CreateThread( 0, 0,vlakna,R2, 0, 0 );
- WaitForSingleObject( vlakno, INFINITE );
- WaitForSingleObject( vlakno2, INFINITE );
- GetSystemTimeAsFileTime( &cas_po );
- cout << "Pole bylo setrideno"<< endl;
- cout << "Setrideni trvalo: " << kolik_ms( &cas_pred, &cas_po )<<"ms" << endl;
- for(int i=0;i<=N-1-i;i++)
- {
- int b = P[i];
- P[i] = P[N-1-i];
- P[N-1-i] = b;
- }
- cout << "Prevracene pole."<< endl;
- GetSystemTimeAsFileTime( &cas_pred );
- vlakno = CreateThread( 0, 0,vlakna,R, 0, 0 );
- vlakno2 = CreateThread( 0, 0,vlakna,R2, 0, 0 );
- WaitForSingleObject( vlakno, INFINITE );
- WaitForSingleObject( vlakno2, INFINITE );
- GetSystemTimeAsFileTime( &cas_po );
- cout << "Pole bylo setrideno"<< endl;
- cout << "Setrideni trvalo: " << kolik_ms( &cas_pred, &cas_po )<<"ms" << endl;
- int T[N];
- vlakno = CreateThread( 0, 0,vlakna1,R, 0, 0 );
- WaitForSingleObject( vlakno, INFINITE );
- cout <<"Pole spojeno."<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement