DEV Community

Implementing Merge Sort in Rust

felixfaisal on July 23, 2021

Merge sort is arguably the most popular divide and conquer algorithm, It is one of the first algorithms any software engineer learns while learning...
Collapse
 
rsalmei profile image
Rogério Sampaio de Almeida

Man, your merge has very poor performance, because you are cloning the whole array twice inside a recursive function! Every merge call, even the ones with only one element on each side, will clone the whole array twice... You could try to make it in-place, just swapping elements.
Also, it is written like in C, it could be very improved using Rust constructs, like iterators, slices, and some trait magic.
Here it is one that's very good: dev.to/creativcoder/merge-k-sorted...
I specially like the one in the comments.

Collapse
 
felixfaisal profile image
felixfaisal

Hey, Thanks a lot for sharing the article, I'm still exploring Rust plus learning other things. I'm familiar with C so I was sort of seeing how it would be implemented in Rust Syntax.