Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
(→‎[[Seed7]]: bubbleSort)
(Added Clean example)
Line 121: Line 121:
std::cout << std::endl ;
std::cout << std::endl ;
}
}

==[[Clean]]==
[[Category:Clean]]
Bubble sorting an array in-situ (using destructive updates), using Clean's uniqueness typing. We specified the type of <tt>sweep</tt> using strictness annotations to improve performance.
import StdEnv
bsort :: *(a e) -> *(a e) | Array a e & < e
bsort array
# (done, array) = sweep 1 True array
= if done array (bsort array)
where
sweep :: !Int !Bool !*(a e) -> (!Bool, !*(a e)) | Array a e & < e
sweep i done array
| i >= size array = (done, array)
# (e1, array) = array![i - 1]
(e2, array) = array![i]
| e1 > e2 = sweep (i + 1) False {array & [i - 1] = e2, [i] = e1}
= sweep (i + 1) done array
Using it to sort an array of a hunderd numbers:
Start :: {Int}
Start = bsort {x \\ x <- [100,99..1]}


==[[Forth]]==
==[[Forth]]==