So, I have some C code for a bitonic sort, and I am trying to convert that code into C#. One of the lines in the C code is confusing me. I have never worked with C before.
void merge_up(int *arr, int n) {
int step=n/2,i,j,k,temp;
while (step > 0) {
for (i=0; i < n; i+=step*2) {
for (j=i,k=0;k < step;j++,k++) {
if (arr[j] > arr[j+step]) {
// swap
temp = arr[j];
arr[j]=arr[j+step];
arr[j+step]=temp;
}
}
}
step /= 2;
}
}
void merge_down(int *arr, int n) {
int step=n/2,i,j,k,temp;
while (step > 0) {
for (i=0; i < n; i+=step*2) {
for (j=i,k=0;k < step;j++,k++) {
if (arr[j] < arr[j+step]) {
// swap
temp = arr[j];
arr[j]=arr[j+step];
arr[j+step]=temp;
}
}
}
step /= 2;
}
}
void printArray(int *arr, int n) {
int i;
printf("[%d",arr[0]);
for (i=1; i < n;i++) {
printf(",%d",arr[i]);
}
printf("]\n");
}
int main(int argc, char **argv) {
int n, *arr, i,s;
FILE *fp = fopen(argv[1],"r");
if (fp == NULL) {
fprintf(stderr,"file not found\n");
exit(1);
}
// first line gives number of numbers to be sorted
fscanf(fp,"%d",&n);
// allocate space and read all the numbers
arr = (int *)malloc(n*sizeof(int));
for (i=0; i < n; i++) {
fscanf(fp,"%d",(arr+i));
}
// print array before
printArray(arr,n);
// do merges
for (s=2; s <= n; s*=2) {
for (i=0; i < n;) {
merge_up((arr+i),s);
merge_down((arr+i+s),s); //Having trouble with this line here.
i += s*2;
}
}
printArray(arr,n);
}
What is it doing when it calls merge_down((arr+i+s), s);
Specifically, the arr+i+s. arr is the array, but what is +i+s doing? I would really appreciate some help.
-EDIT: I should add what ive done in C# for that part. This is what ive got:
//Do merges
for (int s = 2; 2 <= n; s = s * 2)
{
for(int i = 0; i < n;){
mergeUp(arr, s);
mergeDown(arr, s);
i += s * 2;
}
}