shegx01

shegx01

Little Help implementing Quick-sort algorithm

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.

Most Liked

hauleth

hauleth

You sort only less_than_head (BTW Your head is commonly called pivot) and greater_than_head is passed as is, so there is only partial sorting (only elements less than head are sorted).

EDIT: The big pro of using :lists.partition/2 is 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:

{lesser, greater} = :lists.partition(& &1 <= head, tail)
NobbZ

NobbZ

Not the lesser_than_head is usually called pivot, but the head is. Its the pivot point you use to compare.

Also you are still not sorting the greater_than_head :wink:

shegx01

shegx01

Got deprecated warning with Enum.partition/2,
Enum.split_with/2 instead.
Works great!
Thanks

My mistake :lists.partition/2 still works
updated to this

defmodule Quick do
  import Enum, only: [split_with: 2]
  def sort([]), do: []
  def sort([head|tail]) do
    {pivot, greater_than_head} = split_with(tail, &(&1 <= head))
    sort(pivot) ++ [head] ++ (greater_than_head)
   end

end

big thanks @hauleth

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement