haskell quick sort, why the first let uses <= instead of <?

BufBills
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
    let smallerOrEqual = [a | a <- xs, a <= x]
        larger = [a | a <- xs, a > x]
    in quicksort smallerOrEqual ++ [x] ++ larger

main = do
    let a = [ 5, 1, 9, 4, 6, 7, 3]
    print  $ quicksort a

in Haskell quick sort, why the first let uses <= instead of <? I think <= will duplicate that x many times. Why not?

thefourtheye

I think <= will duplicate that x many times

No, it will not. Let us understand what exactly is happening here. You are basically partitioning your list into three parts.

  1. List of numbers smaller than or equal to the pivot element (excluding the first element, since that is the pivot element)

  2. The pivot element itself (the first element in the list)

  3. List of numbers greater than the pivot element

So, in your case, the partitioned list becomes like this

[1, 4, 3] ++ [5] ++ [9, 6, 7]

Consider a case like this, quicksort [5, 1, 5, 9, 8, 5, 3, 6, 4]. Then, your program will partition it into something like this

smallerOrEqual ++ [x] ++ larger

Since smallerOrEqual and larger work with xs, which doesn't have the x, there is no duplication as such. Now, the partitioned list, after the filtering, becomes

[1, 5, 5, 3, 4] ++ [5] ++ [9, 8, 6]

See? There is no duplication, just partitioning.

Note: Your program has a serious bug. Check this line

quicksort smallerOrEqual ++ [x] ++ larger

It basically works like this

(quicksort smallerOrEqual) ++ [x] ++ larger

So, the larger list is never sorted. You recursively have to sort both the smaller list and the greater list and finally merge them in to one. So, it should have been

(quicksort smallerOrEqual) ++ [x] ++ (quicksort larger)

which can be written without the brackets like this

quicksort smallerOrEqual ++ [x] ++ quicksort larger

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

haskell quick sort complexity?

分類Dev

quick sort, first sort works, parameters for recursive calls don't

分類Dev

Why combining insert sort and quick sort get worse result?

分類Dev

Why don't we use let instead of States?

分類Dev

Why first peek and then remove instead of poll

分類Dev

Control.Arrow: Why "let (a,b) = (first,second)" fails?

分類Dev

Why use null function instead of == [] to check for empty list in Haskell?

分類Dev

Quick Sort Understanding in this case

分類Dev

Implementing quick sort in Ruby

分類Dev

Why does this promise resolve with the first item in the chain instead of the last one?

分類Dev

Quick Sort not working as expected in java

分類Dev

Quick sort issue c++

分類Dev

Haskell:Where vs. Let

分類Dev

K-th smallest quick sort python

分類Dev

Creating a quick sort using recursion and generics

分類Dev

What is the time complexity of a modified quick sort algorithm?

分類Dev

Sort tuples by one of their elements in haskell

分類Dev

Why is Apple using this "if let" code?

分類Dev

Haskell - constructing a type that uses existential quantification

分類Dev

External library uses print instead of return

分類Dev

Restangular uses HTTP OPTIONS instead of GET

分類Dev

Why is <*> an infix function in Haskell?

分類Dev

How to find out whether collation uses word sort or string sort?

分類Dev

Why is python writing ` instead of @

分類Dev

Sort very long Linked List using Merge Sort or Quick Sort in C

分類Dev

Quick & Easy: why won't this run?

分類Dev

Intellij quick documentation is displayed in a separate panel instead of a popup

分類Dev

Adding a counter to a Quick Sort algorithm to display the total number of swap actions

分類Dev

Haskell Esqueleto project to list of records instead of tuples

Related 関連記事

  1. 1

    haskell quick sort complexity?

  2. 2

    quick sort, first sort works, parameters for recursive calls don't

  3. 3

    Why combining insert sort and quick sort get worse result?

  4. 4

    Why don't we use let instead of States?

  5. 5

    Why first peek and then remove instead of poll

  6. 6

    Control.Arrow: Why "let (a,b) = (first,second)" fails?

  7. 7

    Why use null function instead of == [] to check for empty list in Haskell?

  8. 8

    Quick Sort Understanding in this case

  9. 9

    Implementing quick sort in Ruby

  10. 10

    Why does this promise resolve with the first item in the chain instead of the last one?

  11. 11

    Quick Sort not working as expected in java

  12. 12

    Quick sort issue c++

  13. 13

    Haskell:Where vs. Let

  14. 14

    K-th smallest quick sort python

  15. 15

    Creating a quick sort using recursion and generics

  16. 16

    What is the time complexity of a modified quick sort algorithm?

  17. 17

    Sort tuples by one of their elements in haskell

  18. 18

    Why is Apple using this "if let" code?

  19. 19

    Haskell - constructing a type that uses existential quantification

  20. 20

    External library uses print instead of return

  21. 21

    Restangular uses HTTP OPTIONS instead of GET

  22. 22

    Why is <*> an infix function in Haskell?

  23. 23

    How to find out whether collation uses word sort or string sort?

  24. 24

    Why is python writing ` instead of @

  25. 25

    Sort very long Linked List using Merge Sort or Quick Sort in C

  26. 26

    Quick & Easy: why won't this run?

  27. 27

    Intellij quick documentation is displayed in a separate panel instead of a popup

  28. 28

    Adding a counter to a Quick Sort algorithm to display the total number of swap actions

  29. 29

    Haskell Esqueleto project to list of records instead of tuples

ホットタグ

アーカイブ