Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <iostream>
- #include <sys/time.h>
- #include <sys/param.h>
- #include <pthread.h>
- #include <math.h>
- #define TYPE float
- using namespace std;
- const int count_Thread = 2;
- const int my_length = 10;
- struct thread_argument
- {
- int id;
- int from, to;
- TYPE *data;
- };
- float randomFloat(float minR,float maxR){
- return (minR + 1) + (((float) rand()) / (float) RAND_MAX) * (maxR - (minR + 1));
- }
- int randomInt(int minR, int maxR){
- return rand()%(maxR-minR + 1) + minR;
- }
- void BubbleSort_dsc(TYPE *arr, int start, int end){
- TYPE tmp;
- for(int i = start; i < end; i++)
- {
- for(int j = start; j < end - 1; j++)
- {
- if(arr[j+1] > arr[j])
- {
- tmp = arr[j + 1];
- arr[j + 1] = arr[j];
- arr[j] = tmp;
- }
- }
- }
- }
- void BubbleSort_asc(TYPE *arr, int start, int end){
- TYPE tmp;
- for(int i = start; i < end; i++)
- {
- for(int j = start; j < end - 1; j++)
- {
- if(arr[j+1] < arr[j])
- {
- tmp = arr[j + 1];
- arr[j + 1] = arr[j];
- arr[j] = tmp;
- }
- }
- }
- }
- void fill_array(TYPE *arr, int my_length, int minR, int maxR)
- { TYPE temp;
- for (int i = 0; i < my_length; i++)
- {
- temp = randomFloat(minR,maxR);
- arr[i] = roundf(temp * 100) / 100.0;
- //arr[i] = randomInt(minR, maxR);
- }
- }
- void *thread_sorting( void *void_arg )
- {
- thread_argument *ptr_data = (thread_argument*) void_arg;
- BubbleSort_asc(ptr_data->data, ptr_data->from, ptr_data->to);
- return NULL;
- }
- int main()
- { srand( ( int ) time( NULL ) );
- TYPE *my_array = new TYPE [my_length];
- pthread_t pt[count_Thread];
- thread_argument ta[count_Thread];
- int minR = -100.00;
- int maxR = 100.00;
- for (int i = 0; i < count_Thread; i++)
- {
- ta[i].id = i;
- ta[i].from = i * (my_length/count_Thread);
- ta[i].to = ta[i].from + (my_length/count_Thread);
- ta[i].data = my_array;
- }
- fill_array(my_array, my_length, minR, maxR);
- for (int i = 0; i < count_Thread; i++)
- {
- pthread_create( &pt[i], NULL, thread_sorting, &ta[i] );
- }
- for (int i = 0; i < count_Thread; i++)
- {
- pthread_join(pt[i], NULL);
- }
- for (int i = 0; i < my_length; i++)
- {
- cout << my_array[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement