|
main.c: single-threaded merge sort merge-sort.c: merge sort algorithm file + some support functions merge-sort.h: header file for merge-sort.c main-t.c: skeleton file for the multi-threaded merge sort project Makefile: make file for main and main-t programs |
|
|
|
The gist of the code in main.c is:
#define MAX 50000000 // Max array size int N; // Actual array size int x[MAX]; // input data int t[MAX]; // help array to perform merge int main(int argc, char *argv[]) { int i; N = some value; /* -------------------------------------- Generate random numbers to be sorted -------------------------------------- */ for (i = 0; i < N; i++) x[i] = random()%1000; mergeSort(x, 0, N, t ); // Sort the entire array } |
|
|
Note:
|
|
Run it using:
main N |
Example:
main 80 Array before sorting: 383 886 777 915 793 335 386 492 649 421 362 27 690 59 763 926 540 426 172 736 211 368 567 429 782 530 862 123 67 135 929 802 22 58 69 167 393 456 11 42 229 373 421 919 784 537 198 324 315 370 413 526 91 980 956 873 862 170 996 281 305 925 84 327 336 505 846 729 313 857 124 895 582 545 814 367 434 364 43 750 Array AFTER sorting: 11 22 27 42 43 58 59 67 69 84 91 123 124 135 167 170 172 198 211 229 281 305 313 315 324 327 335 336 362 364 367 368 370 373 383 386 393 413 421 421 426 429 434 456 492 505 526 530 537 540 545 567 582 649 690 729 736 750 763 777 782 784 793 802 814 846 857 862 862 873 886 895 915 919 925 926 929 956 980 996 Good job: Array was sorted correctly ! **** Elapsed time = 20 microseconds |
If array must be sorted when the program is done
The only difference between main and your main-t program will be the elapsed time:
|
main-t 10000000 2 (should finish in around 1322288 microseconds) main-t 10000000 4 (should finish in around 968839 microseconds) main-t 10000000 8 (should finish in around 744422 microseconds) main-t 10000000 16 (should finish in around 690425 microseconds) |
|