06-27-2003, 01:55 PM
Basic is kinda hopeless for this task for all but the very simplest algorithms because even a loop construct requires more than one line:
Futhermore you cant make assignments within the condition part of a loop because Basic interprets this as a test for equality.
So, seeing as your rules didn't actually specify that I have to write Basic code, heres a pseudo bubble sort in one line of Java or C++ code (Wont work in C because you arent allow to declare variables within the expression list of a for loop:
Sorts the array a in descending order. It differs from most bubble sort implementations becuase it only uses a single loop (by resetting i every time it does a swap). On the plus side, in the best case (data is already sorted) the algorithm works in linear O(n) time, however in the worst case it runs in O(n ^ 2).
Code:
while condition : wend
So, seeing as your rules didn't actually specify that I have to write Basic code, heres a pseudo bubble sort in one line of Java or C++ code (Wont work in C because you arent allow to declare variables within the expression list of a for loop:
Code:
for(int a[5]={2,4,3,5,1},int t,int i=0;i<4;i++){if(a[i]<a[i+1]){(t=a[i])&&(a[i]=a[i+1])&&(a[i+1]=t)&&(i=-1);}}
esus saves.... Passes to Moses, shoots, he scores!