Share your repls and programming experiences

← Back to all posts
The WORST sorting algorithm possible-In just 1 line! (random sort)
h
rafrafraf (1775)

This is my sorting algorithm came up with called random sort - the absolute worst sorting algorithm I could make

the art of python one liners
Python's PEP8 style guide suggests that you should limit all lines to a maximum of 79 characters... 529 isnt too far off

As the name suggests, the algorithm randomly arranges the list until it finally ends up in order

The biggest challenge was doing this all in one line..

  • That's because I had to firstly created a randomly sorted list
  • Then made a check to see if its in order as an extra challenge instead of using the built-in "sorted" function and comparing it
  • But in the end it came together pretty nicely

Roughly how i went about making it inline:

  • the walrus operator (introduced in python 3.8) was a life saver - it allows you to declare variable in if statements (and while loops)
  • lambda
  • inline ifs and fors :)

..AND there is no semi-colons, so this is truly a one liner

I think we all know that python was created to be used illegibly... duhhh

Important notes:

  • if you enter more than 4 keys this program will likely have a recursion error because of how inefficient random sort is
  • enter the values with commas and no brackets[]

Let me know what i should make in 1 line next

Comments
hotnewtop
JBloves27 (1902)

you have to add numbers and without brackets. like this: @ch1ck3n

rafrafraf (1775)

@ch1ck3n thats a recursion error, because of how inefficient random sort is when you enter over 4 keys its likely to have that error, its simply a python thing when a function calls itself too many times it errors :/

ch1ck3n (2866)

@rafrafraf Oh man, in this program, the errors are long as hec

rafrafraf (1775)

@ch1ck3n lmao its a recursion depth error

[deleted]

@rafrafraf yes it can have spaces, i tried it out myself

rafrafraf (1775)

@DevZeth thanks for letting me know apparently int() removes spaces from strings if theyre just numbers and spaces

LegendaryWolf (572)

That's what a mod would do if he/she viewed this post. And is @CodingCactus a male/boy?

LegendaryWolf (572)

wut? Im sayin that's what a mod wud comment if he/she viewed this post

Pankak (49)

This is my sorting algorithm came up with called random sort - the absolute worst sorting algorithm I could make

bogo sort

rafrafraf (1775)

@Pankak i wasnt aware random sort was already named something else :/

Pankak (49)

@rafrafraf at least now you can say you created a sorting algorithm in one line of python code on your resume

rafrafraf (1775)

@Pankak ah yes i will definitely include my inline bogo sort algorithm in my resume, because as we all know, python was made to be used illegibly ahahaha

HahaYes (1868)

hey this looks like my code on a daily basis

1CornBoy (1)

For me, hard limit is 6, any higher and it does not work

zplusfour (912)

lmao good job

algorithm sorting in one line.. who knows?

zplusfour (912)

@rafrafraf np, high-effort programmers go brrrr lol :)

TheCheeseCoder (66)

@rafrafraf

make a 1-line console game. Lets see how long it will be

WILLIAMBAEWER (116)

ooooooooo aaaaaaaaaa

MarcusWeinberger (805)

bruh this is a very cool one-liner damnn even with the walrus operator too

hg0428 (196)


I entered 2 items

CONNERSTANEK (3)

Ummmm it did not work

rafrafraf (1775)

@CONNERSTANEK if you enter more than 3-4 keys this program will likely have a recursion error because of how inefficient random sort is
EDIT: also enter the keys with only commas no spaces

CONNERSTANEK (3)

@rafrafraf how many keys should i enter.

CONNERSTANEK (3)

@rafrafraf I tried 1, 2, 3, and 4 it did not work

rafrafraf (1775)

@CONNERSTANEK did you put spaces after the comma?

DominicHuieWhit (52)

it just added brackets, i tried it multiple times

rafrafraf (1775)

@DominicHuieWhit if you enter a list unsorted it will output that list in ascending order