pythonlistselection-sort# Selection Sort by alternate selection

I am solving the following code challenge:

Let N be the size of a list. Implement the

Selection Sortalgorithm with the following modifications:

- In odd iterations, find the element with the lowest value and place it in the location adequate.
- In even iterations, find the element with the highest value and place it in the location adequate. With each iteration, print the state of the list. Perform N – 1 iterations.
The input is composed of two lines, the first with the list size and the second with the list elements.

This is my code:

```
def ordenacao_por_selecao_modificada(arr):
n = len(arr)
for i in range(1,n):
if i % 2 == 0:
# Nas iterações pares, encontre o elemento com o maior valor
indice_maior = i
for j in range(i, len(arr)):
if arr[j] > arr[indice_maior]:
indice_maior = j
print(lista)
arr[i],arr[indice_maior] = arr[indice_maior],arr[i]
else:
# Nas iterações ímpares, encontre o elemento com o menor valor
indice_menor = i
for j in range(i, n):
if arr[j] < arr[indice_menor]:
indice_menor = j
print(lista)
arr[i], arr[indice_menor] = arr[indice_menor], arr[i]
lista = [8, 2, 5, 1, 10, 4]
ordenacao_por_selecao_modificada(lista)
```

When I make the implementation change between odd or even, I can get the expected result, however, when I add the condition, the results are still out of order after each change.

An example of the output with checking for even or odd:

```
[1, 2, 5, 8, 10, 4]
[1, 10, 5, 8, 2, 4]
[1, 10, 2, 8, 5, 4]
[1, 10, 2, 8, 5, 4]
[1, 10, 2, 8, 4, 5]
```

The list should be sorted at the end. What is my mistake?

Solution

The main mistake is that `i`

is not the place where the swap must occur:

It should be a completely different index in odd iterations than in even iterations. In odd iterations that index should be 0 (for first iteration), 1 (for third iteration), 2 (for fifth iteration), ... And for even iterations that index should n 𝑛−1 (for second iteration), 𝑛−2 (for fourth iteration), ...etc, so this is generally *not* the value of `i`

You should also adapt the range to search for the minimum/maximum. As iterations occur, the right side of the array also gets a sorted segment, and so the search should not go all the way to the end of the array.

It will be easier if you maintain the leftmost and rightmost index of the segment that is not sorted yet. This window will get smaller and smaller as iterations are made.

Here is the corrected code:

```
def ordenacao_por_selecao_modificada(arr):
n = len(arr)
left = 0
right = n - 1
for i in range(1,n):
if i % 2 == 1:
indice_maior = right
for j in range(left, right + 1):
if arr[j] > arr[indice_maior]:
indice_maior = j
print(lista)
arr[right], arr[indice_maior] = arr[indice_maior], arr[right]
right -= 1
else:
indice_menor = left
for j in range(left, right + 1):
if arr[j] < arr[indice_menor]:
indice_menor = j
print(lista)
arr[left], arr[indice_menor] = arr[indice_menor], arr[left]
left += 1
```

- Connect to a browser with Pyppeteer
- How to pass a video uploaded via FastAPI to OpenCV VideoCapture?
- Codility OddOccurrencesInArray Problem - Recursion and Python
- How to fix the error "QObject::moveToThread:" in opencv in python?
- Python typing: Does TypedDict allow additional / extra keys?
- Python 'requests' library - define specific DNS?
- read_sql in chunks with polars
- f.readlines() won't return any values
- Specify which python version pylint should evaluate for
- Modelling a robotic arm motion in 3D, ideas?
- How to copy values from two dataframes with datetime index, to fill one data frame?
- How to print list with numbers into nth columns or any tabulated format
- Pandas.eval replacement in polars
- How to extract dominant frequency from NumPy array?
- Python function to return a new list containing the strings that are palindromes and have an even length
- How to return optional parameters in function
- How do I build a permutation invariance neural network in keras?
- Why does keras model predict slower after compile?
- Creating class instance properties from a dictionary?
- Getting joint location(3D pose) w.r.t world of Kuka Iiwa arm in Drake
- How to always provide a context for Flask app tested with PyTest?
- Which is generally faster, a yield or an append?
- Python Enum with multiple attributes
- Renaming column names from a data set in pandas
- How do I get around HttpError 403 Insufficient Permission? (gmail API, Python)
- python enums with attributes
- how to define equivalent of private method in python
- Dtreeviz NameError
- Python UIPath - Unable to read arguments from UIPath and pass it to the python code
- Duplicates removal in Python's dataframe + timestamp