Skip to content

Instantly share code, notes, and snippets.

@VoQn
Forked from mizchi/alg.rb
Created February 15, 2012 21:16
Show Gist options
  • Save VoQn/1839052 to your computer and use it in GitHub Desktop.
Save VoQn/1839052 to your computer and use it in GitHub Desktop.
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}"
{-# 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])
@VoQn
Copy link
Author

VoQn commented Feb 15, 2012

自動テスト追加して Haskell で実装してみた (2行)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment