Selection sort

From Algorithmist

Jump to: navigation, search

With selection sort, you essentially take the smallest entry from the unsorted portion of an array and build a sorted array at the front, entry by entry.

[edit] Algorithm

  1. At each iteration find the smallest entry (the "key") in the unsorted portion of the array.
  2. Swap the "key" with the the ith entry.


[edit] Pseudo-code

  • lst is an array
func selsrtI(lst)
    max = length(lst) - 1

    for i from 0 to max
        key = lst[i]
        keyj = i

        for j from i+1 to max
            if lst[j] < key
                key = lst[j]
                keyj = j

        lst[keyj] = lst[i]
        lst[i] = key

    return lst

end func

Personal tools