Wednesday, 27 February 2008

selection sort

Selection sort in an algorithm which sorts a sequence of elements. The smallest element is removed from the sequence and added to the end of a new sorted sequence. This continues until all elements have been moved to the new sequence.

Selection sort is an O(n²) algorithm. The redcode implemention is 10 instructions long and requires 1.5n²+5.5n-8 cycles in the worst case, making it approximately twice as fast as Bubble Sort.

Implementation in Redcode:
          org    outer+1

temp equ (outer-1)

outer mov.b x, y
mov <p, temp
inner slt <p, *p p, p
y djn inner, #LENGTH-1
add.b x, p
mov *p, @p
mov temp, *p
x djn outer, #LENGTH-1

No comments: