Search code examples
sortingselection-sort

Can't understand authors description related to Insertion Sort


Insertion Sort

Can someone explain me the points circled in red? I am really confused and don't really get why did the author say so, flag is denoted by f in the book


Solution

  • I'm not sure why the article calls "f" a flag, as it's just an index. The function f(...) is supposed to count how many times "f" gets updated (f = k), to "find" the smallest element, which I assume means to swap the smallest element to a[1]. In this case the smallest element has a value of 1. If a[n] == 1, then "f" will get updated to "n" and a swap for a[1] <=> a[n] occurs. If a[n] != 1, then "f" may get updated, but not because a[n] is the smallest element in the group (the smallest element has a value of 1), so it's not counted by the function f(...). I understand the definition of f(), but I don't understand the point for defining f(...) in that manner.