[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

What is the slowest algorithm you can think of that swaps two

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 27
Thread images: 2

File: 0.png (289KB, 500x300px) Image search: [Google]
0.png
289KB, 500x300px
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
})()
>>
File: 1ZA9P0S.jpg (58KB, 600x825px) Image search: [Google]
1ZA9P0S.jpg
58KB, 600x825px
>>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;
>>
>>61438046
a <- 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 was

 a = 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.
>>
>>61437665
void 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
Thread posts: 27
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.