Skip to content

Instantly share code, notes, and snippets.

@krlohnes
Created October 25, 2016 16:55
Show Gist options
  • Save krlohnes/f1fd3c8378737ecff53ef51cfd949676 to your computer and use it in GitHub Desktop.
Save krlohnes/f1fd3c8378737ecff53ef51cfd949676 to your computer and use it in GitHub Desktop.
BubbleSort in brainfuck
BF BubbleSort v0_0
=========================constraints=============================
|arrays of nonzero numbers (could easily fix this by initially |
|incrementing everything, then decrementing at end, but wont) |
|five cells at end three containing size less 1 |
|seven cells workspace at beginning |
=================================================================
+>>>>>>>workspace
++++>+++>++>+>+++++>>>++++>++++>+++ create array
<<
[
[ first loop
<<<[<]>>second index
[-<<<<<<+>+>>>>>]<<<<<[->>>>>+<<<<<]>>>> second index to 4
[-<<<<+>+>>>]<<<[->>>+<<<]<< first index to 5
[->-[>]<<]
<[>>[-]>>>>>[>]>>[>]<-<<<<<[<]] decrement no swap counter
<[->>>>>>>[-<+>]>[-<+>]<<[->>+<<]<<<<<<+>>>[-]>>] swap
>>first index
copy first to last plus 2
[-<+<+>>]<[->+<]<[[>]>[>]>+<<[<]<-]>>[-]
shift all
>[[<+>-]>]
copy moved first to last
>[<<+>>-]
>- decrement comparison counter
]
<<<<[<]>
copy first to last plus 2
[-<+<+>>]<[->+<]<[[>]>[>]>+<<[<]<-]>>[-]
shift all
>[[<+>-]>]
copy moved first to last
>[<<+>>-]
>>>[[-]<[-<+<+>>>+<]<<[->>+<<]]>
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment