-
-
Save VoQn/1839052 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def qsort (arr, left=0, right=arr.length-1, pl=left, pr=right) | |
x = arr[(left+right)/2] | |
begin | |
pl+=1 while arr[pl] < x | |
pr-=1 while arr[pr] > x | |
arr[pl], arr[pr] = arr[pr], arr[pl] | |
end while arr[pl] != arr[pr] | |
qsort(arr, left, pl-1) if pl-left > 1 | |
qsort(arr, pr+1, right) if right-pr > 1 | |
end | |
arr =(1..10).sort_by {rand} | |
qsort(arr) | |
p "result:#{arr}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
{-| | |
- dependency other hackage packages | |
- | |
- test-framework, test-framework-th, | |
- test-framework-hunit, test-framework-quickcheck, | |
- HUnit, QuickCheck, data-ordlist | |
|-} | |
import Test.Framework.TH ( defaultMainGenerator ) | |
import Test.Framework ( defaultMain, Test ) | |
import Test.Framework.Providers.HUnit ( testCase ) | |
import Test.Framework.Providers.QuickCheck2 ( testProperty ) | |
import Test.HUnit ( (@=?) ) | |
import Data.List.Ordered | |
{-| QuickSort Implementation |-} | |
qsort [] = [] | |
qsort (x:xs) = rest (<= x) ++ x : rest (> x) where | |
rest that = qsort $ filter that xs | |
{-| Tests |-} | |
main = $(defaultMainGenerator) | |
case_empty_list_qsort | |
= [] @=? qsort ([] :: [Int]) | |
case_only_one_value_list_qsort | |
= [1] @=? qsort [1] | |
case_some_values = | |
[-5..5] @=? qsort [5, -2, 0, 4, 1, -1, 3, -5, 2, -3, -4] | |
prop_list_that_sort_by_qsort_is_sorted | |
= isSorted . (qsort :: [Int] -> [Int]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
自動テスト追加して Haskell で実装してみた (2行)