Quick Sort in Rust vs Haskell

preview_player
Показать описание
Of course Rust is faster and all, but just look at Haskell code 😮 (parenthesis around flip filter are unnecessary btw)

This is the comparison between Quick array Sort algorithm written in Rust (I found the code on GitHub) and Haskell (I wrote the code). Turns out, Rust code is obscenely big, while Haskell is pretty elegant 😊. I know, Haskell implementation is not "true" quick sort as it is not "in place", but it is beautiful!!1

Рекомендации по теме
Комментарии
Автор

I'm a rust newbie, so sorry for any mistakes I make. I tried to also implement quicksort in Rust (see below), and I don't think the implementation needs to be that complicated.
Overcomplicated things I found:
ln 1: 'PartialEq' type constraint is unused
ln 2: using slices I think is cleaner
ln 11: just access pivot by index or use a reference, then you
a) don't need to clone it
b) can therefore get rid of 'Clone' type constraint
ln 11, 14: 'arr.get(idx).unwrap()' is equivalent to 'arr[idx]'
ln 14: 'arr' is already a vector, '.to_vec()' does nothing
ln 15, 19: 'arr.swap' doesn't return anything, 'let _' is unnecessary
This code seems to be written in a way to be as unnecessarily long as possible.

My implementaion:

fn quicksort<T: PartialOrd>(arr: &mut [T]) {
if arr.len() <= 1 {
return;
}

let pivot = partition(arr);

quicksort(&mut arr[..pivot]);
quicksort(&mut arr[pivot + 1..]);
}

fn partition<T: PartialOrd>(arr: &mut [T]) -> usize {
let mut i = 0;

for j in 0..arr.len() - 1 {
if arr[j] <= arr[arr.len() - 1] {
arr.swap(i, j);
i += 1;
}
}

arr.swap(i, arr.len() - 1);

i
}

fn main() {
let mut vec = vec![1, 5, 2, 7, 4, 4, 7, 4, 3];

println!("{:?}", vec);



for v in vec {
print!("{} ", v);
}

println!();
}

EDIT: note: 'quicksort(&mut vec)' is shorter than 'quicksort(vec.as_mut_slice())', I should've used that in my code

error-
Автор

Rust: more typing than thinking.
Haskell: more thinking than typing.
💀💀

polymations
Автор

1:03 be like: Arr! Get high, unwrap clone!

danielkonopka
Автор

Faster syntax is really not what Haskell brings to the table. Type system, safety, currying, function application and the infamous Monad - game changers.

davidyanceyjr
Автор

I just forgot how quicksort works by looking at the haskell code

Justanoobcoder
Автор

Had to write a javascript tolkenizer and interpreter in haskell for a CS class. It's programming poetry! You can't read it out loud like c-style imperative code, but if you know the pattern it's perfectly clear, like a haiku

SillyNaughty
Автор

My brother in Christ, you imported Data.List, and then didn’t even use partition?

This solution was very cute, I’ve been programming Haskell for nearly 15 years and never seen this solution. Well done.

Axman
Автор

The haskell sort is not quicksort. Quicksort is an in-place sorting algorithm and your haskell quicksort is not in-place.

This sort of comparison ultimately does Haskell a disservice because when folks clock that it isn't in-place they can be fooled into thinking Haskell cannot do an in-place sort.
It can do an in-place sort with with ST (state threads), but naturally you can't sort immutable data in-place, so this works out meaning that you must either copy once, or the data must be created in the same state thread that sorts it.

The state thread solution using mutable vectors would probably look similar to the rust solution.

DarkEthereal
Автор

Rust and Haskell are two languages I'd love to love. I think I'd need regular tuition to really get my head into it. That, and the lack of need for the stuff I write myself.

Chalisque
Автор

I might be biased but I think the haskell code is unreadable.

redcrafterlppa
Автор

The haskell one can be written more succinctly as

quicksort [] = []
quicksort (x:xs) = filter (<= x) xs ++ (x : filter (>x) xs)

Notably this is more the "student" form of sort, you can actually implement in-place (well almost, you need to copy the list twice) introsort in "pure" Haskell, but it'd look nearly identical to what you'd write in C++ or Rust so it loses its value as an example.

hannessteffenhagen
Автор

i don't know rust but it looks like it's sorting in place whereas the haskell program doesn't.

minamur
Автор

Meanwhile me who's used to C/C++, Java and Python: what the kurwa fuck is this syntax

theabyss
Автор

I would like to also mention underestimated ELM - its great functional-first programming language for frontend - its a complete tool for frontend! And its REALLY REALLY EASY!

crioss
Автор

Title is extremely misleading. Students will stumble upon this and get screwed on haskell understanding. This is not quicksort. Not even a different form of quicksort.

Poneglyph
Автор

It’s funny to see some comments said q sort is in-place and if wanted to achieve so the code would be imperative at the same time.

陳毅澂-ch
Автор

i agree haskell is more elegant and simple

caddr
Автор

Люблю твій канал, бачу відос спішу дивитись перш ніж інші, але от питоня, на яких мовах працюєш професійно й у якій області?

hytryi_huy
Автор

Здравствуйте. Ответьте пожалуйста что это за ОС, терминал и текстовый эдитор. Мне очень интересно !

Hello. Please answer - what are those OS, terminal, and text editor. I'm very interested !

🍇🍇🍇

riad
Автор

qs :: Ord a => [a] -> [a]
qs [] = []
qs (h:t) = qs [b|b<-t, b<h] ++ [h] ++ qs [c|c<-t, c>=h]

randomguy