The selection sort algorithm is a simple sorting algorithm that repeatedly selects the smallest element from the unsorted portion of the list and swaps it with the element in the correct position in the sorted portion. Here's a step-by-step description of how the selection sort algorithm works:
* ALGORITHM'S :
1. Start with an unsorted list of elements.
2. Find the minimum element in the unsorted portion of the list.
3. Swap the minimum element with the first element in the unsorted portion.
4. Move the boundary between the sorted and unsorted portions one element to the right.
5. Repeat steps 2-4 until the entire list is sorted.
Here's a visual representation of the selection sort algorithm on an example list `[64, 25, 12, 22, 11]`:
```
[64, 25, 12, 22, 11] // Initial unsorted list
[11, 25, 12, 22, 64] // Swap 11 (minimum) with 64
[11, 12, 25, 22, 64] // Swap 12 (minimum) with 25
[11, 12, 22, 25, 64] // Swap 22 (minimum) with 12
[11, 12, 22, 25, 64] // Swap 25 (minimum) with 25 (no change)
[11, 12, 22, 25, 64] // The entire list is sorted
```
At each iteration, the smallest element from the unsorted portion is selected and swapped with the appropriate position in the sorted portion. This process is repeated until the entire list is sorted.
Selection sort has an average and worst-case time complexity of O(n^2), where n is the number of elements in the list. It is an in-place sorting algorithm, meaning it doesn't require additional memory for sorting. However, it is not the most efficient sorting algorithm for large lists and is generally used for educational purposes or when the list size is small.
1. Program of Selection sorting using C language .
- #include <stdio.h>
- void selectionSort(int arr[], int n) {
- int i, j, minIndex, temp;
- // Traverse through all array elements
- for (i = 0; i < n - 1; i++) {
- // Find the minimum element in the unsorted portion
- minIndex = i;
- for (j = i + 1; j < n; j++) {
- if (arr[j] < arr[minIndex])
- minIndex = j;
- }
- // Swap the found minimum element with the first element
- temp = arr[i];
- arr[i] = arr[minIndex];
- arr[minIndex] = temp;
- }
- }
- int main() {
- int arr[] = {64, 25, 12, 22, 11, 45, 78, 90}; // user input any number of list .
- int n = sizeof(arr) / sizeof(arr[0]);
- int i;
- printf("Unsorted array: ");
- for (i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- selectionSort(arr, n);
- printf("Sorted array: ");
- for (i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- return 0;
- }
OUTPUT :
- Unsorted array: 64 25 12 22 11 45 78 90
- Sorted array: 11 12 22 25 45 64 78 90
- ]def selection_sort(arr):
- n = len(arr)
- # Traverse through all array elements
- for i in range(n - 1):
- # Find the minimum element in the unsorted portion
- min_index = i
- for j in range(i + 1, n):
- if arr[j] < arr[min_index]:
- min_index = j
- # Swap the found minimum element with the first element
- arr[i], arr[min_index] = arr[min_index], arr[i]
- return arr
- # Example usage
- arr = [64, 25, 12, 22, 11, 54, 76, 89, 19]
- sorted_arr = selection_sort(arr)
- print("Sorted array:", sorted_arr)
OUTPUT :
- Sorted array: [11, 12, 19, 22, 25, 54, 64, 76, 89]