shegx01
Just getting started with Elixir and trying to implement Quick-sort algorithm using recursion. It seems like everything works but keying in this [8,16,3,7,23,77,9,30] broke the flow.
I was expecting [3, 7, 8, 9, 16, 23, 77, 30] but I did get this
[3, 7, 8, 16, 23, 77, 9, 30].
Also did a google search and saw other solutions calling filter or partition fro Enum module, I’m keen on using list comprehension implementing it.
Here is the code.
defmodule Quick do
def sort([]), do: []
def sort([head|tail]) do
less_than_head = for x <- tail, x <= head, do: x
greater_than_head = for x <- tail, x > head, do: x
sort(less_than_head) ++ [head] ++ (greater_than_head)
end
end
please do advice and how I can improve it.
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
You sort only
less_than_head(BTW Yourheadis commonly calledpivot) andgreater_than_headis passed as is, so there is only partial sorting (only elements less thanheadare sorted).EDIT: The big pro of using
:lists.partition/2is that it will split your list into two in one “walk” through the list instead of two, which can be improvement in case of long lists (but in such case the quick sort isn’t the best solution anyway, merge sort will work better).So your 2 comprehensions can be easily replaced with:
shegx01
Got deprecated warning with
Enum.partition/2,Enum.split_with/2instead.Works great!
Thanks
My mistake :lists.partition/2 still works
updated to this
big thanks @hauleth
shegx01
Thanks to you,
Will replace the list comprehension with Enum.partition
NobbZ
Not the
lesser_than_headis usually calledpivot, but theheadis. Its the pivot point you use to compare.Also you are still not sorting the
greater_than_headhauleth
This should be written as:
As you still do not sort
greaterpart as @NobbZ said.shegx01
Ohh,
It has been written well in the Idea, didn’t know I failed to include sort greater.
Thanks to you