简体   繁体   中英

Does sort function in OCaml use immutable or mutable data structure?

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 ?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM