Skip to content

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

Merged
merged 14 commits into from
Aug 14, 2018
Merged

Added Lisp implementation for bubble sort #348

merged 14 commits into from
Aug 14, 2018

Conversation

Trashtalk217
Copy link
Contributor

Added my name twice to CONTRIBUTORS.md but I have no clue how to fix it.

@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Aug 9, 2018
CONTRIBUTORS.md Outdated
@@ -57,3 +57,5 @@ NIFR91
Michal Hanajik
<br>
Bendik Samseth
Trashtalk
Trashtalk
Copy link
Member

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

Copy link
Member

@berquist berquist left a 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))
Copy link
Member

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) #'<))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is insead correct word?

Copy link
Contributor Author

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
Copy link
Member

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)
Copy link
Member

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.

berquist
berquist previously approved these changes Aug 11, 2018
Copy link
Member

@jiegillet jiegillet left a 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.

(print
(bubble-sort (list 1 2 3 3 2 1)))

;; The built-in sort is quicker, see the usage below
Copy link
Member

@jiegillet jiegillet Aug 11, 2018

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
Copy link
Member

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?

Copy link
Contributor Author

@Trashtalk217 Trashtalk217 Aug 12, 2018

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... :(

Copy link
Member

@jiegillet jiegillet Aug 12, 2018

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))
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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
Copy link
Member

@jiegillet jiegillet Aug 14, 2018

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

Copy link
Member

@jiegillet jiegillet left a 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!

@jiegillet jiegillet merged commit e5ddeba into algorithm-archivists:master Aug 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants