Skip to content

Instantly share code, notes, and snippets.

@jutememo
Created October 19, 2009 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jutememo/213444 to your computer and use it in GitHub Desktop.
Save jutememo/213444 to your computer and use it in GitHub Desktop.
import Data.List
import Data.Maybe
selectionsort [] = []
selectionsort xs = head swapped : (selectionsort $ tail swapped)
where swapped = swap 0 indexOfMin xs
indexOfMin = fromJust $ elemIndex (minimum xs) xs
-- リスト xs のインデックス i, j の要素を交換
swap i j xs | i < j = slice 0 (i-1) xs ++ [xs!!j] ++
slice (i+1) (j-1) xs ++ [xs!!i] ++
slice (j+1) ((length xs)-1) xs
| i == j = xs
-- リスト xs のインデックス i から j の間の要素を取得
slice i j xs | i < j = xs!!i : slice (i+1) (j-1) xs ++ [xs!!j]
| i == j = [xs!!i]
| i > j = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment