By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

C implementation of merge sort

P: 2
Hi all,

I've just copy a part of C source code for my C applet. My program is about merge sort and array implementation. When I run, I find that it has a lot of errors. Even I try my best to fix the errors, It does not run. Please help me to solve my problem.

I will post my source code here:

#include<stdio.h>
#include<stdlib.h>
#define MAXARRAY 10;
void mergesort(int a[], int low, int high);
int main(void)
{
int array[MAXARRAY];
int i = 0;
/*Load some random into array*/
for (i = 0; i < MAXARRAY; i++)
array[MAXARRAY] = rand() % 100;
/*array before mergesort*/
printf("Before: ");
for (i = 0; i < MAXARRAY; i++)
print("%d", array[i]);
printf("\n");
mergesort (array, 0, MAXARRAY - 1);
/* array after mergesort*/
printf("mergesort: ");
for (i = 0; i < MAXARRAY; i++)
printf("%d" , array[i]);
printf("\n")
return 0;
}
void mergesort(int a[], int low, int high)
{
int i = 0;
int length = high - low + 1;
int pivot = 0;
int merge1 = 0;
int merge2 = 0;
int working [length];
if (low = high)
return;
pivot = (high + low)/2;
mergesort(a, low, pivot);
mergesort(a, pivot+1, high);
for (i = 0;i < length; i ++)
working[i] = [low + i];
merge1 = 0;
merge2 = pivot - low +1;
for (i = 0; i < lenght; i++)
if (merge2 <= high - low)
if (merge1 <= pivot - low)
if (working[merge1] > working[merge2])
a[i+low] = working[merge2++];
else
a[i+low] = working[merge1++];
else
a[i+low] = working[merge2++];
else
a[i+low] = working[merge1++];
}
}
Dec 10 '07 #1
Share this Question
Share on Google+
3 Replies


gpraghuram
Expert 100+
P: 1,275
Hi all,

I've just copy a part of C source code for my C applet. My program is about merge sort and array implementation. When I run, I find that it has a lot of errors. Even I try my best to fix the errors, It does not run. Please help me to solve my problem.

I will post my source code here:

#include<stdio.h>
#include<stdlib.h>
#define MAXARRAY 10;
void mergesort(int a[], int low, int high);
int main(void)
{
int array[MAXARRAY];
int i = 0;
/*Load some random into array*/
for (i = 0; i < MAXARRAY; i++)
array[MAXARRAY] = rand() % 100;
/*array before mergesort*/
printf("Before: ");
for (i = 0; i < MAXARRAY; i++)
print("%d", array[i]);
printf("\n");
mergesort (array, 0, MAXARRAY - 1);
/* array after mergesort*/
printf("mergesort: ");
for (i = 0; i < MAXARRAY; i++)
printf("%d" , array[i]);
printf("\n")
return 0;
}
void mergesort(int a[], int low, int high)
{
int i = 0;
int length = high - low + 1;
int pivot = 0;
int merge1 = 0;
int merge2 = 0;
int working [length];
if (low = high)
return;
pivot = (high + low)/2;
mergesort(a, low, pivot);
mergesort(a, pivot+1, high);
for (i = 0;i < length; i ++)
working[i] = [low + i];
merge1 = 0;
merge2 = pivot - low +1;
for (i = 0; i < lenght; i++)
if (merge2 <= high - low)
if (merge1 <= pivot - low)
if (working[merge1] > working[merge2])
a[i+low] = working[merge2++];
else
a[i+low] = working[merge1++];
else
a[i+low] = working[merge2++];
else
a[i+low] = working[merge1++];
}
}

I havent checked the logic but there is an error in the main
Expand|Select|Wrap|Line Numbers
  1. int array[MAXARRAY];
  2. int i = 0; 
  3. /*Load some random into array*/
  4.  for (i = 0; i < MAXARRAY; i++)
  5. //      array[MAXARRAY] = rand() % 100;
  6. //it shuld be
  7.      array[i] = rand() % 100;
  8.   /*array before mergesort*/
  9.   printf("Before: ");
  10.  
Make this change and try and in mean time i can check the logic.
Note:
Please use code tags while posting your code

Thanks
Raghuram
Dec 10 '07 #2

P: 2
I havent checked the logic but there is an error in the main
Expand|Select|Wrap|Line Numbers
  1. int array[MAXARRAY];
  2. int i = 0; 
  3. /*Load some random into array*/
  4.  for (i = 0; i < MAXARRAY; i++)
  5. //      array[MAXARRAY] = rand() % 100;
  6. //it shuld be
  7.      array[i] = rand() % 100;
  8.   /*array before mergesort*/
  9.   printf("Before: ");
  10.  
Make this change and try and in mean time i can check the logic.
Note:
Please use code tags while posting your code

Thanks
Raghuram
Thanks Mr Raghuram!
I tried my best to fix my problems.The program dont notice about errors any more. However, it can input the array and do merge sort as I expected. Who can help me, please?
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void mergesort(int a[], int low, int high);
  4. int main(void)
  5. {
  6.     int array[50];
  7.         int i; 
  8. /*Load some random into array
  9.  for (i = 0; i < 50; i++)
  10.       array[i] = rand() % 50;
  11.     array before mergesort
  12.   printf("Before: ");
  13.   for (i = 0; i < 50; i++)
  14.   printf("%d", array[i]);*/
  15.   //printf("\n");
  16.  for (i = 0; i < 50; i++)
  17.       {
  18.   printf("Input array[%d] here!", i);
  19.   scanf("%d", array[i]);
  20.   }
  21.   /*printf("the array is:\n");*/
  22.   for (i = 0; i < 50; i++)
  23.       {
  24.   printf("%d  ", array[i]);
  25.   //printf("\n");
  26.   }
  27.   mergesort (array, 0, 50 - 1);
  28.   /* array after mergesort*/
  29.   printf("mergesort: ");
  30.   for (i = 0; i < 50; i++)
  31.   printf(" %d" , array[i]);
  32.   printf("\n");
  33.   return 0;
  34.   }
  35.   void mergesort(int a[], int low, int high)
  36.   {
  37.       int i;
  38.       int length = high - low + 1;
  39.       int pivot = 0;
  40.       int merge1 = 0;
  41.       int merge2 = 0;
  42.       int working[length];
  43.       if (low = high)
  44.       return; 
  45.       pivot = (high + low)/2;
  46.       mergesort(a, low, pivot);
  47.       mergesort(a, pivot+1, high);
  48.     for (i = 0; i < length; i ++)
  49.     working[length] =working [low + i];
  50.     merge1 = 0;
  51.     merge2 = pivot - low + 1;
  52.     for (i = 0; i < length; i++)
  53.     if (merge2 <= high - low)
  54.     if (merge1 <= pivot - low)
  55.     if (working[merge1] > working[merge2])
  56.        a[i+low] = working[merge2++];
  57.        else
  58.        a[i+low] = working[merge1++];
  59.        else
  60.        a[i+low] = working[merge2++];
  61.        else
  62.        a[i+low] = working[merge1++];
  63.   }
Dec 10 '07 #3

gpraghuram
Expert 100+
P: 1,275
Hi,
Make this change
Expand|Select|Wrap|Line Numbers
  1.   for (i = 0; i < 50; i++)
  2.   {
  3.      printf("Input array[%d] here!", i);
  4.      scanf("%d", &array[i]);
  5.   }
  6.  
Then it will work fine.
But the logic of merge sort still has issues.....

Check it

Raghuram
Dec 12 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.