2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,8 +1,21 @@
|
|||
{{Sorting Algorithm}}
|
||||
In this task, the goal is to sort an [[array]] (or list) of elements using the Selection sort algorithm. It works as follows:
|
||||
|
||||
;Task:
|
||||
Sort an [[array]] (or list) of elements using the Selection sort algorithm.
|
||||
|
||||
|
||||
It works as follows:
|
||||
|
||||
First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.
|
||||
Its asymptotic complexity is [[O]](n<sup>2</sup>) making it inefficient on large arrays. Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM.
|
||||
|
||||
|
||||
Its asymptotic complexity is <big> [[O]](n<sup>2</sup>) </big> making it inefficient on large arrays.
|
||||
|
||||
Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM.
|
||||
|
||||
No other sorting algorithm has less data movement.
|
||||
|
||||
For more information see the article on [[wp:Selection_sort|Wikipedia]].
|
||||
|
||||
;Reference:
|
||||
* Wikipedia: [[wp:Selection_sort|Selection sort]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
* Selection sort 26/06/2016
|
||||
SELECSRT CSECT
|
||||
USING SELECSRT,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) prolog
|
||||
ST R13,4(R15) "
|
||||
ST R15,8(R13) "
|
||||
LR R13,R15 "
|
||||
LA RJ,1 j=1
|
||||
DO WHILE=(C,RJ,LE,N) do j=1 to n
|
||||
LR RK,RJ k=j
|
||||
LR R1,RJ j
|
||||
SLA R1,2 .
|
||||
LA R3,A-4(R1) @a(j)
|
||||
L RT,0(R3) temp=a(j)
|
||||
LA RI,1(RJ) i=j+1
|
||||
DO WHILE=(C,RI,LE,N) do i=j+1 to n
|
||||
LR R1,RI i
|
||||
SLA R1,2 .
|
||||
L R2,A-4(R1) a(i)
|
||||
IF CR,RT,GT,R2 THEN if temp>a(i) then
|
||||
LR RT,R2 temp=a(i)
|
||||
LR RK,RI k=i
|
||||
ENDIF , end if
|
||||
LA RI,1(RI) i=i+1
|
||||
ENDDO , end do
|
||||
L R0,0(R3) a(j)
|
||||
LR R1,RK k
|
||||
SLA R1,2 .
|
||||
ST R0,A-4(R1) a(k)=a(j)
|
||||
ST RT,0(R3) a(j)=temp;
|
||||
LA RJ,1(RJ) j=j+1
|
||||
ENDDO , end do
|
||||
LA R3,PG pgi=0
|
||||
LA RI,1 i=1
|
||||
DO WHILE=(C,RI,LE,N) do i=1 to n
|
||||
LR R1,RI i
|
||||
SLA R1,2 .
|
||||
L R2,A-4(R1) a(i)
|
||||
XDECO R2,XDEC edit a(i)
|
||||
MVC 0(4,R3),XDEC+8 output a(i)
|
||||
LA R3,4(R3) pgi=pgi+4
|
||||
LA RI,1(RI) i=i+1
|
||||
ENDDO , end do
|
||||
XPRNT PG,L'PG print buffer
|
||||
L R13,4(0,R13) epilog
|
||||
LM R14,R12,12(R13) "
|
||||
XR R15,R15 "
|
||||
BR R14 exit
|
||||
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1'
|
||||
DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74'
|
||||
N DC A((N-A)/L'A) number of items of a
|
||||
PG DC CL80' ' buffer
|
||||
XDEC DS CL12 temp for xdeco
|
||||
YREGS
|
||||
RI EQU 6 i
|
||||
RJ EQU 7 j
|
||||
RK EQU 8 k
|
||||
RT EQU 9 temp
|
||||
END SELECSRT
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
import Data.List (delete)
|
||||
|
||||
selSort :: (Ord a) => [a] -> [a]
|
||||
selSort [] = []
|
||||
selSort xs = let x = maximum xs in selSort (remove x xs) ++ [x]
|
||||
where remove _ [] = []
|
||||
remove a (x:xs)
|
||||
| x == a = xs
|
||||
| otherwise = x : remove a xs
|
||||
selSort xs = selSort (delete x xs) ++ [x]
|
||||
where x = maximum xs
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
function selectionSort(nums) {
|
||||
var len = nums.length;
|
||||
for(var i = 0; i < len; i++) {
|
||||
var minAt = i;
|
||||
for(var j = i + 1; j < len; j++) {
|
||||
if(nums[j] < nums[minAt])
|
||||
minAt = j;
|
||||
}
|
||||
|
||||
if(minAt != i) {
|
||||
var temp = nums[i];
|
||||
nums[i] = nums[minAt];
|
||||
nums[minAt] = temp;
|
||||
}
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
fun <T : Comparable<T>> Array<T>.selection_sort() {
|
||||
for (i in 0..size - 2) {
|
||||
var k = i
|
||||
for (j in i + 1..size - 1)
|
||||
if (this[j] < this[k])
|
||||
k = j
|
||||
|
||||
if (k != i) {
|
||||
val tmp = this[i]
|
||||
this[i] = this[k]
|
||||
this[k] = tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val i = arrayOf(4, 9, 3, -2, 0, 7, -5, 1, 6, 8)
|
||||
i.selection_sort()
|
||||
println(i.joinToString())
|
||||
|
||||
val s = Array(i.size, { -i[it].toShort() })
|
||||
s.selection_sort()
|
||||
println(s.joinToString())
|
||||
|
||||
val c = arrayOf('z', 'h', 'd', 'c', 'a')
|
||||
c.selection_sort()
|
||||
println(c.joinToString())
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
sub selectionSort(@tmp) {
|
||||
for ^@tmp -> $i {
|
||||
my $min = $i; @tmp[$i, $_] = @tmp[$_, $i] if @tmp[$min] > @tmp[$_] for $i^..^@tmp;
|
||||
}
|
||||
return @tmp;
|
||||
}
|
||||
|
|
@ -1,35 +1,29 @@
|
|||
/*REXX program sorts a stemmed array using the selection-sort algorithm.*/
|
||||
@. = /*assign a default value to stem.*/
|
||||
@.1 = '---The seven hills of Rome:---'
|
||||
@.2 = '=============================='
|
||||
@.3 = 'Caelian'
|
||||
@.4 = 'Palatine'
|
||||
@.5 = 'Capitoline'
|
||||
@.6 = 'Virminal'
|
||||
@.7 = 'Esquiline'
|
||||
@.8 = 'Quirinal'
|
||||
@.9 = 'Aventine'
|
||||
do k=1 until @.k=='' /*find the number of array items.*/
|
||||
end /*k*/ /* [↑] find the "null" item. */
|
||||
items=k-1 /*adjust the # of items slightly.*/
|
||||
call show@ 'before sort' /*show the before array elements,*/
|
||||
call selectionSort items /*invoke the selection sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SELECTIONSORT subroutine────────────*/
|
||||
/*REXX program sorts a stemmed array using the selection-sort algorithm. */
|
||||
@.=; @.1 = '---The seven hills of Rome:---'
|
||||
@.2 = '=============================='
|
||||
@.3 = 'Caelian'
|
||||
@.4 = 'Palatine'
|
||||
@.5 = 'Capitoline'
|
||||
@.6 = 'Virminal'
|
||||
@.7 = 'Esquiline'
|
||||
@.8 = 'Quirinal'
|
||||
@.9 = 'Aventine'
|
||||
do #=1 until @.#==''; end; #=#-1 /*find the number of items in the array*/
|
||||
/* [↑] adjust # ('cause of DO index)*/
|
||||
call show 'before sort' /*show the before array elements. */
|
||||
say copies('▒', 65) /*show a nice separator line (fence). */
|
||||
call selectionSort # /*invoke selection sort (and # items). */
|
||||
call show ' after sort' /*show the after array elements. */
|
||||
exit /*stick a fork in it, we're a;; done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
selectionSort: procedure expose @.; parse arg n
|
||||
|
||||
do j=1 for n-1
|
||||
_=@.j; p=j; do k=j+1 to n
|
||||
if @.k<_ then do; _=@.k; p=k; end
|
||||
end /*k*/
|
||||
if p==j then iterate /*if the same, order of items OK.*/
|
||||
_=@.j; @.j=@.p; @.p=_ /*swap two items out-of-sequence.*/
|
||||
end /*j*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: w=length(items); do i=1 for items
|
||||
say 'element' right(i,w) arg(1)":" @.i
|
||||
end /*i*/
|
||||
say copies('─',79) /*show a nice separator line. */
|
||||
return
|
||||
do j=1 for n-1
|
||||
_=@.j; p=j; do k=j+1 to n
|
||||
if @.k<_ then do; _=@.k; p=k; end
|
||||
end /*k*/
|
||||
if p==j then iterate /*if the same, the order of items OK. */
|
||||
_=@.j; @.j=@.p; @.p=_ /*swap 2 items that're out-of-sequence.*/
|
||||
end /*j*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do i=1 for #; say ' element' right(i,length(#)) arg(1)":" @.i; end; return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
def selectionSort[T <% Ordered[T]](list: List[T]): List[T] = {
|
||||
def remove(e: T, list: List[T]): List[T] =
|
||||
list match {
|
||||
case Nil => Nil
|
||||
case x :: xs if x == e => xs
|
||||
case x :: xs => x :: remove(e, xs)
|
||||
}
|
||||
|
||||
list match {
|
||||
case Nil => Nil
|
||||
case _ =>
|
||||
val min = list.min
|
||||
min :: selectionSort(remove(min, list))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue