Spaces:
Sleeping
Sleeping
| // selection sort | |
| a1 = arr[1, 10, 5, 6, 4, 7, 19] | |
| fun Selection_Sort(<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 | |
| <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): | |
| <j, outline, yellow> if (array[j] < array[minIdx]): | |
| <j, text, fg> minIdx = j; // new minimum value index found | |
| end if | |
| end for | |
| // Swap Values when a smaller value is encountered | |
| <minIdx, highlight, green> swap(array[i], array[minIdx]) | |
| end for | |
| return array | |
| end fun | |
| result = Selection_Sort(a1) | |