c++ - Iterative mergesort from index a to index b -
i'm trying sort of mergesort going it'll mergesort(int[] a, int start, int stop)
start index start sorting , stop index stop sorting. idea can have 4 different threads sorting 4 subsections of array @ same time. can call mergesort(a, 0, length/4); mergesort(a, length/4, length/2); mergesort(a, length/2, (3/4)*length); etc.
on different threads.
i've come across bunch of iterative algorithms go 0
length
when edit bit try them work subsections of array segfaults on inputs large , bunch of silent errors when looking in valgrind.
here's have right now:
void mergesort(int *a, int *b, int low, int high) { int pivot; if (low < high) { pivot = (low + high)/2; if(level < maxlevel) { // if have not reached max threads spawn mergesort(a, b, low, pivot); // need change these spawn threads mergesort(a, b, pivot + 1, high); merge(a, b, low, pivot, high); } else { level += 2; // tell when can stop spawning threads (int curr_size=1; curr_size<=(high-low)-1; curr_size = 2*curr_size) { // pick starting point of different subarrays of current size (int left_start=low; left_start<high; left_start += 2*curr_size) { // find ending point of left subarray. mid+1 starting // point of right int mid = left_start + curr_size - 1; int right_end = min(left_start + 2*curr_size - 1, high); // merge subarrays arr[left_start...mid] & arr[mid+1...right_end] merge(a, b, left_start, mid, right_end); } } } } }
the output sorted array if doesn't segfault. valgrind there 1 instance of "invalid write of size 4" error: https://puu.sh/s7zb2.png
here's merge function i'm using:
void merge(int *a, int *b, int low, int pivot, int high) { cout << "merging " << low << " " << high << endl; int h, i, j, k; h = low; = low; j = pivot + 1; while ((h <= pivot) && (j <= high)) { if (a[h] <= a[j]) { b[i] = a[h]; // store element of left half in temporary array h++; // shift index of array element copied temporary } else { b[i] = a[j]; j++; // shift index of array element copied temporary } i++; } if (h > pivot) { (k = j; k <= high; k++) { b[i] = a[k]; i++; } } else { (k = h; k <= pivot; k++) { b[i] = a[k]; i++; } } (k = low; k <= high; k++) a[k] = b[k]; // recopy values temporary original array. }
what's going wrong? have feeling it's cause of merge function trying access value it's not supposed to, can't seem locate happening.
thanks
Comments
Post a Comment