06-25-2009 02:42 PM
Hi, everyone, I want to use the formula node to sort one array with small to large value order, e.g 16,2,1,5,3, then the result should be 1,2,3,5,6.
then my program is like this, but I cannot get the corret answer. Please help me. Thank you very much.
Solved! Go to Solution.
06-25-2009 02:56 PM
I hate to break it to you, but you are re-inventing the wheel.
Use the Sort function in the Array palette to achieve what you are trying to do.
06-25-2009 03:01 PM
Hi, the function you shown up is what I know before.
Now I am just trying to practise the formula node, but it can not work, could you help me to work on the formula node? Thank you very much for your reply....
06-25-2009 03:03 PM
06-25-2009 03:17 PM - edited 06-25-2009 03:19 PM
It is so kind of you to give me the help....:womanvery-happy:
By the way, I checked the code in Dev-CPP softwar, it works, but here is strange.
06-25-2009 03:21 PM
06-25-2009 03:30 PM
06-25-2009 03:31 PM
McArthur,
You are probably getting an array of zeros because you are iterating the inner loop one to many times. Change line 9 to "for(j=0;j<(sizeArr-1);j++)". This should give you correct numbers.
Having said that, this algorithm is very inefficient (O(n^2)). I would suggest simply using the native "Sort 1D Array" primitive.
Chris M
06-25-2009 03:43 PM
Ha, Thank you very much. you solve my stupid error...
But still strange that I can compile and run them in the Dev-CPP.
like:
#include <stdio.h>
#include <stdlib.h>
#define sizeArr 5
int main(int argc, char *argv[])
{
int inArr[sizeArr]={6,1,4,5,0};
int *ii=inArr;
int i=0;
int j=0;
int temp=0;
for (i=0;i<sizeArr;i++)
{
for(j=0;j<sizeArr;j++)
{
if (inArr[j]>=inArr[j+1])
{
temp=inArr[j+1];
inArr[j+1]=inArr[j];
inArr[j]=temp;
}
}
}
printf("Hello\n");
for(i=0;i<sizeArr;i++)
{
printf("%d\n",inArr[i]);
}
system("PAUSE");
return 0;
}
What I got is: