Sort a list without builtin functions

-4

Sorting a list would make a pretty fun code-golf challenge, but all the builtins make it boring! Its no fun when you spend all of your time golfing a function and then you see that someone else has a shorter answer just using a builtin!

The best way to solve this problem is to ban builtins, but no challenge has gone quite far enough! Most challenges only ban one or two builtins. We are going to ban them all!

Ok lets stop being sardonic for a moment.

Your task will be to write a function that takes a list of integers and returns a sorted version of the same list. You are to do this without the use of builtins

What are builtins?

To keep ourselves from having to navigate edge case hell we are going to limit answers to Haskell (sorry if Haskell is not your favorite language). We are going to define a builtin as any named function that is not defined in your code. We are going to say that symbolic operators, (+), (:), etc. are not builtin functions. This applies both to prelude and any libraries that are imported.

What does it mean to use?

Before anyone tries to get smart with me, using a function is using a function. Whether or not the function is named in your source code is irrelevant (this is not ), if you make calls to the function without naming it that is also considered cheating. That being said calls to a function are not the only form of use, you shouldn't name a builtin function in your code even if you don't call it (I don't know why you would want to do that but I'm not taking any risks).

Scoring

This is so you should aim to do this in as few bytes as possible. However since different algorithms are not going to be competitive, (merge sort answers are probably going to be longer than insertion sort answers) I would like this to be considered a competition among individual algorithms, just as normal code golf is a competition among individual languages. For that reason I would like you to name your algorithm used in your header instead of the language. If you don't know the name feel free to put Unknown.

Post Rock Garf Hunter

Posted 2017-07-08T14:24:03.400

Reputation: 55 382

1Related (sort list without sort builtin) – Okx – 2017-07-08T14:35:22.557

Well that did not go well so I gave a pity upvote – Christopher – 2017-07-08T14:46:29.950

1I would have upvoted this if the challenge was not limited to a specific language. – user41805 – 2017-07-08T15:14:14.400

@KritixiLithos I would have like to made it open to all languages, however the definition of builtin would have needed to be extremely complex. I figured it was best to constrain it to one language rather than have a broken challenge. Maybe I should have not asked the question. But I think its pretty fun if you give it a try. – Post Rock Garf Hunter – 2017-07-08T15:16:00.873

as we are talking about Haskell only, what about builtin values? – nimi – 2017-07-08T15:19:42.610

@WheatWizard Idea: allow other languages to join in but they are marked as NC – Christopher – 2017-07-08T15:19:48.187

@nimi I think builtin values are fine. – Post Rock Garf Hunter – 2017-07-08T15:21:24.377

@Christopher The problem with other languages is that the challenge gets very out of control and the definition becomes untenable, not that I don't like them. I don't think allowing NC answers would improve the challenge. – Post Rock Garf Hunter – 2017-07-08T15:22:24.917

Good point imagine jelly without builtins – Christopher – 2017-07-08T15:22:47.463

1What does the list contain? Arbitrary Ord types?Positive integers? Arbitrary types and we get an ordering function? – Laikoni – 2017-07-08T15:24:20.193

@Laikoni Oh duh I forgot to add that. It will just be integers. Either Int or Integer type. – Post Rock Garf Hunter – 2017-07-08T15:25:37.713

@WheatWizard I think that if you allow functional programming languages that aren't designed for golfing (e.g. Python, Java, C etc.) where the definition of a builtin matches your current one, then the challenge would be better. If you do this, however, I would recommend having a list of languages which people can suggest changes to. – caird coinheringaahing – 2017-07-08T16:22:29.743

@cairdcoinheringaahing Believe it or not this was originally a python challenge, but I found it too difficult to get a good definition of builtin with member functions. I would also like to point out that none of the three languages you mentioned are functional :P, but I suppose that doesn't matter. – Post Rock Garf Hunter – 2017-07-08T16:29:03.413

Sorry, Wheat but I've downvoted this for the language restriction. The shame about it is that it would be an interesting challenge otherwise. – Shaggy – 2017-07-09T22:30:43.207

@Shaggy If you or anyone would like to post a version that is open to all languages they are free to do so. However it is my belief that such a challenge would turn out rather poorly and I am very satisfied with the current answers so I will not be opening this challenge to additional languages. I've already expressed why I believe this in previous comments so I won't go into further detail. – Post Rock Garf Hunter – 2017-07-10T01:36:15.300

Answers

4

Haskell, 57 56 37 33 bytes

f l=[y|x<-[minBound..],y<-l,y==x]

Feed it with Ints, e.g. f [1::Int,2,3].

A port of my answer from the other sort without builtin challenge which supports also lists with repeated values.

Edit: @Ørjan Johansen saved 4 bytes. Thanks!

nimi

Posted 2017-07-08T14:24:03.400

Reputation: 34 639

That fix is quite clever. – Post Rock Garf Hunter – 2017-07-08T15:44:45.313

@WheatWizard: it loops through all Ints, which takes some time. For a test you can go with f l=[-10..10]>>= \x->[y|y<-l,y==x] and limit the range of the element of the list to sort accordingly. -- or sort small values: f [-2^63+1::Int,-2^63,-2^63+1] – nimi – 2017-07-08T16:01:57.073

1I see. Congratulations, It look like you have figure out a way to sort a list in O(n) :P – Post Rock Garf Hunter – 2017-07-08T16:05:04.590

2This is 4 bytes shorter as a big list comprehension: f l=[y|x<-[minBound..],y<-l,y==x] – Ørjan Johansen – 2017-07-08T19:49:16.933

@ØrjanJohansen: well spotted! Thanks a lot! – nimi – 2017-07-08T20:23:54.553

1

Insertion Sort, 44 bytes

s!(a:b)|a<s=a:s!b
s!x=s:x
f(a:x)=a!f x
f a=a

Try it online!

Since nimi has already outgolfed me I thought I would post the solution I came up with to my own challenge.

Explanation

Here we define a function (!) that takes a sorted list and inserts an element so that the list is still sorted.

s!(a:b)|a<s=a:s!b
s!x=s:x

We then define a function f that takes sieves the elements of a list into this function. (basically the equivalent of a foldr).

f(a:x)=a!f x
f a=a

Since the empty list is sorted, and each insertion keeps the list sorted, the end result is a sorted list with all the same items as the original. This algorithm is O(n2).

Post Rock Garf Hunter

Posted 2017-07-08T14:24:03.400

Reputation: 55 382

1

Mergesort, 110 bytes

x@(a:r)#y@(b:s)|a<b=a:r#y|1<3=b:x#s
r#s=r++s
(x:r)!(a,b)=r!(b,x:a)
_!t=t
s[x]=[x]
s l|(a,b)<-l!([],[])=s a#s b

Try it online! Example usage: s [4,1,26,-3,0,5].

# takes two sorted lists and merges them. ! splits a list in two sublists of equal length. s returns singleton lists unchanged because they are already sorted and sorts longer lists by splitting them in two parts, recursively sorts both and merges the resulting lists.

Laikoni

Posted 2017-07-08T14:24:03.400

Reputation: 23 676

1

Quicksort (sort of), 44 bytes

s(h:t)=s[e|e<-t,e<=h]++h:s[e|e<-t,e>h]
s x=x

Try it online!

How it works:

s(h:t)=               -- let h be the head and t the rest of the input list
        [e|e<-t,e<=h] -- take all elements e from t that are less or equal than h
       s              -- and sort them recursively
          ++ h :      -- append h and append
        [e|e<-t,e>h]  -- all elements from t greater than h
       s              -- after sorting them
s x=x                 -- base case: if there's no first elemet, i.e. the list
                      -- is empty, the result is also the empty list

nimi

Posted 2017-07-08T14:24:03.400

Reputation: 34 639

0

Here's a shorter merge sort.

Merge sort, 99 bytes

s=f.((:[])<$>)
a@(x:y)!b@(z:w)|x>z=z:a!w|0<1=x:y!b
a!b=a++b
d(x:y:z)=x!y:d z
d p=p
f[x]=x
f x=f$d x

Try it online!

dfeuer

Posted 2017-07-08T14:24:03.400

Reputation: 1 016