Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Algorithms having only one line of code.
#26
Basic is kinda hopeless for this task for all but the very simplest algorithms because even a loop construct requires more than one line:
Code:
while condition : wend
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:
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);}}
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).
esus saves.... Passes to Moses, shoots, he scores!
Reply


Messages In This Thread
To Oracle: 2nd look at your code. - by Moneo - 06-24-2003, 09:07 PM
To Seph and Agamemnus: - by Moneo - 06-24-2003, 09:17 PM
factorial. - by Agamemnus - 06-24-2003, 09:51 PM
To Agamemnus - by Moneo - 06-24-2003, 10:21 PM
To na_th_an: - by Moneo - 06-27-2003, 03:15 AM
Challenge: Algorithms having only one line of code. - by LooseCaboose - 06-27-2003, 01:55 PM
ok, - by Agamemnus - 06-27-2003, 05:17 PM
To Oracle re Statlib - by Moneo - 06-27-2003, 10:39 PM
Re: To na_th_an: - by na_th_an - 06-28-2003, 01:14 AM
To LooseCaboose: - by Moneo - 06-28-2003, 03:08 AM
Re: To Oracle re Statlib - by oracle - 06-28-2003, 05:50 AM
To Oracle: - by Moneo - 06-29-2003, 03:10 AM
To Ak00ma: - by Moneo - 06-30-2003, 03:39 AM
Power of 2 - by Moneo - 07-02-2003, 02:42 AM
To Antoni: - by Moneo - 07-02-2003, 03:03 AM
197 - by whitetiger0990 - 07-02-2003, 06:29 PM
To na_th_an re "replacement" logic: - by Moneo - 07-02-2003, 08:29 PM
Re ROUNDING using .5: - by Moneo - 07-02-2003, 08:47 PM
Re: Re ROUNDING using .5: - by Moneo - 03-20-2006, 06:19 AM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:09 PM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:24 PM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:52 PM
my one-liner - by neuro - 03-30-2006, 12:08 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)