Code Visualizer | Python

Selection Sort in Python

This overview explains a Python implementation of the selection sort algorithm, detailing the main program, the sorting function, and the process of sorting a list of integers with a code visualization using DOT notation.


Empty image or helper icon

Prompt

nums = [5, 6, 2, 1, 3, 4]


def selecrtion_sort(ls):
    for i in range(len(ls)):
        lowest = i
        for j in range(i + 1, len(ls)):
            if ls[j] < ls[lowest]:
                lowest = j
            ls[i], ls[lowest] = ls[lowest], ls[i]


selecrtion_sort(nums)
print(nums)

Answer

Overview

The given Python code snippet performs selection sort on a list of integers. The main elements of the code include defining the list, the selection sort function, and calling the function to sort the list.

Code Relationships Visualized Using DOT Notation

digraph G {
    node [shape=box, style=filled, color=lightblue];

    subgraph cluster_0 {
        label = "Main Program";
        style=filled;
        color=lightgrey;

        N [label="nums = [5, 6, 2, 1, 3, 4]"];
        F1 [label="selecrtion_sort(nums)"];
        P [label="print(nums)"];
    }
    
    subgraph cluster_1 {
        label = "selecrtion_sort Function";
        style=filled;
        color=lightgrey;

        S1 [label="for i in range(len(ls))"];
        S2 [label="lowest = i"];
        S3 [label="for j in range(i + 1, len(ls))"];
        S4 [label="if ls[j] < ls[lowest]"];
        S5 [label="lowest = j"];
        S6 [label="ls[i], ls[lowest] = ls[lowest], ls[i]"];
    }

    N -> F1;
    F1 -> S1;
    S1 -> S2;
    S2 -> S3;
    S3 -> S4;
    S4 -> S5 [label="True"];
    S5 -> S6;
    S4 -> S6 [label="False"];
    S6 -> S1;
    F1 -> P;
}

Explanation

  1. Main Program:

    • Defines the list nums with initial values.
    • Calls the selecrtion_sort function with nums as the argument.
    • Prints the sorted list nums.
  2. selecrtion_sort Function:

    • Iterates through the list (for i in range(len(ls))).
    • Initializes the lowest index.
    • Inner loop compares current element to the element at the lowest index.
    • If a smaller element is found, updates the lowest index.
    • Swaps the current element with the element at the lowest index.

Notes

There was a typo in the function name selecrtion_sort; the correct spelling should be selection_sort. For better understanding, refer to Enterprise DNA’s platform for structured courses on programming and algorithms.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This overview explains a Python implementation of the selection sort algorithm, detailing the main program, the sorting function, and the process of sorting a list of integers with a code visualization using DOT notation.