Does my code below work? Change only what does not work and leave it alone if it does.…
- User
Does my code below work? Change only what does not work and leave it alone if it does. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include #include #include <unistd.h> #include using namespace std;
unsigned int NUM_THREADS = 10; unsigned int NUM_SIZE = 1000000;
struct printStruct{ int *pstart; // pointer to the starting array element int size; }
void *bubblesort(void *ptr){ printStruct *arg = (printStruct *) ptr; int i, j; for(int i = 0; i < (elements - 1); i++) { for (int j = 0; j < (elements - 1 - i); j++) { if (sortArray[j] > sortArray[j+1]) { temp = sortArray[j]; sortArray[j] = sortArray[j+1]; sortArray[j+1] = temp; } } } return NULL; }
void merge(int* left, int* right, int leftSize, int rightSize){ int leftIndex = 0, rightIndex = 0, mergedIndex = 0; int* temp = new int[leftSize + rightSize]; while(leftIndex < leftSize && rightIndex < rightSize){ if (left[leftIndex] < right[rightIndex]) { temp[mergedIndex++] = left[leftIndex++]; } else { temp[mergedIndex++] = right[rightIndex++]; } } while(leftIndex < leftSize){ temp[mergedIndex++] = left[leftIndex++]; } while(rightIndex < rightSize){ temp[mergedIndex++] = right[rightIndex++] } for (int i = 0; i < mergedIndex; i++) { left[i] = temp[i]; } delete[] temp; }
int main(int argc, char*argv[]) { ifstream numFile(argv[1]); int elements = 0; int sortArray[NUM_SIZE]; int size; int temp;
while (numFile >> size) { sortArray[elements] = size; elements++; } numFile.close(); printStruct s1; s1.pstart = sortArray; s1.size = 100000; printStruct s2; s2.pstart = sortArray + 100000; s2.size = 100000; printStruct s3; s3.pstart = sortArray + 200000; s3.size = 100000; printStruct s4; s4.pstart = sortArray + 300000; s4.size = 100000; printStruct s5; s5.pstart = sortArray + 400000; s5.size = 100000; printStruct s6; s6.pstart = sortArray + 500000; s6.size = 100000; printStruct s7; s7.pstart = sortArray + 600000; s7.size = 100000; printStruct s8; s8.pstart = sortArray + 700000; s8.size = 100000; printStruct s9; s9.pstart = sortArray + 800000; s9.size = 100000; printStruct s10; s10.pstart = sortArray + 900000; s10.size = 100000; pthread_t thread0, thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8, thread9; int iret0, iret1, iret2, iret3, iret4, iret5, iret6, iret7, iret8, iret9; iret0 = pthread_create( &thread0, NULL, bubblesort, (void*) &s1); iret1 = pthread_create( &thread1, NULL, bubblesort, (void*) &s2); iret2 = pthread_create( &thread2, NULL, bubblesort, (void*) &s3); iret3 = pthread_create( &thread3, NULL, bubblesort, (void*) &s4); iret4 = pthread_create( &thread0, NULL, bubblesort, (void*) &s5); iret5 = pthread_create( &thread1, NULL, bubblesort, (void*) &s6); iret6 = pthread_create( &thread2, NULL, bubblesort, (void*) &s7); iret7 = pthread_create( &thread3, NULL, bubblesort, (void*) &s8); iret8 = pthread_create( &thread0, NULL, bubblesort, (void*) &s9); iret9 = pthread_create( &thread1, NULL, bubblesort, (void*) &s10); pthread_join( thread0, NULL); pthread_join( thread1, NULL); pthread_join( thread2, NULL); pthread_join( thread3, NULL); pthread_join( thread4, NULL); pthread_join( thread5, NULL); pthread_join( thread6, NULL); pthread_join( thread7, NULL); pthread_join( thread8, NULL); pthread_join( thread9, NULL); ofstream outFile(argv[2]); for(int i = 0; i < elements; i++) { outFile << sortArray[i] << endl; } outFile.close(); return 0; - Assistant