-
-
Notifications
You must be signed in to change notification settings - Fork 360
Added Lisp implementation for bubble sort #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Lisp implementation for bubble sort #348
Conversation
CONTRIBUTORS.md
Outdated
@@ -57,3 +57,5 @@ NIFR91 | |||
Michal Hanajik | |||
<br> | |||
Bendik Samseth | |||
Trashtalk | |||
Trashtalk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be possible to modify this file on Github and remove that duplicate line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my understanding of idiomatic Lisp, I think this is fine, algorithm-wise.
Stylistically, indentation should be 2 spaces, not 4.
You also forgot to add the implementation to the book. I'll make a PR on your branch.
lst)))) ;else | ||
|
||
;;Use of an array insead of a list would be faster | ||
;;The built-in sort is also quicker (sort (list 5 4 3 2 1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make this an actual comparison:
(print (sort (list 5 4 3 2 1) #'<)))
-;;Use of an array insead of a list would be faster
-;;The built-in sort is also quicker (sort (list 5 4 3 2 1))
-(print (bubble_sort (list 5 4 3 2 1)))
\ No newline at end of file
+;; Use of an array insead of a list would be faster
+(print (bubble-sort (list 5 4 3 2 1)))
+;; The built-in sort is also quicker:
+(print (sort (list 5 4 3 2 1) #'<))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is insead correct word?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichalHanajik I type really fast ok ;)
@@ -0,0 +1,18 @@ | |||
;;;;Bubble sort implementation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace this with a line on how to run it:
;;; Run as `clisp bubble_sort.lisp`
(rotatef (car list-tail) (elt list-tail (- high low))) | ||
lst)) | ||
|
||
(defun bubble_sort (lst) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bubble_sort -> bubble-sort
to be idiomatic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get rid of the comparison, the other comments are there more for discussion.
(bubble-sort (list 1 2 3 3 2 1))) | ||
|
||
;; The built-in sort is quicker, see the usage below |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comparison part below is not useful, you can get rid of it
EDIT: Sorry, I just saw that this was suggested for you to add. Still get rid of it though ^^
(swap lst n (+ n 1)) ;then | ||
lst)))) ;else | ||
|
||
;; Use of an array instead of a list would be faster |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you use an array then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A list looks a bit prettier and - turns out - an array doesn't work with nth
, which only works with lists. I probably need to look at that code or that comment... :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply remove the comment and don't open the can of worms :)
@@ -0,0 +1,30 @@ | |||
;;;; Bubble sort implementation | |||
|
|||
;;; Swaps two elements in a list (complexity: O(n)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the moment you're using nth
and swap
to check and swap the values, which are both O(n). It's not like we are aiming for performance here, but it seems like you could do make everything a little tighter by navigating through the list and making changes as they come up, and loop over that process only once. I don't know Lisp, so I don't know if it's hard to do, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, but I don't think I can fix this. Other sorts are way easier to implement, and my knowledge of lisp is limited. I could just copy it from elsewhere and it would be more efficient. But should I do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, no, don't copy code. And I know how awkward bubble sort is for functional programming, I did it in Haskell.
I was thinking go a function like this (in pseudocode):
function bubble-up (list):
if length(list) < 2 : return list
else:
if first(list)>second(list): second(list) + bubble-up (first(list) + rest of the list)
else: first(list) + bubble-up (second(list) + rest of the list)
Then you run bubble-up
n
times and voila. Is that difficult to implement?
(cons | ||
(first list) | ||
(bubble-up | ||
(cons |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think (cons (second list) (rest (rest list)) )
is the same as (rest list)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks a lot!
Added my name twice to CONTRIBUTORS.md but I have no clue how to fix it.