Reputation: 25812
Does OCaml use mutable
or immutable
data structure in implementation of sort?
For many sort algorithms, we need to exchange data between positions in list or array or something.
I am just wondering, if OCaml is always intended to use immutable data structures
, then each exchange operation will create a new copy?
Will that impact the performance
?
Upvotes: 0
Views: 429
Reputation: 370212
For many sort algorithms, we need to exchange data between positions in list or array or something.
Not necessarily.
I am just wondering, if OCaml is always intended to use immutable data structures
No, OCaml has both mutable and immutable data structures. Using immutable data structures is generally preferred though.
then each exchange operation will create a new copy?
That would depend on the data structure in question. But generally you wouldn't want to express your sorting algorithm in terms of swapping individual elements when working with an immutable data structure (and as I indicated above, you certainly don't need to).
List.sort
for example works on an immutable data structure (lists) and is perfectly efficient. It uses the merge sort algorithm (in current implementations of OCaml).
Upvotes: 2