Created
October 19, 2009 15:13
-
-
Save jutememo/213444 to your computer and use it in GitHub Desktop.
This file contains 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
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