S
sj
Hi;
I am trying to find the position of the maximum element of and array using
divide-and-conquer method. Following is the code I am trying.
#include <stdio.h>
#define MAX 7
int msort(char list[], int n)
{
int i, half1, half2;
char arr1[MAX/2+1];
char arr2[MAX/2+1];
if(n ==1) return 0;
else if (n==2){
if(list[0]>list[1]) return 0;
else return 1;
}else
{
half1 = n / 2;
half2 = n - half1;
for(i = 0; i < half1; i++)
arr1 = list;
for(i = 0; i < half2; i++)
arr2 = list[half1 + i];
int x1 = msort(arr1, half1);
int x2 = msort(arr2, half2);
if( list[x1] > list[x2]) return x1;
else
return x2;
}
}
int main()
{
int i, n;
char array[MAX];
n = 7;
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
array[3] = 'D';
array[4] = 'E';
array[5] = 'F';
array[6] = 'X';
int x = msort(array, n);
printf("%d ", x );
printf("\n");
return 0;
}
what am i doing wrong here?
thanks for any help
sj
I am trying to find the position of the maximum element of and array using
divide-and-conquer method. Following is the code I am trying.
#include <stdio.h>
#define MAX 7
int msort(char list[], int n)
{
int i, half1, half2;
char arr1[MAX/2+1];
char arr2[MAX/2+1];
if(n ==1) return 0;
else if (n==2){
if(list[0]>list[1]) return 0;
else return 1;
}else
{
half1 = n / 2;
half2 = n - half1;
for(i = 0; i < half1; i++)
arr1 = list;
for(i = 0; i < half2; i++)
arr2 = list[half1 + i];
int x1 = msort(arr1, half1);
int x2 = msort(arr2, half2);
if( list[x1] > list[x2]) return x1;
else
return x2;
}
}
int main()
{
int i, n;
char array[MAX];
n = 7;
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
array[3] = 'D';
array[4] = 'E';
array[5] = 'F';
array[6] = 'X';
int x = msort(array, n);
printf("%d ", x );
printf("\n");
return 0;
}
what am i doing wrong here?
thanks for any help
sj