Skip to content

Instantly share code, notes, and snippets.

@vaishaks
Created November 22, 2013 19:47
Show Gist options
  • Save vaishaks/7605721 to your computer and use it in GitHub Desktop.
Save vaishaks/7605721 to your computer and use it in GitHub Desktop.
Quicksort in scheme. It's beautiful! :'(
(define less
(lambda (L x)
(cond ((null? L) '())
((< (car L) x) (cons (car L) (less (cdr L) x)))
(else (less (cdr L) x)))))
(define great
(lambda (L x)
(cond ((null? L) '())
((> (car L) x) (cons (car L) (great (cdr L) x)))
(else (great (cdr L) x)))))
(define append
(lambda (L M)
(if (null? L)
M
(cons (car L) (append (cdr L) M)))))
(define quicksort
(lambda (L)
(if (null? L)
'()
(append
(append
(quicksort (less L (car L)))
(list (car L)))
(quicksort (great L (car L)))))))
(define user-input
(lambda (l n)
(if (= n 0)
l
(user-input (append (list (read)) l) (- n 1)))))
(display "Enter the number of elements: ")
(define n (read))
(display "Input the elements in the list: ")
(define L (user-input '() n))
(display "Unsorted list: ")
(display L) (newline)
(display "The sorted list: ")
(display (quicksort L)) (newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment