Algorithm
Selection Sort is a simple sorting algorithm that works by repeatedly selecting the minimum element from the unsorted portion of the array and putting it at the beginning. The algorithm maintains two subarrays: the subarray of elements already sorted and the subarray of elements yet to be sorted.
Here is a step-by-step explanation of the Selection Sort algorithm:
-
Find the minimum element: Iterate through the unsorted portion of the array to find the minimum element.
-
Swap with the first element: Swap the found minimum element with the first element of the unsorted portion.
-
Expand the sorted subarray: Expand the sorted subarray to include the newly sorted element by moving the boundary between the sorted and unsorted subarrays.
-
Repeat: Repeat steps 1-3 until the entire array is sorted
Code Examples
#1 Selection Sort Algorithm implement in Python
Code -
Python Programming
def selection_sort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Find the minimum element in the remaining unsorted array
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]
# Example usage:
arr = [64, 25, 12, 22, 11]
selection_sort(arr)
print("Sorted array:", arr)
Copy The Code &
Try With Live Editor
Output
#2 Selection Sort Algorithm implement in Java
Code -
Java Programming
public class SelectionSort {
public static void main(String[] args) {
int[] array = {64, 25, 12, 22, 11};
System.out.println("Original array:");
printArray(array);
selectionSort(array);
System.out.println("\nSorted array:");
printArray(array);
}
// Function to perform selection sort on the given array
public static void selectionSort(int[] array) {
int n = array.length;
// One by one move the boundary of the unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted array
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
// Function to print an array
public static void printArray(int[] array) {
for (int value : array) {
System.out.print(value + " ");
}
System.out.println();
}
}
Copy The Code &
Try With Live Editor
#3 Selection Sort Algorithm implement in C
Code -
C Programming
#include <stdio.h>
// Function to perform selection sort
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
// Iterate through the array
for (i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted array
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[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// Function to print an array
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
// Perform selection sort
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Copy The Code &
Try With Live Editor
#4 Selection Sort Algorithm implement in C++
Code -
C++ Programming
#include <iostream>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
// Find the minimum element in the unsorted part of the array
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
std::swap(arr[i], arr[minIndex]);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original array: ";
printArray(arr, n);
selectionSort(arr, n);
std::cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
Copy The Code &
Try With Live Editor
Demonstration
Selection Sort Data Structure and Algorithm