The quicksort algorithm in HaskellBestApproximationDiv2 problem in HaskellSplit list into groups of n in...

Using only 1s, make 29 with the minimum number of digits

The effects of magnetism in radio transmissions

We are very unlucky in my court

Groups acting on trees

How to deal with an incendiary email that was recalled

Why avoid shared user accounts?

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

Dilemma of explaining to interviewer that he is the reason for declining second interview

What kind of hardware implements Fourier transform?

Is a debit card dangerous for an account with low balance and no overdraft protection?

Checking for the existence of multiple directories

Isn't using the Extrusion Multiplier like cheating?

Does Windows 10's telemetry include sending *.doc files if Word crashed?

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

If I sold a PS4 game I owned the disc for, can I reinstall it digitally?

Help Me simplify: C*(A+B) + ~A*B

Jumping Numbers

What to do when being responsible for data protection in your lab, yet advice is ignored?

How do I say "Brexit" in Latin?

Citing paywalled articles accessed via illegal web sharing

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

Do authors have to be politically correct in article-writing?

Word or phrase for showing great skill at something without formal training in it

Cryptic with missing capitals



The quicksort algorithm in Haskell


BestApproximationDiv2 problem in HaskellSplit list into groups of n in HaskellImplementing Haskell's `insert`Finding overlapping time intervalsProject Euler Problem 54 in HaskellSorting a Stack in ascending orderQuickMergeSort — The power of internal bufferingFast quicksort implementationRadix sort implementation in JS (LSD)Recursive quicksort in Python













0












$begingroup$


I am learning Haskell programming language mainly from this source. And there I have encouraged with "an elegant" realization of the quicksort sorting algorithm (the Quick, sort! section). Here it is:



Book's implementation



quicksort :: ( Ord a ) = > [ a ] -> [ a ]
quicksort [] = []
quicksort ( x : xs ) =
let smallerSorted = quicksort [ a | a <- xs , a <= x ]
biggerSorted = quicksort [ a | a <- xs , a > x ]
in smallerSorted ++ [ x ] ++ biggerSorted


The problem with this implementation (as I think) is doubled number of comparisons inside the let binding every time. Isn't the fact that those of a's that already in the smallerSorted list cannot be in the biggerSorted list, so we don't need to compare x with them anymore.



In order to "improve" the above approach I have written my own implementation, where I use auxiliary local function split that splits a list into two parts: less than (or equal to) x and greater than x.



My implementation



quick_sort :: (Ord a) => [a] -> [a]
quick_sort [] = [] --edge condition
quick_sort (x : xs) = --general condition
let (lt, gt) = split x xs
in (quick_sort lt) ++ [x] ++ (quick_sort gt)
where
--split function is used to split a list
--into two sublists: one for elements
--less or equal (lt) than some value - x
--and one for those that greater (gt) than x
--NOTE: split function is also recursive
split x [] = ([], []) --edge condition
split x (h : hs) --general condition
| h <= x =
let (lt, gt) = split x hs
in ( (h : lt), gt )
| otherwise =
let (lt, gt) = split x hs
in ( lt, (h : gt) )


P.S.
For a while I am not able to compare two approaches, but on the worst cases (when list to sort is already sorted) it seems that my implementation is a bit slower.










share|improve this question









$endgroup$

















    0












    $begingroup$


    I am learning Haskell programming language mainly from this source. And there I have encouraged with "an elegant" realization of the quicksort sorting algorithm (the Quick, sort! section). Here it is:



    Book's implementation



    quicksort :: ( Ord a ) = > [ a ] -> [ a ]
    quicksort [] = []
    quicksort ( x : xs ) =
    let smallerSorted = quicksort [ a | a <- xs , a <= x ]
    biggerSorted = quicksort [ a | a <- xs , a > x ]
    in smallerSorted ++ [ x ] ++ biggerSorted


    The problem with this implementation (as I think) is doubled number of comparisons inside the let binding every time. Isn't the fact that those of a's that already in the smallerSorted list cannot be in the biggerSorted list, so we don't need to compare x with them anymore.



    In order to "improve" the above approach I have written my own implementation, where I use auxiliary local function split that splits a list into two parts: less than (or equal to) x and greater than x.



    My implementation



    quick_sort :: (Ord a) => [a] -> [a]
    quick_sort [] = [] --edge condition
    quick_sort (x : xs) = --general condition
    let (lt, gt) = split x xs
    in (quick_sort lt) ++ [x] ++ (quick_sort gt)
    where
    --split function is used to split a list
    --into two sublists: one for elements
    --less or equal (lt) than some value - x
    --and one for those that greater (gt) than x
    --NOTE: split function is also recursive
    split x [] = ([], []) --edge condition
    split x (h : hs) --general condition
    | h <= x =
    let (lt, gt) = split x hs
    in ( (h : lt), gt )
    | otherwise =
    let (lt, gt) = split x hs
    in ( lt, (h : gt) )


    P.S.
    For a while I am not able to compare two approaches, but on the worst cases (when list to sort is already sorted) it seems that my implementation is a bit slower.










    share|improve this question









    $endgroup$















      0












      0








      0





      $begingroup$


      I am learning Haskell programming language mainly from this source. And there I have encouraged with "an elegant" realization of the quicksort sorting algorithm (the Quick, sort! section). Here it is:



      Book's implementation



      quicksort :: ( Ord a ) = > [ a ] -> [ a ]
      quicksort [] = []
      quicksort ( x : xs ) =
      let smallerSorted = quicksort [ a | a <- xs , a <= x ]
      biggerSorted = quicksort [ a | a <- xs , a > x ]
      in smallerSorted ++ [ x ] ++ biggerSorted


      The problem with this implementation (as I think) is doubled number of comparisons inside the let binding every time. Isn't the fact that those of a's that already in the smallerSorted list cannot be in the biggerSorted list, so we don't need to compare x with them anymore.



      In order to "improve" the above approach I have written my own implementation, where I use auxiliary local function split that splits a list into two parts: less than (or equal to) x and greater than x.



      My implementation



      quick_sort :: (Ord a) => [a] -> [a]
      quick_sort [] = [] --edge condition
      quick_sort (x : xs) = --general condition
      let (lt, gt) = split x xs
      in (quick_sort lt) ++ [x] ++ (quick_sort gt)
      where
      --split function is used to split a list
      --into two sublists: one for elements
      --less or equal (lt) than some value - x
      --and one for those that greater (gt) than x
      --NOTE: split function is also recursive
      split x [] = ([], []) --edge condition
      split x (h : hs) --general condition
      | h <= x =
      let (lt, gt) = split x hs
      in ( (h : lt), gt )
      | otherwise =
      let (lt, gt) = split x hs
      in ( lt, (h : gt) )


      P.S.
      For a while I am not able to compare two approaches, but on the worst cases (when list to sort is already sorted) it seems that my implementation is a bit slower.










      share|improve this question









      $endgroup$




      I am learning Haskell programming language mainly from this source. And there I have encouraged with "an elegant" realization of the quicksort sorting algorithm (the Quick, sort! section). Here it is:



      Book's implementation



      quicksort :: ( Ord a ) = > [ a ] -> [ a ]
      quicksort [] = []
      quicksort ( x : xs ) =
      let smallerSorted = quicksort [ a | a <- xs , a <= x ]
      biggerSorted = quicksort [ a | a <- xs , a > x ]
      in smallerSorted ++ [ x ] ++ biggerSorted


      The problem with this implementation (as I think) is doubled number of comparisons inside the let binding every time. Isn't the fact that those of a's that already in the smallerSorted list cannot be in the biggerSorted list, so we don't need to compare x with them anymore.



      In order to "improve" the above approach I have written my own implementation, where I use auxiliary local function split that splits a list into two parts: less than (or equal to) x and greater than x.



      My implementation



      quick_sort :: (Ord a) => [a] -> [a]
      quick_sort [] = [] --edge condition
      quick_sort (x : xs) = --general condition
      let (lt, gt) = split x xs
      in (quick_sort lt) ++ [x] ++ (quick_sort gt)
      where
      --split function is used to split a list
      --into two sublists: one for elements
      --less or equal (lt) than some value - x
      --and one for those that greater (gt) than x
      --NOTE: split function is also recursive
      split x [] = ([], []) --edge condition
      split x (h : hs) --general condition
      | h <= x =
      let (lt, gt) = split x hs
      in ( (h : lt), gt )
      | otherwise =
      let (lt, gt) = split x hs
      in ( lt, (h : gt) )


      P.S.
      For a while I am not able to compare two approaches, but on the worst cases (when list to sort is already sorted) it seems that my implementation is a bit slower.







      sorting haskell recursion complexity






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      LRDPRDXLRDPRDX

      2346




      2346






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214500%2fthe-quicksort-algorithm-in-haskell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214500%2fthe-quicksort-algorithm-in-haskell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Webac Holding Inhaltsverzeichnis Geschichte | Organisationsstruktur | Tochterfirmen |...

          What's the meaning of a knight fighting a snail in medieval book illustrations?What is the meaning of a glove...

          Salamanca Inhaltsverzeichnis Lage und Klima | Bevölkerungsentwicklung | Geschichte | Kultur und...