Sorting algorithms/Insertion sort: Difference between revisions

Emacs Lisp: Make code more idiomatic
m (header bash)
(Emacs Lisp: Make code more idiomatic)
Line 1,832:
 
=={{header|Emacs Lisp}}==
<lang lisp>(defun min-or-max-of-a-list (lonnumbers relcomparator)
<lang lisp>
"Return minimum or maximum of NUMBERS using COMPARATOR."
(let ((extremum (car numbers)))
(dolist (n (cdr numbers))
(when (funcall comparator n extremum)
(setq extremum n)))
nilextremum))
 
(defun minremove-ornumber-maxfrom-of-2-numberslist (n1 n2numbers reln)
"Return NUMBERS without N.
"n1 and n2 are two numbers, rel can be '< or '> according to
If n is present twice or more, it will be removed only once."
what sort of sorting is wanted, this function returns the greater
(let (result)
or smaller number n1 or n2"
(while numbers
(cond
(let (eval(number (listpop rel n1 n2numbers)) n1)
(tif (= number n2))n)
(while numbers
(push (pop numbers) result))
(defun min-or-max-of-a-list (lon rel)
(push number result))))
"lon is a list of numbers, rel is '< or '>, this fonction
(nreverse result)))
returns the higher or lower number of the list"
(if (cdr lon)
(min-or-max-of-2-numbers (car lon)
(min-or-max-of-a-list (cdr lon) rel)
rel)
(car lon)))
 
(defun removeinsertion-number-from-listsort (nnumbers loncomparator)
"lonReturn is asorted list of numbers,NUMBERS nusing is a number belonging to the list,COMPARATOR."
(if lonnumbers
this function returns the same list but the number n. If n is
(let ((extremum (min-or-max-of-2a-list numbers (car loncomparator)))
present twice or more, it will be removed only once"
(cons extremum
(if lon
(t (cons (car lon)insertion-sort (remove-number-from-list n (cdrnumbers lon))))extremum)
(cond
((= (car lon) n) (cdr lon comparator)))
(t (cons (car lon) (remove-number-from-list n (cdr lon)))))
nil))
 
(sort-insertion-sort '(list 1 2 3 9 8 7 25 12 3 2 1) #'>)</lang>
 
{{out}}
(defun sort-insertion (lon rel)
"lon is a list of numbers, rel can be '< or '>, this function
returns a list containing the same elements but which is sorted
according to rel"
(if lon
(cons (min-or-max-of-a-list lon rel)
(sort-insertion
(remove-number-from-list
(min-or-max-of-a-list lon rel)
lon)
rel))
nil))
 
;;; let's try it :
 
(sort-insertion (list 1 2 3 9 8 7 25 12 3 2 1) '>)
 
(25 12 9 8 7 3 3 2 2 1 1)
</lang>
 
=={{header|Erlang}}==
Anonymous user