File size: 879 Bytes
70f40ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 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)