from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def selection_sort(a_list):
    """Selection Sort algortihm.

    Time complexity: O(n^2).
    """
    for max_slot in reversed(range(len(a_list))):
        select_slot = 0
        for slot in range(1, max_slot + 1):
            if a_list[slot] > a_list[select_slot]:
                select_slot = slot

        a_list[select_slot], a_list[max_slot] = (
            a_list[max_slot], a_list[select_slot])


def main():
    a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    print('a_list: {}'.format(a_list))
    print('By selection sort: ')
    selection_sort(a_list)
    print(a_list)


if __name__ == '__main__':
    main()

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def selection_sort(ls):
    """Selection Sort algortihm.

    Time complexity: O(n^2).
    Space complexity: O(1).
    """
    # Start from the last elemenet reversely: len(ls) - 1, ..., 0.
    for i_max in reversed(range(len(ls))):
        # Select the next max, and interchange it with corresponding element.
        s = 0
        for i in range(1, i_max + 1):
            if ls[i] > ls[s]:
                s = i
        ls[s], ls[i_max] = ls[i_max], ls[s]


def main():
    ls = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    print('List: {}'.format(ls))
    print('By selection sort: ')
    selection_sort(ls)
    print(ls)


if __name__ == '__main__':
    main()

Refactor selection sort w/ adding comments
Refactor selection sort w/ adding comments

  from __future__ import absolute_import
  from __future__ import print_function
  from __future__ import division
  
  
- def selection_sort(a_list):
+ def selection_sort(ls):
      """Selection Sort algortihm.
  
      Time complexity: O(n^2).
+     Space complexity: O(1).
      """
+     # Start from the last elemenet reversely: len(ls) - 1, ..., 0.
-     for max_slot in reversed(range(len(a_list))):
+     for i_max in reversed(range(len(ls))):
-         select_slot = 0
+         # Select the next max, and interchange it with corresponding element.
+         s = 0
-         for slot in range(1, max_slot + 1):
+         for i in range(1, i_max + 1):
+             if ls[i] > ls[s]:
+                 s = i
+         ls[s], ls[i_max] = ls[i_max], ls[s]
-             if a_list[slot] > a_list[select_slot]:
-                 select_slot = slot
- 
-         a_list[select_slot], a_list[max_slot] = (
-             a_list[max_slot], a_list[select_slot])
  
  
  def main():
-     a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
+     ls = [54, 26, 93, 17, 77, 31, 44, 55, 20]
-     print('a_list: {}'.format(a_list))
+     print('List: {}'.format(ls))
      print('By selection sort: ')
-     selection_sort(a_list)
+     selection_sort(ls)
-     print(a_list)
+     print(ls)
  
  
  if __name__ == '__main__':
      main()
  
Refactor selection sort w/ adding comments
## Code Before:
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def selection_sort(a_list):
    """Selection Sort algortihm.

    Time complexity: O(n^2).
    """
    for max_slot in reversed(range(len(a_list))):
        select_slot = 0
        for slot in range(1, max_slot + 1):
            if a_list[slot] > a_list[select_slot]:
                select_slot = slot

        a_list[select_slot], a_list[max_slot] = (
            a_list[max_slot], a_list[select_slot])


def main():
    a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    print('a_list: {}'.format(a_list))
    print('By selection sort: ')
    selection_sort(a_list)
    print(a_list)


if __name__ == '__main__':
    main()

## Instruction:
Refactor selection sort w/ adding comments
## Code After:
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def selection_sort(ls):
    """Selection Sort algortihm.

    Time complexity: O(n^2).
    Space complexity: O(1).
    """
    # Start from the last elemenet reversely: len(ls) - 1, ..., 0.
    for i_max in reversed(range(len(ls))):
        # Select the next max, and interchange it with corresponding element.
        s = 0
        for i in range(1, i_max + 1):
            if ls[i] > ls[s]:
                s = i
        ls[s], ls[i_max] = ls[i_max], ls[s]


def main():
    ls = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    print('List: {}'.format(ls))
    print('By selection sort: ')
    selection_sort(ls)
    print(ls)


if __name__ == '__main__':
    main()
