avp-rag-system / data /selection_sort.avp
valachmr
Added .avp files from the pre-implemented library
70f40ac unverified
raw
history blame contribute delete
879 Bytes
// selection sort
a1 = arr[1, 10, 5, 6, 4, 7, 19]
fun Selection_Sort(@init<table> array):
n = length(array)
// Outer loop: iterate thru all index positions except the last
for (i = 0, i < n - 1, i += 1):
// assume the current index position holds min element
@mark<i, outline, yellow> minIdx = i
// Inner loop: find actual minimum element in remaining
// unsorted portion of array
for (j = i + 1, j < n, j += 1):
@mark<j, outline, yellow> if (array[j] < array[minIdx]):
@mark<j, text, fg> minIdx = j; // new minimum value index found
end if
end for
// Swap Values when a smaller value is encountered
@mark<minIdx, highlight, green> swap(array[i], array[minIdx])
end for
return array
end fun
result = Selection_Sort(a1)