# How to writing a selection sorting for python

Like all sorting algorithms, we consider a list to be sorted only if it is in the ascending order. This process continues moving unsorted array boundary by one element to the right.

## Flowchart for selection sort in python

Using asymptotic notation, Selection Sort is O n2. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. If you compare with the bubble sort, in every pass we swap the elements multiple times. Of course in Python I would just use list. Selection Sort scans the list and picks the smallest item and puts it at the beginning of the list. After the second pass, two elements in the array will be sorted. The first position where 14 is stored presently, we search the whole list and find that 10 is the lowest value. This process continues till all the elements are sorted. Initially, the sorted part is empty and the unsorted part is the entire list. But we don't actually care about the min value; we care about the index.

So after min is done, we just throw away the actual value but keep the index. Initially, the first element is considered the minimum and compared with other elements.

## Flowchart for selection sort in python

Memory Complexity of Selection Sort Selection sort does not require any extra memory except for swapping. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. I believe this is the most efficient way to do selection sort in Python. We find that 14 is the second lowest value in the list and it should appear at the second place. If there is a tie, min will use the second part of the tuple, the index, as a tie-breaker. It's nice to get away from the office and complete my assignments on my iPad without having to bring the MacBook Pro. After the second pass, two elements in the array will be sorted. So in each pass, you need to swap the elements at most one time. As the algorithm proceeds, the sorted portion of the list will grow and the unsorted portion will continue to shrink. Like all sorting algorithms, we consider a list to be sorted only if it is in the ascending order. If you understand the logic behind Python selection sort, it is easy to implement in any programming language. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array.

Then when min evaluates to find minimum, it finds the lowest tuple value; since the value comes first in the tuple before the index, the value will be the important part, and min will find the lowest value. Now consider the second element in the list to be the smallest and so on till all the elements in the list are covered.

During these comparisons, if a smaller element is found then that is considered the new minimum.

We find that 14 is the second lowest value in the list and it should appear at the second place.

Rated 10/10
based on 89 review

Download