![]() |
threaded merge sort help
I am working on a merge sort of two files of integers, and am fuzzy on some of the logic\syntax. I need two threads, each of which will open a file, read its contents into an array, and then sort the array using qsort. One thread will operate on file f1.dat(10000 numbers) and leave its sorted contents in array x, while the other will use file f2.dat(11999 numbers) and array y. When the threads have finished, I need to merge x and y into array z so that z is also sorted. Also, I am wanting to use one function for both threads, passing the array, its size, and the name of the file in a structure.
Currently, I have a struct set up with the arrays, their sizes, and the name of the two files, a comparison function for qsort, a sort function for the contents of the files, a merge sort for the z array, and of course the creation of the threads. I think the biggest area I have questions about is how to properly pass a structure to a generalized sort function and then sort data using that structure parameter. So far: :
#include <stdio.h> |
Hmm. You call qsort(). So do I take this to mean you want to have two threads each quicksort half of the array, then merge the two together? I'm doubtful of the point in this unless you have multiple processors and your system will arrange for each thread to run on one of them.
Well, I think the way to do this would be to load half of the file into one array and half into another, then have your threads each take one and qsort() it, then wait for the threads to end (with pthread_join() if my memory serves me intermittently, but don't trust me on this). Then the main program could do the merge into one big array. I hope this answer belongs more to the solution set than the problem set. :) |
| All times are GMT -5. The time now is 4:23 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC