Skip to content

Instantly share code, notes, and snippets.

@Hayao0819
Created August 2, 2025 08:02
Show Gist options
  • Save Hayao0819/73410f391d201d9648bc88176089a9f0 to your computer and use it in GitHub Desktop.
Save Hayao0819/73410f391d201d9648bc88176089a9f0 to your computer and use it in GitHub Desktop.
クイックソートが動かない
#!/usr/bin/env runghc
class QuickSortable a where
pickPivot :: [a] -> a
pickPivot (top : _) = top
before :: Ord a => a -> [a] -> [a]
before pivot arr = filter (\n -> n < pivot) arr
after :: Ord a => a -> [a] -> [a]
after pivot arr = filter (\n -> n >= pivot) arr
instance Ord a => QuickSortable a
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort arr = sortedBefore ++ [pivot] ++ sortedAfter
where
pivot = pickPivot arr
sortedBefore = quicksort (before pivot arr)
sortedAfter = quicksort (after pivot arr)
main = do
print (quicksort ([1, 2, 4, 2, 7, 9, 6] :: [Int]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment