Selection Sort Algorithm
Selection Sort Algorithm
In this document, we will explain the Selection Sort algorithm, which is a simple but
inefficient sorting algorithm that works by repeatedly finding the minimum element from
the unsorted part of the list and swapping it with the first unsorted element.
The Selection Sort algorithm works by selecting the smallest element from the unsorted
portion of the list and moving it to the sorted portion.
This process is repeated for all the elements in the list until the entire array is sorted.
3. Pseudocode
Début
Pour i allant de 0 à n-2
minIdx ← i
Pour j allant de i+1 à n-1
Si tableau[j] < tableau[minIdx]
minIdx ← j
Fin Si
Fin Pour
Si minIdx ≠ i
échanger tableau[i] et tableau[minIdx]
Fin Si
Fin Pour
Fin
4. Visual Representation
We will visualize how the array changes during each iteration of the Selection Sort
algorithm:
The time complexity of Selection Sort is O(n²), where "n" is the number of elements in the
array. This is because the algorithm involves two nested loops:
the outer loop runs `n` times, and the inner loop runs `n-1`, `n-2`, ..., down to 1 times.
6. Advantages and Disadvantages
7. Conclusion
In this document, we explained the Selection Sort algorithm, a simple algorithm used to sort
an array by repeatedly selecting the smallest element from the unsorted part of the array
and moving it to the sorted portion.
Despite its simplicity, it is not the most efficient algorithm for large datasets due to its O(n²)
time complexity. Selection Sort is often used in educational contexts to help students
understand basic sorting techniques, though more efficient algorithms are preferred for
practical applications.
```python
def selectionSort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
Final Thoughts