Sorting using the Collatz Conjecture

I was curious if the Collatz Conjecture¬†could be utilized to become a valid sorting algorithm, so I wrote up some notes and solved out a list of six numbers. But first, here’s the approach I used:

Image

As you might guess, this is an incredibly inefficient sorting algorithm. However, based on my notes, it seems like it has promise. I decided to implement it in python, because it’s python.

Here’s the code, with periods for whitespace:

# Array of original numbers to sort
sortMe = [14, 31, 7, 43, 3, 13]
sortedMe = []
run = True
# copy over the original values
copy = []
for i in sortMe:
copy.append(i)
while(run):
run = False
for y in xrange(len(copy)):
run = (run or (copy[y] != 1))
for x in xrange(len(copy)):
temp = copy[x]
if(temp == 1):
if(sortMe[x] != 0):
sortedMe.append(sortMe[x])
sortMe[x] = 0
else:
pass
elif(temp % 2 == 0):
temp = temp / 2
elif(temp % 2 != 0):
temp = ( 3 * temp ) + 1
copy[x] = temp
print(sortedMe)

So, I ran this, giving me the result:

[3, 13, 7, 14, 43, 31]

…Well, shucks. Turns out the Collatz Conjecture is not a valid sorting algorithm.

Thanks for Reading!
-Tsoccer93