![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 2
| Mergesort in C For an assignment I have to sort a large amount of numbers with multithreading in C. I am new to C however the part with the threading is going fine. There is some problem with my MergeSort algorithm though, and after staring at it for hours I still can't figure out what the problem is. Maybe someone with a fresh mind..? ![]() Mergesort repeatedly devises an array into two parts, a left and a right part. These parts are sorted recursively and then combined into the resulting array. (the array is split until the length of one part equals 1, because then left=right and it is trivially sorted) Code: void mergesort(int array_to_sort[], int left, int right)
{
int splitpos;
if (left < right)
{
// Partition the array into two subarrays, sort these subarrays recursively and then merge them into
// a correctly sorted array.
splitpos = (left + right) / 2;
mergesort(array_to_sort, left, splitpos);
mergesort(array_to_sort, splitpos + 1, right);
merge(array_to_sort, left, splitpos, right);
}
}
Code: void merge(int array_to_sort[], int left, int splitpos, int right)
{
int size_array1 = splitpos - left + 1;
int size_array2 = right - splitpos;
int array1[size_array1];
int array2[size_array2];
int i = 0;
int j = 0;
// Copy both sub-arrays to auxilary arrays.
for (i = 0; i < size_array1; i++)
{
array1[i] = array_to_sort[left + i];
}
for (j = 0; j < size_array2; j++)
{
array2[j] = array_to_sort[splitpos + j + 1];
}
i = 0;
j = 0;
int k;
for (k = left; k <= right; k ++)
{
if ( j = size_array2 )
{
array_to_sort[k] = array1[i];
i++;
}
else if ( i = size_array1 )
{
array_to_sort[k] = array2[j];
j++;
}
else if (array1[i] <= array2[j])
{
array_to_sort[k] = array1[i];
i++;
}
else
{
array_to_sort[k] = array2[j];
j++;
}
}
}
|
| Oduig is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| One probable problem is that you are using assignment where you may want equality comparison, e.g., you use if ( j = size_array2 ) instead of if ( j == size_array2 ).
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Sep 2008
Posts: 2
| Thank you. i'm used to pascal, and when you stare at a screen for 5 hours you don't see stuff like that |
| Oduig is offline | |
![]() |
| Tags |
| merge, mergesort, problem, sort |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I do MergeSort versus QuickSort instead? | dxfist | C++ Programming | 9 | 03-06-2008 12:12 PM |
| Natural Mergesort | wuzzo87 | C Programming | 31 | 04-14-2007 09:41 PM |
| Mergesort | swanley007 | C++ Programming | 6 | 10-26-2005 11:45 PM |
| Linked Lists + MergeSort....(how?) | JoshR | C++ Programming | 4 | 06-03-2005 02:40 PM |
| MergeSort implementation with linklist. | Kam | C Programming | 3 | 10-21-2002 11:04 AM |