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

Last Post!

shegx01

shegx01

Ohh,
It has been written well in the Idea, didn’t know I failed to include sort greater.
Thanks to you

Where Next?

Popular in Questions Top

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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement