Beispiel: Sortieren durch Einfügen
Beispiel: Sortieren durch Einfügen
das Verfahren ist uns wohlbekannt:
sortiere Liste durch wiederholtes ordnungsgerechtes Einfügen.
into :: Ord a => a->[a]->[a] - Sortieren braucht Ordnungsrelation auf
- Elementtyp!
into x [] = [x]
into x (y:ys)
|x <= y = x:y:ys - aufsteigend!
|otherwise = y:into x ys
iSort :: Ord a => [a]->[a]
iSort [] = []
iSort (y:ys) = into y (iSort ys)
- Anwendung zum Beispiel:
> into 3 [1,5,7,12]
[1,3,5,7,12]
> into 'a' "Computer"
"Caomputer"
> into "xx" ["aa", "zz"]
["aa","xx","zz"] - Strings und allgemein Listen von Elementen mit
- Ordnungsrelation, sind in Haskell
- lexikographisch geordnet!
> iSort [2,-9,67,-3,0,11]
[-9,-3,0,2,11,67]
> iSort [cos 0, cos 1, cos 2, cos 3]
[-0.989992, -0.416147, 0.540302, 1.0]
Man beachte die Segnungen von overloading und polymorphism!
Kontrollfrage: was gibt
> iSort [cos,sin] ? > iSort [] ?
Ronald Blaschke
1998-04-19